Assertions in TestNG : Selenium

Assertions in selenium webdriver are nothing but the verifications or checkpoints during the execution of the program.

In Selenium we do not have any assertions, TestNG provides the assertions to verify a particular condition. Without assertions, we will not able to decide whether a test case is failed or not.

But why we need assertions when we can get to know the details of the execution either using log4j logsor using System.out.println(), Assertion are mainly used for designing the TestNG reported or result.

If we log4j logs or System.out.println(), those will have no effect on the TestNG reporter / results. We use assertions in selenium to compare various values, like comparing strings, comparing objects, to perform relational operation results.

Important Note : If a assertions result is passed then that particular assertion will not have any effect on the Test results, only assertion failures affect the result. If there is no failure, then TestNG by default considers the test case as passed.

A test case will be considered as failed, either if the assert statement results in fail or an exception is thrown inside a test case.


When an assert statement results in failure, TestNG throws AssertionError to fail the test case. Be aware that it is AssertionError, not AssertionException. Also, there is no such thing called as AssertionException

AssertionError is subclass to Error class, Error and Exception are subclasses of Throwable, I hope you remember the difference between error and exception.

AssertionError class has 9 Overloaded constructors, all the Constructors accepts only one parameter but with different parameter type like : boolean, char, float, int, long, double, String, Object along with these, there is also a default Constructor (no-parameter constructor).

Types of Assertions :
There are two types of assertions present in the TestNG :

  • Assert class / Hard Assert
  • SoftAssert class / verify

1. Assert class / Hard Assert

Assert class is present in org.testng.Assert class in TestNG under Selenium webdriver, this assert also known as Hard assert.

Hard assert compares two items and results in a pass or fail. But in-case if the assert is failed then the code after the assert statement will not be executed and because of this only we are calling it as Hard Assert.

All the method present in Assert class are static methods so we can access all the methods using class reference, so no need to create an object

Special Condition for Asserts : Asserts must be inside @Test method only, if we write assert statement inside a normal method or inside the main method then TestNG will not consider the failure or pass. Below image shows the example of failure and pass

In the above image, you can see that I have compared two values 2 and 1 as we know both values are not same and the assert fails but it is not informed in the TestNG Result tab.
Similarly in case of the pass also the result information will not be passed to TestNG tab.

Asserts present in the Assert Class/ Hard Asserts : I am explaining all the asserts present in the Assert Class Sequentially, This order may not follow on the eclipse/IDE suggestions

assertTrue :

this method gives an option to the user to compare two items, if the comparison results in true then this Assert statement is passed otherwise fails the statement.

This statement will be useful when you evaluate more than one condition using relational operators.

public class TestNGAssertTrue {
	public void testAssertTrue()
		Assert.assertTrue( 2 > 1 );
		System.out.println("compare 2 is greated than 1");

		Assert.assertTrue( "chercher tech".equals("karthiQ") );
		System.out.println("compared two not equal string & this should not be executed");

Output of Assert.asserTrue :

With this method we can also pass the failure message, saying why this condition is failed.

I hope you remember java concept of having same method with different parameters. If you know concept, please do comment at comment section.

int i = 10;
Assert.assertTrue(i < 300 && i > 5 , "Either 'i' is not less than '300' or not greater than '5'" );

assertFalse :

Similar to the assertTrue assertion, we have assertFalse statement also in TestNG assertions.

assertFalse statement evaluates the given condition and if the condition results in false then the assertFalse statement will pass the assertion but if the evaluation results in true then assertFalse statement fails the test case.

assertFalse is also an overloaded method

public class testAssertFalse {
	public void testAssertFalse()
		Assert.assertFalse( 2 > 30 , "comparing 2 greater than 30");
		System.out.println("this print will be executed as condition results in false");

