There are two types of wait in Selenium
Implicit & Explicit
Implicit:
- Selenium Web Driver has borrowed the idea of implicit waits from Watir.
- An implicit wait is to tell Web Driver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.
- We should note that implicit waits will be in place for the entire time the browser is open or whole life of Webdriver object.
- This means that any search for an elements on the page could take the time the implicit wait is set for.
- This Time is applicabe to each individual instuction / Statement in test.
- Default implicit wait is 0.
Java Code:
WebDriver driver = null;
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
C# Code:
IWebDriver driver = null;
driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
Explicit Wait:
- In explicit wait you can write custom code for a particular element to wait for particular time of period before executing next steps in your test.
- This provide you better option than implicit wait.
- WebDriver introduces classes like WebDriverWait and ExpectedConditions to enforce Explicit waits into the test scripts.
- It is more suitable to handle JQuery, Ajax techniques / effect.
- FluentWait is also comes under the Explicit wait.
Java Code:
IWebDriver driver = null;
driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("month"))));
driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("month"))));
C# code:
IWebDriver driver = null;
driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By.PartialLinkText("Selenium WebDriver Practical
driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementIsVisible(By.PartialLinkText("Selenium WebDriver Practical
Guide")));
Example of WebdriverWait in Java:
package airtel;
import static org.junit.Assert.*;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.jboss.netty.util.Timeout;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class WebdriverWaitDemo
{
WebDriver driver = null;
WebElement element = null;
String actualText = null;
@Before
public void setUp() throws Exception
{
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.airtel.in/mobile/prepaid/tariffs");
}
@Test
public void test()
{
WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.elementToBeClickable(By.id("region")));
Select changeRegion = new Select(driver.findElement(By.id("region")));
List<WebElement> regionList = changeRegion.getOptions();
for (WebElement webElement : regionList)
{
if(webElement.getText().toString().contains("Maharashtra"))
{
webElement.click();
}
}
/***
* Wait to see the different plans availabe for maharashtra region.
* We will check the label Tariffs plans that are available in karnataka to Tariffs plans that are available in Maharashtra and Goa
*/
WebDriverWait waitt = new WebDriverWait(driver,10);
waitt.until(ExpectedConditions.elementToBeClickable(By.id("region")));
element = driver.findElement(By.id("fillCircleName"));
actualText = element.getText();
System.out.println(actualText);
Assert.assertEquals("WebDriverWait run successfully", "Below are the tariff plans available in Maharashtra and Goa", actualText);
}
@After
public void tearDown() throws Exception
{
/***
* Wait for 30 second to see the changes has done on browser or not.
*/
Thread.sleep(20000);
element = null;
driver.close();
}
}
==============================================================================================
Example of FluentWait in java .
package airtel;
import static org.junit.Assert.*;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.jboss.netty.util.Timeout;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.google.common.base.Predicate;
public class FluentWaitDemo
{
WebDriver driver = null;
WebElement element = null;
String actualText = null;
@Before
public void setUp() throws Exception
{
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.airtel.in/mobile/prepaid/tariffs");
}
@Test
public void test()
{
FluentWait<By> wait = new FluentWait<By>(By.id("region"));
wait.withTimeout(10, TimeUnit.SECONDS);
wait.pollingEvery(1000, TimeUnit.MILLISECONDS);
wait.until(new Predicate<By>()
{
public boolean apply(By by)
{
try
{
return driver.findElement(by).isDisplayed();
}
catch (NoSuchElementException ex)
{
return false;
}
}
});
Select changeRegion = new Select(driver.findElement(By.id("region")));
List<WebElement> regionList = changeRegion.getOptions();
for (WebElement webElement : regionList)
{
if(webElement.getText().toString().contains("Maharashtra"))
{
webElement.click();
}
}
/***
* Wait to see the different plans availabe for maharashtra region.
* We will check the label Tariffs plans that are available in karnataka to Tariffs plans that are available in Maharashtra and Goa
*/
wait.pollingEvery(1000, TimeUnit.MILLISECONDS);
wait.until(new Predicate<By>()
{
public boolean apply(By by)
{
try
{
return driver.findElement(by).isDisplayed();
}
catch (NoSuchElementException ex)
{
return false;
}
}
});
Select changeRegion = new Select(driver.findElement(By.id("region")));
List<WebElement> regionList = changeRegion.getOptions();
for (WebElement webElement : regionList)
{
if(webElement.getText().toString().contains("Maharashtra"))
{
webElement.click();
}
}
/***
* Wait to see the different plans availabe for maharashtra region.
* We will check the label Tariffs plans that are available in karnataka to Tariffs plans that are available in Maharashtra and Goa
*/
FluentWait<By> wait = new FluentWait<By>(By.id("fillCircleName"));
wait.withTimeout(10, TimeUnit.SECONDS);
wait.pollingEvery(1000, TimeUnit.MILLISECONDS);
wait.until(new Predicate<By>()
{
public boolean apply(By by)
{
try
{
return driver.findElement(by).isDisplayed();
}
catch (NoSuchElementException ex)
{
return false;
}
}
});
wait.withTimeout(10, TimeUnit.SECONDS);
wait.pollingEvery(1000, TimeUnit.MILLISECONDS);
wait.until(new Predicate<By>()
{
public boolean apply(By by)
{
try
{
return driver.findElement(by).isDisplayed();
}
catch (NoSuchElementException ex)
{
return false;
}
}
});
element = driver.findElement(By.id("fillCircleName"));
actualText = element.getText();
System.out.println(actualText);
Assert.assertEquals("WebDriverWait run successfully", "Below are the tariff plans available in Maharashtra and Goa", actualText);
}
@After
public void tearDown() throws Exception
{
/***
* Wait for 30 second to see the changes has done on browser or not.
*/
Thread.sleep(20000);
element = null;
driver.close();
}
}
Thread.Sleep(time):
This also comes under the Explicit time but the standard says instead of thread.Sleep(time) Use above defined class as "WebDriverwait" and "FluentWait".
Thread.Sleep(time) un-necessary creates / generates the delay in execution of your TestCase / testScript if the element is available within the time still Thread.sleep(time) will wait till the specified time.
So we most of the time we don't know in how much amount of time an element is available,displayed or visible on the page so better to go with above defined class.
but still in practical situation we have to use the Thread.Sleep(time). But Usage of it should be minimal.
Selenium Automation Blogs By Avinash Pande: Implicit And Explicit Wait In Selenium >>>>> Download Now
ReplyDelete>>>>> Download Full
Selenium Automation Blogs By Avinash Pande: Implicit And Explicit Wait In Selenium >>>>> Download LINK
>>>>> Download Now
Selenium Automation Blogs By Avinash Pande: Implicit And Explicit Wait In Selenium >>>>> Download Full
>>>>> Download LINK n9