Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of Annotation "@FindBy"?

Can anyone explain me about Annotation @FindBy in WebDriver?

Where and why it is used?

like image 937
mra419 Avatar asked Jan 27 '12 03:01

mra419


Video Answer


2 Answers

It's to assist with the construction of locators when using the Page Factory to support your Page Objects

PageFactory Wiki Page

However I'm discovering that I find it more useful to store your locators as By objects rather than WebElements as they are more flexible and you tend to avoid running into the StaleElementException.

By myLocator = By.id("idOfYourElement")

instead of

@FindBy(id = "idOfYourElement")
WebElement myLocator;

This way you can also use your locators when asserting the absence of an element or use it in the ExpectedConditions helpers.

like image 73
el roso Avatar answered Oct 13 '22 19:10

el roso


Can I cite API-documentation?

Used to mark a field on a Page Object to indicate an alternative mechanism for locating the element or a list of elements. Used in conjunction with PageFactory#proxyElement this allows users to quickly and easily create PageObjects.

So, if you use PageObject pattern then you adds this annotation to class members and WebDriver automatically inject appropriate WebElements to it during object initialization (when PageFactory.initElements() called).

I strongly recommend to follow this link and read about PageObject pattern and @FindBy annotations usage with more examples.

like image 26
Slava Semushin Avatar answered Oct 13 '22 21:10

Slava Semushin