Please explain the difference between @FindAll and @FindBys annotations in webdriver page factory concept.
Annotation Type FindBy 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 this allows users to quickly and easily create PageObjects. It can be used on a types as well, but will not be processed by default.
@FindBy(how = How.NAME, using = "logonName") private WebElement logonNameField; @FindBy(how = How.NAME, using = "password") private WebElement passwordField; CLASS_NAME. CSS. ID.
The annotation @FindBy is used in Pagefactory to identify an element while POM without Pagefactory uses the driver. findElement() method to locate an element.
We can use these annotations in those cases when we have more than a single criteria to to identify one or more WebElement objects.
@FindBys : When the required WebElement objects need to match all of the given criteria use @FindBys annotation
@FindAll : When required WebElement objects need to match at least one of the given criteria use @FindAll annotation
Usage:
@FindBys( {
@FindBy(className = "class1")
@FindBy(className = "class2")
} )
private List<WebElement> elementsWithBoth_class1ANDclass2;
Here List elementsWithBothclass1ANDclass2 will contain any WebElement which satisfies both criteria.
@FindAll({
@FindBy(className = "class1")
@FindBy(className = "class2")
})
private List<WebElement> elementsWithEither_class1ORclass2
Here List elementsWithEither_class1ORclass2 will contain all those WebElement that satisfies any one of the criteria.
@FindAll
can contain multiple @FindBy
and will return all the elements which matches any @FindBy
in a single list.
Example:
@FindAll({
@FindBy(id = "one"),
@FindBy(id = "two")
})
public List<WebElement> allElementsInList;
Whereas,
@FindBys
will return the elements depending upon how @FindBy
specified inside it.
@FindBys({
@FindBy(id = "one"),
@FindBy(className = "two")
})
public List<WebElement> allElementsInList;
Where allElementsInList
contains all the elements having className="two"
inside id="one"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With