The time mapping between Script Execution on WebDriver and Eelapsed
time of AUT is called Synchronization
Elapsed time means, time taken by the application to complete specific transaction/operation
Below scenarios are some of the examples where synchronization is applied:
-during application launching time
-when application is performing back end transactions
such as Insert, update and Delete
-when application is loading next page (i.e. Page load time)
-to reach status bar 100% (i.e. uploading/downloading files)
Synchronization options:
-----------------------
There are 2 ways to synchronize test execution
1. Unconditional based Synchronization: Using "Thread" class we can pause execution to fixed time,
irrespective of application process
Syntax:
Thread.sleep(time in milliseconds);
2. Conditional based Synchronization: Based on particular element (i.e. driver.findElement) or elements
(i.e. driver.findElements) availability we can pause execution
In webdriver we have 3 types of options to synchronize test execution
a. Implicit wait:
b. Explicit wait:
c. Fluent wait:
a. Implicit wait: Using this option, we can pause execution to specified amount of time
(i.e. 30 sec) when any webelement is not able to identify on a page during runtime
Syntax:
driver.manage().timeouts().implicitlyWait(time, TimeUnit parameter);
Exp:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Exp: Write script to select "Hyderbad-MGBS" in "http://www.apsrtconline.in/oprs-web" using implicit wait
WebDriver driver= new FirefoxDriver();
driver.get("http://www.apsrtconline.in/oprs-web/");
driver.manage().window().maximize();
//implicitlywait (This applies to all the elements in this script, hence this is widely used)
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//to enter "HYD"
driver.findElement(By.id("fromPlaceName")).sendKeys("HYD");
//to pause execution
//Thread.sleep(3000);
//to select "HYDERABAD MGBS"
driver.findElement(By.linkText("HYDERABAD MGBS")).click();
b. Explicit wait: To pause execution at particular position based on presence
of webelement in DOM(i.e. to wait when webelement code not available in HTML page)
Syntax:
Wait obj=new WebDriverWait(driver, 30);
obj.until(ExpectedConditions.presenceOFElementLocated(By.id("id value of element"));
Exp: Write Explicit wait to pause execution based on "Password" webelement presence
Wait wt= new WebDriverWait(driver, 30);
wt.until(ExpectedConditions.presenceOfElementLocated(By.id("Passwd")));
Exp: Write script to select "Hyderbad-MGBS" in "http://www.apsrtconline.in/oprs-web" using explicit wait
WebDriver driver= new FirefoxDriver();
driver.get("http://www.apsrtconline.in/oprs-web/");
driver.manage().window().maximize();
//to enter "HYD"
driver.findElement(By.id("fromPlaceName")).sendKeys("HYD");
//to pause execution
//Thread.sleep(3000);
//to pause execution based on "HYDERABAD MGBS" link availability using Explicitwait
//this applies to the specific element only unlike implicitwait
Wait wt= new WebDriverWait(driver, 30);
wt.until(ExpectedConditions.presenceOfElementLocated(By.linkText("HYDERABAD MGBS")));
//to select "HYDERABAD MGBS"
driver.findElement(By.linkText("HYDERABAD MGBS")).click();
c. Fluent Wait: Using this option we can specify specified amount of time to pause execution
when webelement is not found to operate, it will try to find webelement repeatadly
on a page(DOM) based on given time paramter (i.e. polling time)
Syntax:
FluentWait wt = new FluentWait(driver).withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
Exp: WebDriver driver= new FirefoxDriver();
driver.get("http://www.apsrtconline.in/oprs-web/");
driver.manage().window().maximize();
//implicitlywait
//driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//to enter "HYD"
driver.findElement(By.id("fromPlaceName")).sendKeys("HYD");
//to pause execution
//Thread.sleep(3000);
//to pause execution based on "HYDERABAD MGBS" link availability
FluentWait wt = new FluentWait(driver).withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
wt.until(ExpectedConditions.presenceOfElementLocated(By.linkText("HYDERABAD MGBS")));
//to select "HYDERABAD MGBS"
driver.findElement(By.linkText("HYDERABAD MGBS")).click();
}
Exp: Write script to validate login functionality in Gmail application with valid data
script:
WebDriver driver= new FirefoxDriver();
driver.get("http://gmail.com");
driver.manage().window().maximize();
//driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("identifierId")).sendKeys("seleniumuser15");
driver.findElement(By.xpath("//span[text()='Next']")).click();
//to pause execution
Wait wt= new WebDriverWait(driver, 30);
wt.until(ExpectedConditions.presenceOfElementLocated(By.name("password")));
driver.findElement(By.name("password")).sendKeys("Mercury9");
driver.findElement(By.xpath("//span[text()='Next']")).click();
Thread.sleep(3000);
String pgTitle= driver.getTitle();
if (pgTitle.contains("seleniumuser15")){
System.out.println("succesful login operation");
}
else{
System.out.println("unsuccessful login operation");
}
CASESTUDY: Synchronization in WebDriver: We need to wait for elements
-to be present -->in DOM for any webelement(ImplicitWait)
-to be available -->in DOM for specific webelement (ExplicitWait)
-to be clickable -->FluentWait
time of AUT is called Synchronization
Elapsed time means, time taken by the application to complete specific transaction/operation
Below scenarios are some of the examples where synchronization is applied:
-during application launching time
-when application is performing back end transactions
such as Insert, update and Delete
-when application is loading next page (i.e. Page load time)
-to reach status bar 100% (i.e. uploading/downloading files)
Synchronization options:
-----------------------
There are 2 ways to synchronize test execution
1. Unconditional based Synchronization: Using "Thread" class we can pause execution to fixed time,
irrespective of application process
Syntax:
Thread.sleep(time in milliseconds);
2. Conditional based Synchronization: Based on particular element (i.e. driver.findElement) or elements
(i.e. driver.findElements) availability we can pause execution
In webdriver we have 3 types of options to synchronize test execution
a. Implicit wait:
b. Explicit wait:
c. Fluent wait:
a. Implicit wait: Using this option, we can pause execution to specified amount of time
(i.e. 30 sec) when any webelement is not able to identify on a page during runtime
Syntax:
driver.manage().timeouts().implicitlyWait(time, TimeUnit parameter);
Exp:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Exp: Write script to select "Hyderbad-MGBS" in "http://www.apsrtconline.in/oprs-web" using implicit wait
WebDriver driver= new FirefoxDriver();
driver.get("http://www.apsrtconline.in/oprs-web/");
driver.manage().window().maximize();
//implicitlywait (This applies to all the elements in this script, hence this is widely used)
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//to enter "HYD"
driver.findElement(By.id("fromPlaceName")).sendKeys("HYD");
//to pause execution
//Thread.sleep(3000);
//to select "HYDERABAD MGBS"
driver.findElement(By.linkText("HYDERABAD MGBS")).click();
b. Explicit wait: To pause execution at particular position based on presence
of webelement in DOM(i.e. to wait when webelement code not available in HTML page)
Syntax:
Wait
obj.until(ExpectedConditions.presenceOFElementLocated(By.id("id value of element"));
Exp: Write Explicit wait to pause execution based on "Password" webelement presence
Wait
wt.until(ExpectedConditions.presenceOfElementLocated(By.id("Passwd")));
Exp: Write script to select "Hyderbad-MGBS" in "http://www.apsrtconline.in/oprs-web" using explicit wait
WebDriver driver= new FirefoxDriver();
driver.get("http://www.apsrtconline.in/oprs-web/");
driver.manage().window().maximize();
//to enter "HYD"
driver.findElement(By.id("fromPlaceName")).sendKeys("HYD");
//to pause execution
//Thread.sleep(3000);
//to pause execution based on "HYDERABAD MGBS" link availability using Explicitwait
//this applies to the specific element only unlike implicitwait
Wait
wt.until(ExpectedConditions.presenceOfElementLocated(By.linkText("HYDERABAD MGBS")));
//to select "HYDERABAD MGBS"
driver.findElement(By.linkText("HYDERABAD MGBS")).click();
c. Fluent Wait: Using this option we can specify specified amount of time to pause execution
when webelement is not found to operate, it will try to find webelement repeatadly
on a page(DOM) based on given time paramter (i.e. polling time)
Syntax:
FluentWait wt = new FluentWait(driver).withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
Exp: WebDriver driver= new FirefoxDriver();
driver.get("http://www.apsrtconline.in/oprs-web/");
driver.manage().window().maximize();
//implicitlywait
//driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
//to enter "HYD"
driver.findElement(By.id("fromPlaceName")).sendKeys("HYD");
//to pause execution
//Thread.sleep(3000);
//to pause execution based on "HYDERABAD MGBS" link availability
FluentWait wt = new FluentWait(driver).withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
wt.until(ExpectedConditions.presenceOfElementLocated(By.linkText("HYDERABAD MGBS")));
//to select "HYDERABAD MGBS"
driver.findElement(By.linkText("HYDERABAD MGBS")).click();
}
Exp: Write script to validate login functionality in Gmail application with valid data
script:
WebDriver driver= new FirefoxDriver();
driver.get("http://gmail.com");
driver.manage().window().maximize();
//driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("identifierId")).sendKeys("seleniumuser15");
driver.findElement(By.xpath("//span[text()='Next']")).click();
//to pause execution
Wait
wt.until(ExpectedConditions.presenceOfElementLocated(By.name("password")));
driver.findElement(By.name("password")).sendKeys("Mercury9");
driver.findElement(By.xpath("//span[text()='Next']")).click();
Thread.sleep(3000);
String pgTitle= driver.getTitle();
if (pgTitle.contains("seleniumuser15")){
System.out.println("succesful login operation");
}
else{
System.out.println("unsuccessful login operation");
}
CASESTUDY: Synchronization in WebDriver: We need to wait for elements
-to be present -->in DOM for any webelement(ImplicitWait)
-to be available -->in DOM for specific webelement (ExplicitWait)
-to be clickable -->FluentWait
No comments:
Post a Comment