Use Many Small Classes Instead of Few Large Ones

Alex Siminiuc
8 min readJun 27, 2021
Photo by Ross Sneddon on Unsplash

One of the things that makes Selenium code better is having many small, single-purpose classes instead of fewer large, multiple purpose ones.

It is common for testers that just started Selenium automation to use utility classes as containers for many, unrelated common methods.

This leads to less duplicated code and shorter tests.

Unfortunately, this approach is not correct.

Let’s see an example.

I will use the following site to implement a test case:

The test case is very simple:

  • open the site
  • wait until the page is loaded
  • create an item by typing “one” in the text box and pressing the ENTER key
  • create another item by typing “two” in the text box and pressing the ENTER key
  • check that the item count is equal to 2
  • select the first item
  • select the second item
  • check that the item count is equal to 0

The code that implements this test case is below:

import java.time.Duration;import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class SiteTest {

private String PROJECT_PATH = System.getProperty(“user.dir”);
private WebDriver driver;
private WebDriverWait wait;

private String url = “";

private By textboxXpath =
By.xpath(“//input[@placeholder = ‘What needs to be done?’]”);
private By itemCountXpath =
By.xpath(“//span[@class = ‘todo-count’]/strong”);
private String selectItemXpath =
“//label[text() =…
Alex Siminiuc