Home » Selenium Assertions

Selenium Assertions

by Online Tutorials Library

Assertions

Assertion determines the state of the application whether it is the same what we are expecting or not. If the assertion fails, then the test case is failed and stops the execution.

To use the Assertion in Web Driver, you need to download the Testng jar file and add it to the eclipse. Download the Testng jar file from the link given below:

https://mvnrepository.com/artifact/org.testng/testng/6.7

There are two types of Assertion:

  • Hard Assertion
  • Soft Assertion

Assertions

Hard Assertion

Hard Assertion is an Assertion that throws the AssertException when the test case is failed. In the case of Hard Assertion, you can handle the error by using a catch block like a java exception. Suppose we have two test cases in a suite. The first test case in a suite has an assertion that fails, and if we want to run the second case in a suit, then we need to handle the assertion error. A Hard Assertion contains the following methods:

  • AssertEquals
  • AssertNotEquals
  • AssertTrue
  • AssertFalse
  • AssertNull
  • AssertNotNull

AssertFalse()

Assertion verifies the boolean value returned by a condition. If the boolean value is false, then assertion passes the test case, and if the boolean value is true, then assertion aborts the test case by an exception. Syntax of AssertFalse() method is given below:

Let’s understand through an example:

  • When the condition is false

In the above code, Assert.assertFalse() contains the condition which is returning false value. Therefore, it passes the test case.

Output on the console

Assertions

  • When the condition is true

In the above code, Assert.assertFalse() method contains the true condition. Therefore, the assertion fails which means that the test case is also failed. Assertion failure will stop the execution by exception.

Output on the console

Assertions

AssertTrue()

Assertion verifies the boolean value returned by a condition. If the boolean value is true, then assertion passes the test case, and if the boolean value is false, then assertion aborts the test case by an exception. Syntax of AssertTrue() method is given below:

Let’s understand through an example.

In the above code, driver.findElement(By.cssSelector(“input[id*=’SeniorCitizenDiscount’]”)).click(); This statement is used to select the ‘Senior Citizen’ box. In the next statement, we are applying assertion to check whether the test case fails or pass. The parameter inside the Assert.assertTrue() method is returning true value, therefore the test case pass.

Output

Assertions

Output on the Console

Assertions

AssertEquals()

AssertEquals() is a method used to compare the actual and expected results. If both the actual and expected results are same, then the assertion pass with no exception and the test case is marked as “passed”. If both the actual and expected results are not the same, then the assertion fails with an exception and the test case is marked as “failed”. Syntax of an AssertEquals() method is given below:

Let’s understand through an example.

  • When a number of adults is 5.

Assertions
Assertions

  • When the number of adults is not equal to 5

Assertions

Output on the console

Assertions

AssertNotEquals()

It is opposite to the function of AssertNotEquals() method. AssertNotEquals() is a method used to compare the actual and expected results. If both the actual and expected results are not the same, then the assertion pass with no exception and the test case is marked as “passed”. If both the actual and expected results are same, then the assertion fails with an exception and the test case is marked as “failed”. Syntax of an AssertNotEquals() method is given below:

Let’s understand through an example.

  • When actual string is not equal to the expected string.

In the above code, actual string, i.e., Hello is not equal to the expected string, i.e., How are you. Therefore, the assertion passes the test case. This will execute the next statement and the next statement is System.out.println(“Hello…This is tutoraspire”);.

Output

Assertions

  • When the actual string is equal to the expected string.

Output

Assertions

AssertNull()

AssertNull() is a method that verifies whether the object is null or not. If an object is null, then assertion passes the test case, and the test case is marked as “passed”, and if an object is not null, then assertion aborts the test case and the test case is marked as “failed”. Syntax of AssertNull() method is given below:

Let’s understand through an example.

  • When an object is null.

Output

Assertions

  • When an object is not equal to null.

Output

Assertions

AssertNotNull()

AssertNotNull() is a method that verifies whether the object is null or not. If an object is not null, then assertion passes the test case and test case is marked as “passed”, and if an object is null, then assertion aborts the test case and test case is marked as “failed”. Syntax of AssertNotNull() method is given below:

Let’s understand through an example.

  • When an object is not null.

Output

Assertions

  • When an object is null.

Output

Assertions

SoftAssertion

Till now, we have learnt about the Hard Assertion in Web Driver using Testng framework. In hard assertion, if an assertion fails then it aborts the test case otherwise it continues the execution. Sometimes we want to execute the whole script even if the assertion fails. This is not possible in Hard Assertion. To overcome this problem, we need to use a soft assertion in testng.

Next TopicSelenium Grid

You may also like