Last active
July 25, 2019 06:20
-
-
Save safebear/925986fbcd4a2ae8092724b770631ceb to your computer and use it in GitHub Desktop.
The Wait for Element method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static WebElement waitForElement(By locator, WebDriver driver){ | |
// Maximum time to wait in seconds | |
int WAIT = 30; | |
try { | |
// Wait for the element to appear | |
new WebDriverWait(driver, WAIT) | |
.until(ExpectedConditions | |
.numberOfElementsToBeMoreThan(locator, 0)); | |
} catch (TimeoutException e){ | |
// If it times out, print out the error, take a screenshot and then fail the test | |
e.printStackTrace(); | |
Screenshots.capturescreenshot(driver, Screenshots.generateScreenShotFileName()); | |
Assert.fail("Timeout: The element couldn't be found in " + WAIT + " seconds!"); | |
} catch (Exception e){ | |
// If any other errors occur, print out the error, take a screenshot and then fail the test | |
e.printStackTrace(); | |
Screenshots.capturescreenshot(driver, Screenshots.generateScreenShotFileName()); | |
Assert.fail("Something went wrong!"); | |
} | |
// If the element is found and no failures occur, return the element | |
return driver.findElement(locator); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment