Tuesday, 29 October 2013

How to add synchronization in selenium webdriver in Java?

We can add synchronization points in Selenium using 3 ways as mentioned below.
  1. Page Load Synchronization
  2. Element Synchronization
  3. Specific condition synchronization
Page Load Synchronization
We can set the default page navigation timeout. Below statement will set the navigation timeout as 50. This means that selenium script will wait for maximum 50 seconds for page to load. If page does not load within 50 seconds, it will throw an exception.

driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS);

Element Synchronization
We can set the default element existance timeout. Below statement will set the default object synchronization timeout as 20. This means that selenium script will wait for maximum 20 seconds for element to exist. If Web element does not exist within 20 seconds, it will throw an exception.

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

Synchronization based upon specific condition

We can also instruct selenium to wait until element is in expected condition.
To use this kind of synchronization, you will have to import WebDriverWait class using below statement.
import org.openqa.selenium.support.ui.WebDriverWait;


WebDriverWait w = new WebDriverWait(driver,20);
w.ignoring(NoSuchElementException.class);
WebElement P = null;

//below statement will wait until element becomes visible
P=w.until(ExpectedConditions.visibilityOfElementLocated(By.id("x")));

//below statement will wait until element becomes clickable.
p= w.until(ExpectedConditions.elementToBeClickable(By.id("ss")));

Sample example of Synchronization in selenium in Java
//import the required packages and classes. import java.io.File; import java.util.NoSuchElementException; import java.util.Set; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Keys; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; @SuppressWarnings("unused") public class OpenGoogle { public static void main(String [] arg) { //set the path of the chrome driver exe file System.setProperty("webdriver.chrome.driver", "C:\\Selenuim\\chromedriver2.8.exe"); //create the new instance of the chrome browser WebDriver driver = new ChromeDriver(); try{ //set the implicit wait time out to 20 seconds. driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); //set the page load timeout to 50 seconds. driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS); //navigate to given url driver.get("https://www.google.co.in"); //Maximize the browser window driver.manage().window().maximize(); Thread.sleep(2000); } catch(Exception e){ System.out.println("Exception - > " + e.toString()); } finally{ driver.close(); driver.quit(); } } //main function ends }//class ends

Explicit Synchronization

We can insert explicit synchronization points in the script using WebDriverWait class. Please remember that you have to import this class before you use it.

import org.openqa.selenium.support.ui.WebDriverWait;

import org.openqa.selenium.support.ui.ExpectedConditions;

We can instruct selenium to wait until specific element is in expected condition. For example – in below code, code will wait until element with id – x becomes visible.

 //create WebDriverWait object
 
WebDriverWait w = new WebDriverWait(driver,20);
 
//add exceptions to ignore
w.ignoring(NoSuchElementException.class);
 
WebElement P = null;
//below statement will wait until element becomes visible
 
P=w.until(ExpectedConditions.visibilityOfElementLocated(By.id("x")));
 
//below statement will wait until element becomes clickable.
p= w.until(ExpectedConditions.elementToBeClickable(By.id("ss")));


What do you think on above selenium topic. Please provide your inputs and comments. Thanks

2 comments:

  1. Hi Sagar,

    How to use chrome browser on Win7 64bit machine

    ReplyDelete
    Replies
    1. Same as for 32 bit machine. No change whatsoever.

      Delete

Buy Best Selenium Books

Contributors