Selenium For Beginners

Page Class

Do not use method parameters for method flow

Alex Siminiuc's avatar
Alex Siminiuc
Jul 29, 2023
∙ Paid
Share

Let’s start with the following test that verifies that results on a page can be sorted:

@Test
public void can_Filter_Results_Test() {
 
   HomePage homePage = new HomePage(driver);
   homePage.open();

   Assert.assertTrue(homePage.isDisplayed());

   ResultsPage resultsPage = homePage.searchBy(KEYWORD);

   Assert.assertTrue(resultsPage.isDisplayed());

   Assert.assertTrue(resultsPage.getTotalResultCount() > 0);

   resultsPage.sortBy(PRICE_LOW_TO_HIGH);
   Assert.assertTrue(resultsPage.isSortedBy(PRICE_LOW_TO_HIGH));

   resultsPage.sortBy(PRICE_HIGH_TO_LOW);
   Assert.assertTrue(resultsPage.isSortedBy(PRICE_HIGH_TO_LOW));
   
   resultsPage.sortBy(HIGHEST_RATED);
   Assert.assertTrue(resultsPage.isSortedBy(HIGHEST_RATED));

}

The interesting part is the one about the sorting of the results:

resultsPage.sortBy(PRICE_LOW_TO_HIGH);
Assert.assertTrue(resultsPage.isSortedBy(PRICE_LOW_TO_HIGH));

resultsPage.sortBy(PRICE_HIGH_TO_LOW);
Assert.assertTrue(resultsPage.isSortedBy(PRICE_HIGH_TO_LOW));

resultsPage.sortBy(HIGHEST_RATED);
Assert.assertTrue(resultsPage.isSortedBy(HIGHEST_RATED));

The sortBy() method sorts the results in 3 different ways.

This is how it is implemented:

public void sortBy(String sortType) {

   WebElement element = driver.findElement(SORT_ID);

   Select list = new Select(element);

   switch(sortType) {

      case "priceLowToHigh":
          list.selectByValue("price-ascending");
          break;

      case "priceHighToLow":
          list.selectByValue("price-descending");
          break;

      case "highestRated":
          list.selectByValue("highest-Rated");
          break;

      default:
          throw new IllegalArgumentException(
                  "invalid sort order type - " + sortType);

   }

}

This method has a number of problems that we will look into next.

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 your SubstackGet the app
Substack is the home for great culture