Skip to content

Instantly share code, notes, and snippets.

@safebear
Last active July 25, 2019 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save safebear/925986fbcd4a2ae8092724b770631ceb to your computer and use it in GitHub Desktop.
Save safebear/925986fbcd4a2ae8092724b770631ceb to your computer and use it in GitHub Desktop.
The Wait for Element method
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