How to use implicit wait in selenium webdriver and why
As you knows sometimes, some elements takes some time to appear on page when browser is loading the page. In this case, sometime your webdriver test will fail if you have not applied Implicit wait in your test case. If implicit wait is applied in your test case then webdriver will wait for specified amount of time if targeted element not appears on page. As you know, we can Set default timeout which is same as implicit wait in webdriver.
If you write implicit wait statement in you webdriver script then it will be applied automatically to all elements of your test case. I am suggesting you to use Implicit wait in your all test script of software web application with 10 to 15 seconds. In webdriver, Implicit wait statement is as bellow.
How To Write Implicit Wait In WebDriver
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
Above statement will tell webdriver to wait for 15 seconds if targeted element not found/not appears on page. Le we look at simple exemple to understand implicit wait better.
public class demoImplicitWait {
FirefoxDriver driver;
@Before
public void setUp()
{
@Before
public void setUp()
{
driver = new FirefoxDriver();
driver.get("http://jsbin.com/usidix/1");
}
@Test
public void shoudImplicitWait(){
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.cssSelector("input[type=\"button\"]")).click();
String text = driver.switchTo().alert().getText();
driver.switchTo().alert().accept();
System.out.println(text);
}
}
driver.get("http://jsbin.com/usidix/1");
}
@Test
public void shoudImplicitWait(){
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.cssSelector("input[type=\"button\"]")).click();
String text = driver.switchTo().alert().getText();
driver.switchTo().alert().accept();
System.out.println(text);
}
}
How to use explicit wait in selenium webdriver and why
Explicit waits are intelligent waits that are confined to a particular web element. Using explicit waits you are basically telling WebDriver at the max it is to wait for X units of time before it gives up.
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 provide “WebDriverWait”, “ExpectedCondition” classes to implement this.
This above classes provide set of predefined conditions to wait for the particular element to load. Following are few conditions mostly used in the Expected condition class.
alertIsPresent() : Alert is present
elementSelectionStateToBe: an element state is selection.
elementToBeClickable: an element is present and clickable.
elementToBeSelected: element is selected
frameToBeAvailableAndSwitchToIt: frame is available and frame selected.
invisibilityOfElementLocated: an element is invisible
presenceOfAllElementsLocatedBy: present element located by.
textToBePresentInElement: text present on particular an element
textToBePresentInElementValue: and element value present for a particular element.
visibilityOf: an element visible.
titleContains: title contains
public class demoExplicitWait {
public WebDriver driver;
String baseUrl;
@Test
public void testUntitled() throws Exception {
driver = new FirefoxDriver();
driver.get("http://www.wikipedia.org/");
driver.get("http://www.wikipedia.org/");
//explicit wait for search field
WebDriverWait wait = new
WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("searchInput")));
driver.findElement(By.id("searchInput")).clear();
driver.findElement(By.id("searchInput")).sendKeys("India");
driver.findElement(By.className("formBtn")).click();
}
@AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
}
_____________________________________________
_____________________________________________
No comments:
Post a Comment