While using Pagefactory
I am directly declaring WebElement
as below.
@AndroidFindBy(accessibility = "androidLocator")
@iOSFindBy(accessibility = "iosLocator")
private MobileElement element;
But, is there a way out to handle StaleElementReference
exception as I am not using any By object here. All the solutions that I could figure out asks me to use locators as object of By.
I wanted to write a generic method in a parent class for all the page classes which handles StaleElementReferenceException
. But the problem is I can only pass the reference as a WebElement
and not as a By object which beats the purpose of reinitialize the WebElement
.
I could find the below solution :
FluentWait<MobileDriver<MobileElement>> wait = new FluentWait<MobileDriver<MobileElement>>(driver)
.withTimeout(20, TimeUnit.SECONDS).pollingEvery(500, TimeUnit.MILLISECONDS)
.ignoring(NoSuchElementException.class).ignoring(StaleElementReferenceException.class);
wait.until(new Function<WebDriver, MobileElement>() {
@Override
public MobileElement apply(WebDriver driver) {
element.get
MobileElement element = driver.findElement(by);
return element;
}
});
But the same problem occurs here too. I need to pass the reference as By
object where as in PageFactory
I have reference as WebElemrnt