How to Use Soft Assertions with Selenium

Alper Yigit
Beyn Technology
Published in
4 min readJun 22, 2023

Assertions are mostly used in test automation projects.It is used to compare the actual value with the expected value. When writing automation project for Youtube, if you want to check the icon is enable or there is “youtube” in url, you have to use assertions.

For example: Is the homepage word correct on the home button?

For this example we expect there should be “homepage” word on the home button. That’s why, “homepage” word our expected value. The word that writes the home button on the real site is our actual value. We must have selector of it. And now, we have both expected and actual values. We can compare them using assertions.

There are two types of assertions. These are : hard assertions and soft assertions. Generally, hard assertions are used in projects. Both have advantages and disadvantages. We have to be careful to these when choosing which one to use for our project. We should choose whichever is appropriate for the project.

Today, I will try to explain soft assertions and why they are useable. Firstly, I will briefly talk about the positive and negative aspects of both hard assertions and soft assertions. Then I will create a basic test automation project and I will use soft assertions in this project.

Hard Assertions : It uses with Assert library.

import org.testng.Assert;

Advantage : If there is an assertion failure in our test, fail logs show to us directly where is the failure.

Disadvantage : If an assertion fails, test ends and doesn’t continue. So we have to solve all the problems in order to run the test to the end. We run the test all over again for every assertion error that occurs.

Failed logs for hard assertions. It shows us where is the error

Soft Assertions : It uses with SoftAssert library. For can use it, we have to create a softAssert object.

import org.testng.asserts.SoftAssert;

Advantage : If an assertion fails, test doesn’t end and it will continue until end of the test.

Disadvantage : If there is an assertion failure in our test, fail logs don’t show to us where is the failure. It shows us just there is a fail.

Why We Use Soft Assertions ?

If we want the test to continue when there is an assertion error, we should use soft assertions. This way, we can detect all errors in a test project at once. Otherwise, if we use hard assertions, we have to run the project multiple times to be able to detect all errors. This can lead to loss of time and cost. Especially in large automation projects, using soft assertions helps us spend less effort and time

A Basic Selenium Project with Soft Assertions

I will use TestNG framework. Firstly, let’s add the relevant dependencies to pom.xml.

Selenium dependency ;

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.8.2</version>
</dependency>

TestNG dependency ;

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.1</version>
<scope>test</scope>
</dependency>

Webdrivermanager ;

<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.1.0</version>
</dependency>

CLASSES

I will use the POM scructure for this simple project. Firstly, I will create the files and classes.

1. Configuration.properties file

This file should be created for make dynamic our some values.

browser = chrome
amazonUrl = https://www.amazon.com
youtubeUrl = https://www.youtube.com
searchText = Java

2. ConfigReader

This class must be created for read to configuration.properties file.

3. Driver

We need to create this class for can manage driver and browsers.

4. Test Class

Then, we have to create an object for soft assertion.

SoftAssert softAssert = new SoftAssert();

There are many methods that we can use with this object. Most used methods ;

  • softAssert.assertEquals(actual value, expected value)
  • softAssert.assertNotEquals(actual value, expected value)
  • softAssert.assertTrue(Boolean Conditions)
  • softAssert.assertFalse(Boolean Conditions)
  • softAssert.assertNull(Object object)
  • softAssert.assertNotNull(Object object)

After that, we must write our test code and finally, for finish the assertion, we must write this ; “softAssert.assertAll();”

There are four assertions in the project. I will change line 21. Instead of “amazonUrl”, I will type “youtubeUrl”.

Although there is an error, the automation continues until the test is finished. The error returns because there is an assertion failure on line 21. But, as you can see, error logs don’t show where is the failure. .

That’s All

I hope, this article will help you. If you have a question or advice, you can contact me via my e-mail address below.

İbrahim Alper Yiğit, Test Engineer

ialper.yigit@gmail.com

Github | Linkedin

--

--