Selenium Assertion: In this section, we will learn about the Assertion in Selenium WebDriver.

An Assertion is a feature available in TestNG, which is used to verify the expected result of the test case. If the actual result of an application matches the expected result, then only we can use the assertion.

Assertions can be handled with the help of the predefined method of Junit and TestNG frameworks.

Type of Assertion

There are two types of assertion available in selenium WebDriver:

  • Soft Assert
  • Hard Assert
Selenium Assertion

To use the Assertion in WebDriver, we need to download the TestNG and add it to the eclipse.

To download the TestNG, refer to the below link.

http://dl.bintray.com/testng-team/testng-eclipse-release/zipped/

Below packages are used while using assertion in the selenium WebDriver:

import org.testng.AssertJUnit;

import org.testng.asserts.SoftAssert;

Hard Assert

The Hard assertion is used when the test case got failed and threw an AssertException.

  • All the method available in hard assert is static.
  • Whenever the hard assert() method fails, TestNG generates the assert error message exception along with line under, and error traces (information), then stop the current test script execution and continue the implementation with the remaining test script.
  • We go for the hard assert to verify the mandatory information or crucial information.
Selenium Assertion

 Following methods are available in assert class of hard assert:

  • assertEquals()
  • assertNotSame()
  • assertTrue()
  • assertFalse()
  • assertNull()
  • assertNotNull()

assertEquals()

The assertEquals() method is used to match the actual and expected results.

  • If both the expected and actual result does not match, it will throw an AssertionError and terminate the execution.
  • And if both actual and excepted results are matched or the same, the test case will be passed.

Syntax for assertEquals()

AssertJUnit.assertEquals(String actual,String expected);  

For example:

Where actual and expected string value are equal

The output of the above code is as below:

when actual and expected string value are not equal

The output of the above code is as follows:

Selenium Assertion

assertNotSame()

The assertNotSame() method is used to match the actual and excepted result.

  • The test case will be passed with no exception if both an actual and the expected results are not similar.
  • The test case will be failed if both the actual and expected results arethe same.

Syntax for assertNotSame() method

AssertJUnit.assertNotSame(actual,expected,message); 

For example:

When actual string is not similar to the expected string

In the above code, actual string value is “hey” which is not equal to the expected string value “what’s up”. So, the assertion pass the test case and perform the next statement which is System.out.println(“Hello…..cool”)

The output of the above code:

When the actual string is same as the expected string

The output of the above code:

Selenium Assertion

assertFalse()

The assertFalse() method is used to take one Boolean argument, and check if the condition is false; it will pass the test case. Else, it will throw an AssertionError if the condition is true.

Syntax for assertFalse() method

AssertJUnit.assertFalse(condition);  

For example

When condition is false

In the above code, the assertFalse() methodhaving the condition, which will return the false value. So, the test case is passed.

As we can see in the below output console window:

Selenium Assertion

When the condition is true

In the above code, the assertFalse() method is having the true condition. So, the assertion got failed, which implies that the test case is also got failed.

 And it will stop the execution and give the AssertionError, which is as shown below:

Selenium Assertion

assertTrue()

The assertTrue() method is used to take the one Boolean argument and checks that a condition is true, then assertion will pass the test case; If the condition is not true, it will throw an AssertionError.

Syntax for assertTrue() method

AssertJUnit.assertTrue(condition);  

For example:

When the condition is true

Output

Selenium Assertion

When the condition is false

Output

Selenium Assertion

assertNull()

The assertNull() is used to check if the given object is having a null value or not.

  • If an object has a null value, it will pass the test case.
  • Otherwise, the test case will get failed and throw an AssertionError.

Syntax for assertNull() method

AssertJUnit.assertNull(object); 

Let see one example, where

The value of the object is null

Output

Selenium Assertion

When the value of the object is not null

Output

Selenium Assertion

assertNotNull()

An assertNotNull() method is used to check whether the object is null or not.

  • If the object is not null, it will pass the test case.
  • Otherwise, the test case will be failed and throws the AssertionError.

Syntax for assertNotNull() method

AssertJUnit.assertNotNull(object);  

For example

The object is not null


Output

Selenium Assertion

When the object is null

Output

Selenium Assertion

Soft assertion

A soft assertion is used when the assertion condition is not matched.

Mostly, the soft assertion does not throw any error, but when the assertion condition fails, it continues with the next step of the test case.

  • All the methods available in soft assert is non-static method.
  • Whenever softassert() failed, TestNG generates error message along with assertion exception, then continue the same test script execution.
  • We go for soft assert to verify non-mandatory fields.
  • In the case of soft assert, assertAll() should be used at the end of the test scripts because it always collects all the log traces and displays on the console.
Selenium Assertion

Following method available in assert class of soft assert:

  • assertEquls()
  • assertNotEquals()
  • assertTrue()
  • assertFalse()
  • assertAll()

Let us see one example program where we will understand the concept of Soft assert:

In the above code, we are using reporter class, which is used for test methods to log the message that will be included in the Html reports generated by the TestNG.

The output of the above example:

Selenium Assertion

In the above output, we are using the assertAll() method, which is used to capture the error message and displayed it to the console window.

Pin It on Pinterest

Share This