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 - Data Driven Testing

In Automation, we need to runs scripts to test the same functionality with different types of data. This data is usually stored in an excel file and accessed via the script to enter in the Application Under Test. In QTP, this is done using the DataTable .  In Selenium we can use the Apache POI API to do read an excel workbook and write the output to the excel file. Using the API is quite simple if you understand the basics. POI  - Poor Obfuscation Implementation. This name was humorously given  because Microsoft code was deliberately made difficult to reverse engineer but still it was reverse engineered. POIFS - Poor Obfuscation Implementation File System. Used to access the input file. HSSF - Horrible Spreadsheet Format This is used to read and write .xls files XSSF - XML Spreadsheet Format This is used to read and write .xlsx files. FileInputStream fs=new FileInputStream("C:\\Users\\Krishna\\selenium workspace\\Data\\firstone.xls"); POIFSFileSyst...

How to find a WebElement

Find Element & Find Elements Method The difference between “ Find Elemen t” and “ Find Elements ” method is the first returns a WebElement object otherwise it throws an exception and the latter returns a list of WebElements, it can return an empty list if no DOM elements match the query. The “Find” methods take a locator or query object called “ By ”. “ By ” methods are listed below. By ID With this strategy, the first element with the id attribute value matching the location will be returned. If no element has a matching id attribute, a  NoSuchElementException   will be raised. This is the most efficient and preferred way to locate an element, as most of the times IDs are unique. But in some cases UI developers make it having non-unique ids on a page or auto-generating the id, in both cases it should be avoided. Example : If an element is given like this: <input id=”username”></input> WebElement we=driver.findElement(By.id(“username”));  ...

Switch between browsers\windows and pop ups

These commands can be used to switch between windows, iterate through different windows opened by the webdriver, handle pop ups and frames in windows.  GetWindowHandle Command Purpose : To get the   window handle   of the current window. driver.getWindowHandle(); Returns an alphanumeric string GetWindowHandles Command Purpose : To get the   window handle   of   all   the current windows. Set<String> handle=Driver.getWindowHandles(); Returns a set of window handles SwitchTo Window Command Purpose : WebDriver supports   moving   between named windows using the “switchTo” method. For(String handle:driver.getWindowHandles()) Driver.switchto().window(handle); Or Driver.switchto().window(windowname); <A href="newwindow.html" target=" windowname ">click here for a new window </A> SwitchTo Frame Command Purpose : WebDriver supports moving between frames using the “ switchTo ” method. Driver.s...