TestNG Assertion in Selenium

Q: What is TestNG Assert and list out common TestNG Assertions?

A TestNG assertion in Selenium is a way to verify that the actual behavior of an application matches the expected behavior. It is a mechanism that allows you to define conditions that must be met in order for a test to be considered successful. If the conditions are not met, the test will fail.

To use a TestNG assertion in Selenium, you will need to import the TestNG library and use the assert method provided by TestNG. For example, you might use an assertion to verify that a particular element is present on the page, or that the text displayed on the page matches a certain value.

Some of the common assertion methods supported by TestNG are:

  • assertEqual(String actual,String expected)
  • assertEqual(String actual,String expected, String message)
  • assertEquals(boolean actual,boolean expected)
  • assertTrue(condition)
  • assertTrue(condition, message)
  • assertFalse(condition)
  • assertFalse(condition, message)

There are two types of Assert in TestNG:

  1. Hard Assert
  2. Soft Assert

When an assert fails the test script stops execution unless handled in some form. We call general assert as Hard Assert.

Hard Assert – Hard Assert throws an AssertException immediately when an assert statement fails and test suite continues with next @Test

The disadvantage of Hard Assert – It marks method as fail if assert condition gets failed and the remaining statements inside the method will be aborted.

To overcome this we need to use Soft Assert. Let’s see what is Soft Assert.

Soft Assert – Soft Assert collects errors during @Test. Soft Assert does not throw an exception when an assert fails and would continue with the next step after the assert statement.

If there is any exception and you want to throw it then you need to use assertAll() method as a last statement in the @Test and test suite again continue with next @Test as it is.

We need to create an object to use Soft Assert class which is not needed in Hard Assert.

Let’s see an example:

Here I took two methods namely softAssert() and hardAssert().

In the softAssert() method, I have used SoftAssert class and intentionally passing value false in the assertTrue() method to make it fail

In thehardAssert() method, I simply used Assert and intentionally passing parameter value false in the assertTrue() method to make it fail.

package softwareTestingMaterial;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class Assertiontest {

	@Test
	public void softAssert(){
		SoftAssert softAssertion= new SoftAssert();
		System.out.println("softAssert Method Was Started");
		softAssertion.assertTrue(false);
		System.out.println("softAssert Method Was Executed");
	}
	
	@Test
	public void hardAssert(){
		System.out.println("hardAssert Method Was Started");
		Assert.assertTrue(false);
		System.out.println("hardAssert Method Was Executed");
	}
}

Execute the above script and see the console output. You could see only one failure.

Console Output:

————————————————————-

Default suite

Total tests run: 2, Failures: 1, Skips: 0

————————————————————–

Below script is same as the first one but just added assertAll() method in the end of the first method (i.e., softAssert()).

Note:  If you forget to call assertAll() at the end of your test, the test will pass even if any assert objects threw exceptions as shown in the above example. So don’t forget to add assertAll()

package softwareTestingMaterial;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class Assertiontest {

	@Test
	public void softAssert(){
		SoftAssert softAssertion= new SoftAssert();
		System.out.println("softAssert Method Was Started");
		softAssertion.assertTrue(false);
		System.out.println("softAssert Method Was Executed");
		softAssertion.assertAll();
	}
	
	@Test
	public void hardAssert(){
		System.out.println("hardAssert Method Was Started");
		Assert.assertTrue(false);
		System.out.println("hardAssert Method Was Executed");
	}
}

Execute the above script and see the console output. There are two failures here. Second failure is due to assertAll() method

Console Output:

—————————————————————–

Default suite

Total tests run: 2, Failures: 2, Skips: 0

——————————————————————

Share the Knowledge

You May Also Like

About the Author: codenbox

Leave a Reply

Your email address will not be published. Required fields are marked *