Skip to main content

Selenium Wait

The different WebDriver Wait statements that can be useful for an effective scripting are listed below. It is a bad practice to use Thread.sleep() command because it makes the script slow and doesn't work with unpredictable environments.

ImplicitlyWait Command

Purpose: We can tell Selenium that we would like it to wait for a certain amount of time before throwing an exception that it cannot find the element on the page. We should note that implicit waits will be in place for the entire time the browser is open. This means that any search for elements on the page could take the time the implicit wait is set for.

WebDriver driver = new FirefoxDriver();

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

driver.get("http://url_that_delays_loading");

WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

FluentWait Command

Purpose: Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

Wait wait = new FluentWait(driver);

.withTimeout(30, Timeunit.SECONDS);

.pollingEvery(5, Timeunit.SECONDS);

.ignoring(NoSuchElementException.class);

WebElement foo = wait.until(new Function() {

public WebElement apply(WebDriver driver) {

return driver.findElement(By.id("foo"));

}

});

ExpectedConditions Command

Purpose: Models a condition that might reasonably be expected to eventually evaluate to something that is neither null nor false.

WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(<someid>)));

ExpectedConditions.presenceOfElementLocated(By.id(“elm”)); - returns WebElement
ExpectedConditions.visibilityOfElementLocated(By.id("elm")); - returns WebElement
ExpectedConditions.alertIsPresent(); - returns Alert object
ExpectedConditions.invisibilityOfElementLocated(By.id("elm")); - returns boolean
For more information click here

PageLoadTimeout Command

Purpose: Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.

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

setScriptTimeout Command

Purpose: Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.
driver.manage().timeouts().setScriptTimeout(100, TimeUnit.SECONDS);

Code to create your own ExpectedCondition command

public static ExpectedCondition<WebElement> linkpresent(By by){
return new ExpectedCondition<WebElement>(){
public WebElement apply(WebDriver driver1){
try{
return driver1.findElement(by);
}catch(Exception e){
return null;
}
}};
}


WebElement element = wait.until(linkpresent(By.id(<someid>)));

Comments

Popular posts from this blog

How to Install Selenium and execute your first program

You can install and test Selenium using the following steps. All files can be downloaded from  http://www.seleniumhq.org/download/   Download Selenium Server jar file   Download client library java jar file   Download InternetExplorer Driver file   Install eclipse workspace. (download eclipse. Unzip folder. Find eclipse.exe application and open it) Create a new java project with 1,2 as references Copy 3 to workspace For IE, go to internet options->security and change the security level so that it’s the same for all zones. Write sample java program Below program opens google in IE and searches for selenium test and closes it. // All the imports happen automatically when you use eclipse ide and add the downloaded jars in the project references package test; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.ie...

Form - User Input Automation

Text boxes WebElement t=driver.findElement(By.id(“username”)); t.sendkeys(“test name”); Buttons WebElement t=driver.findElement(By.id(“createbutton”)); t.click(); DropDown Boxes  WebElement select = driver.findElement(By.tagName("select")); List<WebElement> allOptions = select.findElements(By.tagName("option")); for (WebElement option : allOptions) { System.out.println(String.format("Value is: %s", option.getAttribute("value"))); option.click(); } This will find the first “SELECT” element on the page, and cycle through each of its OPTIONs in turn, printing out their values, and selecting each in turn. As you will notice, this isn’t the most efficient way of dealing with SELECT elements. WebDriver’s support classes include one called “Select”, which provides useful methods for interacting with these Select oSelection = new Select(driver.findElement(By.tagName("select"))); oSelection.deselectAll(); //All op...

Transition from Test Automation Engineer to Test Engineer

What's the difference? Test Automation Engineer works on automating the Regression Test Suite. Test Engineer ensures the product can be released to the customer. With my industry experience, I have realized, that, as part of automation engineer your job duties don't stop with creating Automation tests, Frameworks and running them. With more tests being automated, your responsibilities not only include running the complete Regression suite but occasionally validation and verification of the product. There are different documents/sites defining different types of testing and where their usage, however,but finding bugs is a different skill. Though we are testers by role, our analytical minds work like developers as we develop scripts too. We could be biased. When something doesn't work, we look for a workaround and think it's no big deal. This attitude is a deal breaker!Instead use your skills to analyse and break the code. Here I am going to list some of my ideas ...