Skip to content

Instantly share code, notes, and snippets.

@titusfortner
Last active March 19, 2023 17:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save titusfortner/fc2b6d892ef64be23b580ae4c3939a1c to your computer and use it in GitHub Desktop.
Save titusfortner/fc2b6d892ef64be23b580ae4c3939a1c to your computer and use it in GitHub Desktop.
Better way to Cache Page Factory Elements
private static final Duration MAX_WAIT = Duration.ofSeconds(10);
public void click(WebElement pageFactoryElement) {
click(pageFactoryElement, System.currentTimeMillis());
}
public void sendKeys(WebElement pageFactoryElement, String text) {
sendKeys(pageFactoryElement, text, System.currentTimeMillis());
}
private void click(WebElement element, long runtime) {
WebElement wrappedElement = null;
try {
wrappedElement = getElement(element);
new WebDriverWait(driver, MAX_WAIT).until((Function<WebDriver, Object>) driver -> wrappedElement.enabled?());
wrappedElement.click();
} catch (NoSuchElementException | StaleElementReferenceException e) {
validateTime(runtime, e);
click(element, runtime);
} catch (ElementNotInteractableException e) {
validateTime(runtime, e);
click(wrappedElement, runtime);
}
}
private void sendKeys(WebElement element, String text, long runtime) {
WebElement wrappedElement = null;
try {
wrappedElement = getElement(element);
element.sendKeys(text);
} catch (NoSuchElementException | StaleElementReferenceException e) {
validateTime(runtime, e);
click(element, runtime);
} catch (ElementNotInteractableException e) {
validateTime(runtime, e);
sendKeys(wrappedElement, text, runtime);
}
}
private WebElement getElement(WebElement element) {
if (element.getClass().isAssignableFrom(RemoteWebElement.class)) {
return element;
} else {
return ((WrapsElement) element).getWrappedElement();
}
}
private void validateTime(long runtime, Exception e) {
if (System.currentTimeMillis() - runtime > MAX_WAIT.toMillis()) {
throw new TimeoutException("After " + MAX_WAIT.getSeconds() + " seconds, " + e.getMessage(), e);
}
}
private static final Duration MAX_WAIT = Duration.ofSeconds(10);
public void click(WebElement pageFactoryElement) {
new WebDriverWait(driver, MAX_WAIT).until((Function<WebDriver, Object>) driver -> pageFactoryElement.enabled?());
click(getElement(pageFactoryElement), System.currentTimeMillis());
}
public void sendKeys(WebElement pageFactoryElement, String text) {
sendKeys(pageFactoryElement, text, System.currentTimeMillis());
}
private void click(WebElement element, long runtime) {
try {
element.click();
} catch (ElementNotInteractableException | NoSuchElementException | StaleElementReferenceException e) {
if (System.currentTimeMillis() - runtime > MAX_WAIT.toMillis()) {
throw new TimeoutException("After " + MAX_WAIT.getSeconds() + " seconds, " + e.getMessage(), e);
}
sendKeys(getElement(element), text, runtime);
}
}
private void sendKeys(WebElement element, String text, long runtime) {
try {
element.sendKeys(text);
} catch (ElementNotInteractableException | NoSuchElementException | StaleElementReferenceException e) {
if (System.currentTimeMillis() - runtime > MAX_WAIT.toMillis()) {
throw new TimeoutException("After " + MAX_WAIT.getSeconds() + " seconds, " + e.getMessage(), e);
}
sendKeys(getElement(element), text, runtime);
}
}
private WebElement getElement(WebElement element) {
try {
Method getWrappedElementMethod = element.getClass().getMethod("getWrappedElement");
getWrappedElementMethod.setAccessible(true);
return (WebElement) getWrappedElementMethod.invoke(element);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
return element;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment