Using ExpectedConditions for explicit wait in Selenium Webdriver.
In this article, we will discuss on different explicit wait asking the test script to wait for some time based on the expectedcondition for object defined by locator.Before discussing further, let us have a look at generic syntax for adding an explicit wait in the test using expected condition.
WebDriverWait wait = new WebDriverWait(driver, 1000);
wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("QAmail")));
In the first statement, we create an instance wait of the WebdriverWait object with arguments as driver object and the time to wait for existence of element.
In the next statement, we are asking the script to wait until the expected condition is met. In the above script, test script will wait for max 1000 second for element identified with linkText as "QAmail" to be available in the DOM for the page.
Let us define what are the different ways to use expected condition based on which we can add explicit wait in the object.
a. presenceOfElementLocated - Verify presence of element in the DOM.
WebDriverWait wait = new WebDriverWait(driver, 1000);
wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("QAmail")));
b. Using elementToBeClickable
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("TGmail")));
c. Using invisibilityOfElementLocated
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.linkText("tGmail")));
d. invisibilityOfElementWithText - Validating the invisibility of element with text for the element provided.
wait.until(ExpectedConditions.invisibilityOfElementWithText(By.xpath("//div[@id='_eE']"), "tGmail"));
e. textToBePresentInElement - Validating the text to be present in the element defined by locator.
wait.until(ExpectedConditions.textToBePresentInElement(By.xpath("//div[@id='_eE']"),"Gmail"));
f. visibilityOfElementLocated by locator.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='_eE']")));
g. titleContains - Wait until the title is displayed correctly
wait.until(ExpectedConditions.titleContains("QA Automation QTP"));
h. alertIsPresent - waits for alert to appear in the window.
wait.until(ExpectedConditions.alertIsPresent());
No comments:
Post a Comment