TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • TestNG
  • Tools
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Selenium

Page Object @FindBy Annotation

Overview

While writing tests, we can mark the elements in Page Object with @FindBy annotation. Also, it is used by web driver to locate the element on the web page. Also, it is used to mark a field on a Page Object to indicate an alternative mechanism for locating the element or a list of elements.

Furthermore, you can use the annotation in conjunction with PageFactory, this allows you to quickly and easily create PageObjects.

 

There are different ways of using @FindBy annotation.

@FindBy(how = How.HowValues, using ="value")
WebElement webElementName;
or

@FindBy(locating_mechanism ="value")
WebElement webElementName;

 

Here, ‘How’ is an enum that can have one of the following values: CLASS_NAME,  CSS,  ID,  ID_OR_NAME,  LINK_TEXT,  NAME,  PARTIAL_LINK_TEXT,  TAG_NAME,  XPATH.
Also, the “value” is an expression that defines a selector of the ‘how’ type.

FindByAnnotation

 

You can either use this annotation by specifying both “how” and “using” or by specifying one of the location strategies (eg: “id”) with an appropriate value to use. Also, both options will delegate down to the matching By methods in By class.

Both these two annotations point to the same element:
@FindBy(id = “emailID”)
WebElement emailAddress;

@FindBy(how = How.ID, using = “emailID”)
WebElement emailAddress;

One best practice is to declare elements using private attribute. Therefore, it will not be accessible from within an outside class. Furthermore, to access the value of the webElement from an outside class, you will need to declare a getter method.

@FindBy(how = How.ID, using = "emailID")
private WebElement emailAddress;
public WebElement getEmailAddress() {
return emailAddress;
}

 

Multiple WebElements

You can also obtain a list of webElements, if the selector you use to declare the webElement returns more than one results. Therefore, in this case, you will define the list of webElements as follows:

@FindBy(how = How.HowValues, using = "value")

private List<WebElement> collectionOfElements;
The getter to return a list instead of a webElement, as follows:

public List<WebElement> getCollectionOfElements() {
 return collectionOfElements;
}

 

In conclusion, once the driver gets the page, all the elements declared by findby annotation are declared by the respective locating mechanism used in the annotation.

—

Selenium Tutorials on this website:

https://www.testingdocs.com/selenium-webdriver-tutorial/

Official Website:

https://www.selenium.dev/

Related Posts

Windows 10 Settings

Selenium /

Add Microsoft Webdriver on Windows OS

Download Selenium Components

Selenium /

Getting Started with Selenium Webdriver

LambdaTest Testing Cloud SaaS Platform

Selenium /

LambdaTest – Testing Cloud SaaS Platform

Selenium /

Selenium 3.0 and Mozilla GeckoDriver

SauceLabs Website

Selenium /

Run an Example Test on SauceLabs

‹ Cool Selenium WebDriver code tricks› Selenium JavascriptExecutor Examples

Recent Posts

  • Antivirus Products for Windows 11 PC
  • Install CodeBlocks IDE on Windows 11
  • Location Privacy Settings on Windows
  • Windows 11 Virus & Threat Protection
  • Download & Install Python on Windows 11
  • Cumulative Update for Windows 11 Version
  • Test execution speed in Selenium IDE
  • Check Windows 11 Free Upgrade
  • Download IntelliJ IDE on Windows 11
  • New MySQL Connection in Workbench

Back to Top

Automation Tutorials

  • JBehave Tutorial
  • Selenium Tutorial
  • TestNG Tutorial
  • JUnit Tutorial

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version