问题
Does the Selenium FindBy annotation actually instantiate WebElement instances, and if so, what is the connotation to the framework that uses them?
What I have been doing in my page objects looks like this right now. All my test framework methods take By locators as arguments (not WebElement instances).
//fields in the page
public static final By SEARCH_BOX = By.id("input-what");
My question is, does using FindBy instantiate WebElement instances at the time of class instantiation? If so, then I suspect my framework methods would need to take WebElement instances. Is this correct, and which is preferred in a framework: By locator arguments or WebElement arguments?
@FindBy(id = "input-what")
public WebElement SEARCH_BOX_ELEMENT;
I know I am expecting a somewhat opinionated answer. If you give me a hyperlink citation for your belief, then I think that would be a reasonable answer. I think this has far-reaching implications: for example, the methods in Selenide framework, don't take WebElement instances in their arguments and therefore PageObjects that use FindBy annotations would not be able to pass those elements to Selenide methods?
回答1:
If I understand what you are asking, your answer is on the very first line of the documentation:
In order to use the PageFactory, first declare some fields on a PageObject that are
WebElements
orList<WebElement>
...
Or possibly the end of the same documentation:
WebElements are evaluated lazily. That is, if you never use a WebElement field in a PageObject, there will never be a call to "findElement" for it.
Edit:
To complete the answer: PageObject is a coding style of collecting information about your webapp. In order to make this coding style less verbose, Selenium offers a PageFactory, which uses the @FindBy
annotation to decorate WebElements in your PageObject classes.
回答2:
I was not aware of these new annotations in selenium.
Kind of have seen page object pattern implemented through Stand Alone Weld based frameworks.
In any case, I think it's relatively clear: https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/FindBy.html
Used in conjunction with PageFactory this allows users to quickly and easily create PageObjects.
Wenever you ask the factory to return you a page object, it will inject the page object with the webelements are read at that point in time. If you page has something like comet functionality, and is going to get stale with time, you would be well advised to not expect your injected web elements to get stale.
From what I've seen so far, you want page objects to simplify your interaction with the browser content. But you want them to be stateless.
I would defnitly, as a rule of thumb, of ajax enabled pages, never have state on my page objects.
If Wanted to write to a text box. I Would offer an api:
public void writePassword(String password){
... callPrivateGetterToGetPasswordField().writeText(password);
}
And so on. I would promote methods that use short burst of read dom content and write dom content and try to not have page objects harvesting stale content.
来源:https://stackoverflow.com/questions/35490801/does-the-selenium-findby-instantiate-webelement-instances-in-a-pageobject-class