问题
I've used Pagefactory to set up all of my pages in Selenium. The thing is the test are dynamic in that some elements only exist in some of the test. From m understanding AjaxElementFactory works like this:
PageFactory.initElements(new AjaxElementLocatorFactory(driver,5), this);
@FindBy(id="ctl00_DefaultContent_RbIndividual")
WebElement OwnershipIndividual;
public void sendString(String stuff){
OwnershipIndividual.sendKeys(stuff);
}
But if the element OwnershipIndividual is not located in 5 seconds then it would throw a NoSuchElementException. My Problem is that although I have set the timeout for 5 seconds it still takes 50-60 seconds to timeout. Why is that?
回答1:
I don't see any issue as such in your code trials.
AjaxElementLocatorFactory
AjaxElementLocatorFactory is the lazyloading concept in Page Factory pattern to identify WebElements only when they are used in any operation i.e. a timeOut for a WebElement can be assigned to the Object page class with the help of AjaxElementLocatorFactory
.
Syntax:
PageFactory.initElements(new AjaxElementLocatorFactory(driver, TimeoutValue), this);
Example:
PageFactory.initElements(new AjaxElementLocatorFactory(driver,5), this);
The above code will wait for a maximum of 5 seconds until the elements specified by annotations is loaded. If the element is not found in the given time span, it will throw NoSuchElementException
.
So as per your code block, if the element isn't found after 5 seconds
, NoSuchElementException should be thrown.
Under the hood
The AjaxElementLocatorFactory creates an AjaxElementLocator using a SlowLoadableComponent which might not have finished loading when load()
returns. After a call to load()
, the isLoaded()
method should continue to fail until the component has fully loaded.
来源:https://stackoverflow.com/questions/54914570/how-to-implement-ajaxelementlocatorfactory-through-selenium-and-page-factory