		Assert.assertFalse(true == true);
		System.out.println("this will not be executed as condition results in true");

Output of Assert.assertFalse :

fail : method fails the current test with the given message, user not only can set the error message but also they can set the exception which caused the failure.

public class TestNGFail {
	public void testFail()
	{"test purpose", new NoSuchFrameException("user thrown exception"));

Output of the

assertEquals :

Assert.assertEquals method compares given two methods to check whether both are equal or not, If both expected and actual values are different then assertEquals method throws AssertionError.

assertEquals method not only compares the object but also compares the array, element by element will be compared in arrays.

Below example compares the empty list and the lists with a value.

public class TestNGAssertEquals {
	public static void main(String[] args) {
		List l = new ArrayList();
		List l2 = new ArrayList();

		// compare the empty lists
		Assert.assertEquals(l, l2);

		// compare the list with element
		Assert.assertEquals(l, l2, "both List are not same");

Output of Assert.assertEquals :

We can compare various items using assertEquals method in TestNG with selenium. Below are few overloaded methods present in TestNG

  • byte[] : actual method is assertEquals(byte[] actual, byte[] expected)
  • short[] -> Compares two short arrays
  • int[] -> Compares two in arrays
  • boolean[] -> Compares two boolean arrays
  • char[] -> Compares two char arrays
  • float[] -> Compares two float arrays
  • double[] -> Compares two double arrays
  • long[] -> Compares two long arrays
  • Object -> Compares two objects
  • String -> Compares two Strings
  • double Delta -> explained below.
  • float Delta
  • long ->Compares two long values
  • boolean -> Compares two boolean values
  • byte -> compares two byte values
  • char -> Compares two char values
  • short -> Compares two short values
  • int -> Compares two int values
  • Collection<?> -> Compares two Collections
  • Iterator<?> -> Compares two iterators
  • Iterable<?> -> Compares two Iterable
  • Set<?> -> Compares two sets
  • Map<?, ?> -> compares two maps

assertEquals : Delta

This method accepts three double values and performs a subtraction between value1-value2, if the result of the subtraction is less than the third parameter then the assertion is passed otherwise assertion failed and throws AssertionError

public class TestNGAssertEquals {
	public static void main(String[] args) {
		double value1 = 50;
		double value2 = 10;
		double value3 =70;
		Assert.assertEquals(value1, value2, value3, "Subtraction deleta is less than expected delta");

assertNotNull :

assertNotNull method compares the given value with null, if given value is null then TestNG throws AssertionError error.

public class TestNGAssertNotNull {
	static WebDriver driver ;
	public static void main(String[] args) {
		//check driver is not null

Output of Assert.assertNotNull :assertnotnull-testng-selenium-webdriver

assertNull :

assertNull method compares the given value/object with null, if the given value is null then this assertion is passed but given value is not null then this message throws AssertionError

public class TestNGAssertNull {
	public static void main(String[] args) {
		// set the geckodriver.exe property
		System.setProperty("webdriver.gecko.driver", "C:/Users/user/Pictures/geckodriver.exe");

		WebDriver driver = new FirefoxDriver();
		//check driver is null

Output of Assert.assertNull :

assertSame :

assertSame method compares the address of the objects, if the address of the object are same then assertion is passed otherwise TestNG throws an error AssertionError.

The difference between assertEquals and assertSame is, assertEquals compares the object contents whereas assertSame compares the address.

Note : When we create a string using double Quotes, it points to the already existing same string addressif any.

But when we create a String using the new keyword, the object will be created newly even though the same string is present in memory.

public class TestNGAssertSame {
	public static void main(String[] args) {
		String s1 = "Chercher tech";
		String s2 = "Chercher tech";
		// strings created with double quotes keyword will have same addresses if content is same
		Assert.assertSame(s1, s2, "Strings not sharing same address when we create using double quotes");

		String s3 = new String("Google");
		String s4 = new String("Google");
		// compare using Assert Equals, assertion passes
		Assert.assertEquals(s3,  s4, "string content comparision failed");
		// strings created with new keyword will have different addresses
		Assert.assertSame(s3, s4, "String as not sharing same address when created using 'new' keyword");

Output of Assert.assertSame program :

assertNotSame :

assertNotSame method compares the given objects' addresses are not same, if both the addresses are same then TestNG throws the AssertionError

public class TestNGAssertNotSame {
	public static void main(String[] args) {
		String s1 = "Chercher tech";
		System.out.println("s1 address is : "+System.identityHashCode(s1));
		String s2 = "Chercher tech";
		System.out.println("s2 address is : "+System.identityHashCode(s2));
		// strings created with double quotes keyword will have same addresses if content is same
		Assert.assertNotSame(s1, s2, "Strings not sharing same address when we create using double quotes");

Output of Assert.assertNotSame :

Option Present or Not in Dropdown in seleium

assertEqualsNoOrder :

assertEqualsNoOrder method compares the two Object arrays irrespective of the order of values present inside.

If both arrays contain the same values in the different/same order then the assertion is passed. If any extra value is present in any of the array then the assertion is failed.

public class TestNGAssertEqualsNoOrder {
	public static void main(String[] args) {
		Object[] objArray1 =  new Object[3];
		objArray1[0] = 1;
		objArray1[1] = "chercher tech";
		objArray1[2] = false;

		Object[] objArray2 =  new Object[3];
		objArray2[0] = "chercher tech";
		objArray2[1] = false;
		objArray2[2] = 1;
		// compare two arrays
		Assert.assertEqualsNoOrder(objArray1, objArray2);
		System.out.println("Lists compared and AssertionError didnot occur");

output of the Assert.assertEqualsNoOrder :

assertNotEquals :

assertNotEquals method compares and passes the assertion if both object are not same, Assertion results in failure if both objects are same. Similar to assertEquals method, this method is also overload with same kind of parameters.

public class TestNGAssertNotEquals {
	public static void main(String[] args) {
		int i1 =10;
		int i2 = 10;
		// compare i1, i2
		Assert.assertNotEquals(i1, i2, "i1, i2 are same");

Output of Assert.assertNotEquals :

These all are the assertions present in the TestNG Assert class

Xpath in Selenium

SoftAssert / Verify

SoftAssert in TestNG is nothing but the brother of Assert, the SoftAssert not only asserts a given condition but also gives a chance to the user to continue the execution after Assertion failure.

It is not mandatory to continue the execution of a test case after the assertion failure, but sometimes you might be checking a minor value. So just because minor verification failed you may not want to stop the execution of the test case.

Anyways once the soft assertion is failed the TestNG throws AssertionError, instead of throwing an error immediately TestNG throws the error just before completing the test case.

Example : Consider you have a test case which verifies the 100 CSS properties. Do you want to stop the Execution just because first CSS property is not matching ?, NO.

What we would like is at least we want to execute the test case for all the check points/ verification so that we will get to know how many properties are matching and how many are not matching.

All the assertion present in the SoftAssert class in TestNG are non-static methods.

Why do people call the SoftAssert as Verify ?
There is no such thing as verify in Selenium webdriver, but there was a Selenium RC a few years back, the checkpoints present in that RC are verify methods, so people remember those verify methods and they confuse it along with webdriver. So calling SoftAssert as a Verify is a mistake that we are continually making.

Methods present in SoftAssert :

Whoever knows Selenium Webdriver will say that SoftAssert class has all the methods present in Assert class, is that true ? No.

SoftAssert class has only two methods:
1. doAssert (protected method, we would not be using it)
2. assertAll()

You might have a doubt that I am able to see almost all the methods like Assert class inside SoftAssertclass. Yes, You might have seen methods but have you ever heard a concept of Inheritance in Java

The Architecture of SoftAssert :

All the assertion methods are present in Assertion class, the SoftAssert class extends the Assertion class. So through inheritance SoftAssert Class allows the user to access all the methods present in the Assertion class.

I hope you have read all the assertions in TestNG in hard Assert tutorials, so will skip to the assert present in the SoftAsssert.

assertAll :

We have SoftAssert to continue the test case execution even when few assertions are failed. assertAll is the method which makes the SoftAssert class to execute even when there are assertion failures.

You cannot write assertAll method alone in a test case, assertAll method must be used along with some other assert method.

TestNG executes the test steps only which are in between assert statement and assertAll statement, TestNG will not execute the statements which are present after the assertAll statement in Selenium Webdriver

So most of the people will use the assertAll statement as the last statement in the test case.

public class TestNgSoftAssert {
	public static void main(String[] args) {

		// set the geckodriver.exe property
		System.setProperty("webdriver.gecko.driver", "C:/Users/user/Pictures/geckodriver.exe");

		WebDriver driver = new FirefoxDriver();

		SoftAssert sa = new SoftAssert();
		// compare the titles
		sa.assertEquals(driver.getTitle(), "Google", "title is not google");
		//try to print the statement
		System.out.println("this step should execute");
		System.out.println("this step also gets executed");
		// execute all the statement before below step
		// below step will not be executed
		System.out.println("This step will not be executed");

Output of assertAll method :

Create QR Code

Custom Soft Assertions

We have so many assertions in TestNG, but sometimes we want to write our own assertion instead of using the existing TestNG assertion. In this tutorial, we learn how to write custom TestNG assertion in selenium webdriver.

Steps to Write custom Assertions :

  • Create a class called CustomException and extend SoftAssert
  • We are extending the SoftAssert class so that we can use all the methods present in that class.
  • Create a method called assertElementIsTextbox, the purpose of the method is to verify whether a given element is TextBox or not. If it is not TextBox we should fail the test case by raising the AssertionError, This method should accept the WebElement as a parameter.
  • Get the tagname and Type of the element (I am considering that if an element is textbar then it will have tagname as 'input' and type as 'text'. You can have your own consideration for your element)
  • First verify whether given Element has tagname as 'input', type as 'text'
  • If 5 is not true raise AssertionError on else block.
public class CustomAssertion extends SoftAssert{
	public void assertElementIsTextbox(WebElement element){
		// get the tag name and type attribute
		String tag = element.getTagName();
		String type = element.getAttribute("type");
		// verify whether tag is input
		if(tag.equals("input") && type.equals("text")){
			System.out.println("******Element is Textbar*******");
			throw new AssertionError("Element isn't textbar, tag, type are do not belong to textbox");

Create an object to the CustomAssertion class and call the assertElementIsTextbox method and pass the webelement as a parameter.

public class UseCustomAssertion {
	public static void main(String[] args) {

		// set the geckodriver.exe property
		System.setProperty("webdriver.gecko.driver", "C:/Users/user/Pictures/geckodriver.exe");

		WebDriver driver = new FirefoxDriver();

		WebElement searchBar = driver.findElement("q"));

		CustomAssertion ca = new CustomAssertion();

Above program is Pass scenario for Custom Assertion:

Failure scenario for custom Assertion, do change the element:

WebElement searchBar = driver.findElement(By.tagname("a"));
CustomAssertion ca = new CustomAssertion();


Create Headless Firefox Browser in Selenium webdriver

Built-in Assertions in Java

Java provides built-in assertions to verify the conditions, you must enable assertion in Your IDE to use them. If assertion are not enable then assertion will not have any effect on your code.

To enable assertion Java with Eclipse, Go to VM arguments in Run Configuration and set values as -ea, ea is a short form of enable assertions.

assert keyword is used during program development to create an assertion, which is a condition that should be true during the execution of the program.

assert condition

For example, you might have a method that should always return a positive integer value. You might test this by asserting that the return value is greater than zero using an assert statement.

At run time, if the condition is true, no other action takes place. However, if the condition is false, then an AssertionError is thrown.

Assertions are often used during testing to verify that some expected condition is actually met. They are not usually used for code which is in production.

Similar to TestNG assertions, Java assertion also will stop the execution the moment when an assertion fails.

First form of the assert in java

public class JavaAssert {
	public static void main(String[] args) {
		assert 1 > 9 ;

Second form of the assert in java

public static void main(String[] args) {
	assert 1 > 9  : "Error Message goes here";

Output of Java Assert :

Assertions are introduced in JDK 1.4 and implemented using assert keyword in java.

About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions