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

Selenium Grid - Distributed Systems

Selenium grid is powerful and can be easily used with just few lines of code. A Grid is a network of computers which can be heterogeneous and geographically dispersed. A Selenium Grid is how a grid of computers are connected using Selenium and configured to perform a task. A task typically here is running a suite of test cases with different browsers in different platforms across geographically dispersed systems. The test cases can be run in parallel using Selenium. So if we have a grid of three computers the same set of test cases can be run in IE, Firefox and Chrome. To establish this we have three important components. 1) Hub 2) Node 3) Test Script A Hub acts like the server to which requests are sent . The Hub sends to request to various registered nodes. The HUB can be instantiated using command line by using the selenium server standalone jar file. Open command prompt, navigate to the folder which has the selenium server file and use this command. java -jar selen...

Selenium - What Why Who How?

What is Selenium? Selenium is an open source automated   testing   suite for web applications across different browsers and platforms.   It is quite similar to HP Quick Test Pro ( QTP ) only that Selenium focuses on automating web-based applications. Why use Selenium? Selenium is fast and it supports programming in multiple languages like Java, C#,Python and Ruby.  So development of automation scripts can be done in your language of preference. Selenium works well with AJAX testing. Selenium Grid supports execution of test cases in multiple platforms and multiple browsers in remote systems as well.  The support for different programming languages gave selenium the power to harness the capabilities of those programming languages .  For example, using JAVA as the programming language to code selenium scripts enables selenium to use JDBC for database access and testing.All the APIs supported in Java also become the advantage of Selenium like L...

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 ...