Showing posts with label selenium.WebDriver. Show all posts
Showing posts with label selenium.WebDriver. Show all posts

Thursday 9 January 2014

Selenium WebDriver: How to Handle Ajax Application

A common problem we face during automation using selenium WebDriver is to handle the ajax calls. It is difficult to know when the call is complete and page has been updated. There are several ways to handle the ajax calls. This blog describes them one by one.
We can handle Ajax calls in two ways:


1. Hard Wait - this is most commonly used method but not recommended while doing the automation. Only in some exceptional cases use or for short time use it
·    JAVA: Use java method Thread.Sleep( to suspend execution for specified time. Syntax –

Thread.sleep(1000) // time unit is millisecond

·   Implicit Wait – An implicit wait is to tell WebDriver to stop the DOM for a certain amount of time. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance. Syntax –

driver.manage().timeouts().Implicitly Wait(10, TimeUnit.SECONDS); 
// Time is in  seconds or we can also change Unit in second argument

2.   Explicit Wait: An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. Sample code is as follows -

WebDriver driver = new FirefoxDriver();
driver.get("http://google.co.in");
WebDriverWait myDElement = new WebDriverWait(driver, 10)

.until(ExpectedConditions.presenceOfElementLocated(By.xpath("xxxxx")));

Sunday 29 December 2013

Selenium - How to highlight Element

It is always a good practice to highlight element before performing an operation. This helps us to know where the Selenium is currently focused or which element it is being executed.

There is no native way to do this, but because Selenium allows to execute JavaScript, it can be accomplished by  executing JavaScript method. Code is as follows -

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class HighLightElement {

WebDriver driver = new FirefoxDriver(); //Launch firefox browser

@Test
   public void highlightTest() throws InterruptedException {
driver.get("https://www.facebook.com/"); // open facebook site
driver.manage().window().maximize(); // maximize browser
WebElement emailField = driver.findElement(By.xpath("//*[@id='email']")); //get
                reference to email text field
Thread.sleep(2000); //Stop program execution for 2 seconds
highlightElement(emailField); // call to highlight method


/** Method which executes Java Script code to highlight page object */
public void highlightElement(WebElement element) { 
   for (int i = 0; i < 2; i++) 
  { 
JavascriptExecutor js = (JavascriptExecutor) driver; 
js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: red; border: 5px solid red;"); 
js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, ""); 
     } 

}