Custom synchronization methods

Combining the two sets of methods into a wrapper method will allow users to synchronize the scripts on a variety of conditions that might exist on a web or mobile page. The following are examples of wrapper methods that wait for elements to become visible or invisible:

/**
* waitFor method to wait up to a designated period before
* throwing exception (static locator)
*
* @param element
* @param timer
* @throws Exception
*/
public static void waitFor(WebElement element,
int timer)
throws Exception {

WebDriver driver = CreateDriver.getInstance().getDriver();

// wait for the static element to appear
WebDriverWait exists = new WebDriverWait(driver,
timer);
exists.until(ExpectedConditions.refreshed(
ExpectedConditions.visibilityOf(element)));
}
/**
* overloaded waitFor method to wait up to a designated period before
* throwing exception (dynamic locator)
*
* @param by
* @param timer
* @throws Exception
*/
public static void waitFor(By by,
int timer)
throws Exception {

WebDriver driver = CreateDriver.getInstance().getDriver();

// wait for the dynamic element to appear
WebDriverWait exists = new WebDriverWait(driver,
timer);

// examples: By.id(id),By.name(name),By.xpath(locator),
// By.cssSelector(css)
exists.until(ExpectedConditions.refreshed(
ExpectedConditions.visibilityOfElementLocated(by)));
}
/**
* waitForGone method to wait up to a designated period before
* throwing exception if element still exists
*
* @param by
* @param timer
* @throws Exception
*/
public static void waitForGone(By by,
int timer)
throws Exception {

WebDriver driver = CreateDriver.getInstance().getDriver();

// wait for the dynamic element to disappear
WebDriverWait exists = new WebDriverWait(driver,
timer);

// examples: By.id(id),By.name(name),By.xpath(locator),
// By.cssSelector(css)
exists.until(ExpectedConditions.refreshed(
ExpectedConditions.invisibilityOfElementLocated(by)));
}
/**
* waitForURL method to wait up to a designated period before
* throwing exception if URL is not found
*
* @param by
* @param timer
* @throws Exception
*/
public static void waitForURL(String url,
int seconds)
throws Exception {

WebDriver driver = CreateDriver.getInstance().getDriver();
WebDriverWait exists = new WebDriverWait(driver,
seconds);

exists.until(ExpectedConditions.refreshed(
ExpectedConditions.urlContains(url)));
}

/**
* waitFor method to wait up to a designated period before
* throwing exception if Title is not found
*
* @param by
* @param timer
* @throws Exception
*/
public void waitFor(String title,
int timer)
throws Exception {

WebDriver driver = CreateDriver.getInstance().getCurrentDriver();
WebDriverWait exists = new WebDriverWait(driver, timer);

exists.until(ExpectedConditions.refreshed(
ExpectedConditions.titleContains(title)));
}
Notice the .refreshed method is called on ExpectedConditions classes. This is a new method that Selenium introduced to avoid StaleElementReferenceException type failures.

To summarize, any of the ExpectedConditions class methods can be wrapped in synchronization methods as in these examples to wait for element conditions like clickable, text, titles, URLs, and so on. It is important to keep in mind that these methods will only wait up to the designated time period at the most, but as soon as it finds the element, it moves on. This is unlike the behavior of a hardcoded sleep, which will wait the entire length of time passed into it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.80.45