Selenium For Beginners

Share this post

User's avatar
Selenium For Beginners
Your page class will not get smaller if you move the locators outside of it
Page Class

Your page class will not get smaller if you move the locators outside of it

Alex Siminiuc's avatar
Alex Siminiuc
Aug 24, 2023
∙ Paid

Share this post

User's avatar
Selenium For Beginners
Your page class will not get smaller if you move the locators outside of it
Share

The following page class is quite big:

public class CheckOutPage {

   private static String URL = "https://www.sell.com/checkout";

   private static final By ADDRESS_ID = By.id("address");
   private static final By CITY_ID = By.id("city");
   private static final By PROVINCE_ID = By.id("province");
   private static final By POSTAL_CODE_ID = By.id("postalcode");
   
   private static final By FIRST_NAME_ID = By.id("firstname");
   private static final By LAST_NAME_ID = By.id("lastname");
   private static final By PHONE_ID = By.id("phone");
   private static final By EMAIL_ID = By.id("email");
   
   private static final By SUB_TOTAL_ID = By.id("subtotal");
   private static final By ORDER_DISCOUNT_ID = By.id("orderdiscount");
   private static final By ESTIMATED_SHIPPING_ID = By.id("estimatedshipping");
   private static final By ESTIMATED_TAX_ID = By.id("estimatedtax");
   private static final By ESTIMATED_TOTAL_ID = By.id("estimatedtotal");
   
   private static final By CONTINUE_ID = By.id("continue");
  
   private WebDriver driver;

   public CheckOutPage(WebDriver driver) {
      this.driver = driver;
   }

   //shipping info
   public void typeAddress(String address) {
      WebElement field = driver.findElement(ADDRESS_ID);
      field.sendKeys(address);
   }

   public void typeCity(String city) {
      WebElement field = driver.findElement(CITY_ID);
      field.sendKeys(city);
   }
 
   public void typePostalCode(String postalCode) {
      WebElement field = driver.findElement(POSTAL_CODE_ID);
      field.sendKeys(postalCode);
   }

   public void selectProvince(String province) {
      WebElement field = driver.findElement(PROVINCE_ID);
      Select listbox = new Select(field);
      listbox.selectByValue(province);
   }

   //contact info
   public void typeFirstName(String name) {
      WebElement field = driver.findElement(FIRST_NAME_ID);
      field.sendKeys(name);
   }

   public void typeLastName(String name) {
      WebElement field = driver.findElement(LAST_NAME_ID);
      field.sendKeys(name);
   }

   public void typePhone(String phone) {
      WebElement field = driver.findElement(PHONE_ID);
      field.sendKeys(phone);
   }

   public void typeEmail(String email) {
      WebElement field = driver.findElement(EMAIL_ID);
      field.sendKeys(email);
   }

   //order summary
   public double getProductSubTotal() {
      WebElement field = driver.findElement(SUB_TOTAL_ID);
      return Double.parseDouble(field.getText());
   }

   public double getOrderDiscount() {
      WebElement field = driver.findElement(ORDER_DISCOUNT_ID);
      return Double.parseDouble(field.getText());
   }

   public double getEstimatedShippingPrice() {
      WebElement field = driver.findElement(ESTIMATED_SHIPPING_ID);
      return Double.parseDouble(field.getText());
   }

   public double getEstimatedTax() {
      WebElement field = driver.findElement(ESTIMATED_TAX_ID);
      return Double.parseDouble(field.getText());
   }

   public double getEstimatedTotal() {
      WebElement field = driver.findElement(ESTIMATED_TOTAL_ID);
      return Double.parseDouble(field.getText());
   }

   //place order
   public void placeOrder() {
      WebElement button = driver.findElement(CONTINUE_ID);
      button.click();
   }

   public boolean isDisplayed() {
      String url = getUrl();
      return url.contains(URL);
   }

   private String getUrl() {
      return driver.getCurrentUrl();
   }

}

It has

  • 14 locators

  • 15 methods

  • 150 lines of code

This class will grow in size because, the more automated tests are built, the more code is added to this class.

When a page class gets too big, we should try to reduce it.

How can we do this?

There are 2 things that we can try:

  • move locators out of the class

  • move methods out of the class

Keep reading with a 7-day free trial

Subscribe to Selenium For Beginners to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 Alex Siminiuc
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share