During running the automation
script we got exception is occur when Firefox driver is trying to perform
action on the element which is no longer exists or not valid due to page load
or whatever reason.
WebElement element =
driver.findElement(By.id("samplescript"));
// Before click on something happened, DOM has changed due to page refresh, or element is removed //and re-added
element.click();
// Before click on something happened, DOM has changed due to page refresh, or element is removed //and re-added
element.click();
Now at the below example,
the element which you are clicking is no longer valid due to some reason.
So it throws up the
exception and its hands and gives control to automation tester, who get to know
(the test/app author) should know exactly what may or may not happen.
In order to overcome this
situation, Need to explicitly wait until the DOM is in a state where we are about
to sure that DOM will not change.
ie, using a WebDriverWait
syntex to wait for a specific element to exist:
https://www.youtube.com/watch?v=TmHILJhJfFo
https://www.youtube.com/watch?v=odxU3L1OB-M
// times out after 10 seconds
https://www.youtube.com/watch?v=odxU3L1OB-M
// times out after 10 seconds
WebDriverWait wait = new WebDriverWait(driver, 200);
// While the following loop runing, the DOM has changes
-
// page to be refresh state or element is removed and re-added
wait.until(elementIdentified(By.id("element")));
// now it look like good - let's click the element
driver.findElement(By.id("sample")).click();
private static Function<WebDriver,WebElement> elementIdentified(final By locator) {
return new Function<WebDriver, WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
};
}
We can also use here implicit
waits, in which Webdriver API will check for the element to become present
until the specified timeout:
driver.manage().timeouts().implicitlyWait(50,
TimeUnit.SECONDS)
No comments:
Post a Comment