automationScript

How to use By Class In Selenium example with POM (Page Object Model)

One of  the most popular test automation framework design pattern using Selenium WebDriver is Page Object Model.

There are so many ways to implement page object model, honestly it depends on your imagination how you want to setup you framework.

The official documentation of Selenium says nicely explains it in below words.

There is a lot of flexibility in how the page objects may be designed, but there are a few basic rules for getting the desired maintainability of your test code.

And those basic rules are,

  1. Your framework should have a separate place for maintaining Page objects. Those page objects will provide ways to interact with the application page through well defined methods.
  2. Page objects must never have assertions or verification themselves.
  3. Locators of the web-elements should be stored in such a way that they are never duplicated.

And one of the way to implement it, is by using By class in Selenium. Check an example of How to use By class in Selenium with POM

In order to understand this concept, we have taken a simple scenario which is scripted in the class GoogleSearchPageBy.java .

  • Enter a keyword on google search page.
  • Press key ENTER

Sample Class To Use By Locators

Each  WebElement shuld be represented by a By locator. These can be used in the page class across all the methods.
* Notice the import statement 

import org.openqa.selenium.By;

 on the top.
* This is provided by Selenium WebDriver

Checkout the code repo

Subscribe for updates