Selenium For Beginners

Share this post

User's avatar
Selenium For Beginners
How to reduce the number of assertions
Assertions

How to reduce the number of assertions

part 1 - replace related variables with objects

Alex Siminiuc's avatar
Alex Siminiuc
Jun 26, 2023
∙ Paid

Share this post

User's avatar
Selenium For Beginners
How to reduce the number of assertions
Share

Have a look at this test:

@Test
public void canPlaceOrderTest() {
   HomePage homePage = new HomePage(driver);
   homePage.open();
		
   ResultsPage resultsPage = homePage.search("iphone");
   DetailsPage detailsPage = resultsPage.selectProduct(1);
		
   detailsPage.addToCart();
		
   BasketPage basketPage = detailsPage.goToBasketPage();
		
   CheckoutPage checkOutPage = basketPage.checkout();
		
   checkOutPage.addPersonalInfo(FIRST_NAME, 
                                LAST_NAME, 
                                ADDRESS, 
                                CITY, 
                                COUNTRY, 
                                POSTAL_CODE);
		
   checkOutPage.addCardInfo(CARD_INFO, 
                            CARD_EXPIRY, 
                            CARD_CODE);

   checkOutPage.addDeliveryInfo(DELIVERY_DAY, 
                                DELIVERY_TIME, 
                                DESCRIPTION, 
                                DELIVERY_METHOD);
		
   OrderReviewPage reviewPage = checkOutPage.reviewInfo();
		
   Assert.assertEquals(reviewPage.getFirstName(), FIRST_NAME);
   Assert.assertEquals(reviewPage.getLastName(), LAST_NAME);
   Assert.assertEquals(reviewPage.getAddress(), ADDRESS);
   Assert.assertEquals(reviewPage.getCity(), CITY);
   Assert.assertEquals(reviewPage.getCountry(), COUNTRY);
   Assert.assertEquals(reviewPage.getPostalCode(), POSTAL_CODE);
		
   Assert.assertEquals(reviewPage.getCardInfo(), CARD_INFO);
   Assert.assertEquals(reviewPage.getCardExpiry(), CARD_EXPIRY);
   Assert.assertEquals(reviewPage.getCardCode(), CARD_CODE);
	
   Assert.assertEquals(reviewPage.getDeliveryDay(), DELIVERY_DAY);
   Assert.assertEquals(reviewPage.getDeliveryTime(), DELIVERY_TIME);
   Assert.assertEquals(reviewPage.getDescription(), DESCRIPTION);
   Assert.assertEquals(reviewPage.getDeliveryMethod(), DELIVERY_METHOD);
		
   ConfirmationPage confirmationPage = reviewPage.placeOrder();
		
   Assert.assertTrue(confirmationPage.isOrderPlaced());						
}

It is the typical test that places an order on an e-commerce site.

It starts with searching for a keyword, then selecting a product on the results page, adding the product to the cart on details page, navigating from cart page to basket page, from basket page to checkout page.

Lots of things happen on checkout page.

The personal info is entered in the page, then the credit card info and the delivery info.

The test navigates then from checkout page to review page where it asserts that all personal, card and delivery info are correct.

Finally, the order is placed and the order confirmation message is checked.

The test is not complex but it has one major issue.

It has so many assertions, 13 assertions only on the review page:

  • 6 assertions for personal info

  • 3 assertions for credit card info

  • 4 assertions for delivery info

Is it possible to reduce the number of these assertions?


It is possible and we will do it in 3 small steps, one post for each of them.

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