Assertions
In selenium, Assertions or Asserts are used to validate the automated test cases. This helps the testers to understand if the tests have passed or failed. If the actual result matches the expected result, then the assertion is met and if all the assertions are met then the test case is considered to be passed. Assertions in selenium can be handled by predefined methods of Junit and TestNG frameworks. In this tutorial, we are going to learn more about Assertions, different types of assertions and Assertion methods using TestNG framework. For creating assertion, we can use the Assert class provided by TestNG.
Types of Assertions
There are two types of Assertions:
1. Hard Assertion
2. Soft Assertion
Hard Assertion: In hard assertions, when the assertion condition fails, the test execution will be aborted, and the test case will be marked as fail. It will also throw the java.lang.AssertionError exception and continues with the next test in the test suite. These types of assertions are used to validate some of the critical scenarios in the application like login functionality where there is no point in proceeding further if the precondition itself fails.
Soft Assertion: Soft assertions are assertions that do not throw an exception even when the assertion fails and continue with the next step of the test execution. We need to use the class Soft Assert and the method assertAll() will be used to throw all exceptions caught during execution. Soft assertion can be used in scenarios where assertion failure does not affect the test execution.
Hard Assertion | Soft Assertion |
Test execution will be aborted when the assertion condition fails. | Test will continue even if the assertion fails. |
Does not need to invoke any method to capture the assertion. | Need to invoke assertAll() method to throw all exceptions caught during execution. |
Different types of Assertion Methods
The following are the different types of assertion methods in both hard assertion and soft assertion.
assertEquals
assertNotEquals
assertTrue
assertFalse
assertNull
assertNotNull
We are going to use assertEquals() method to understand more about hard assertion and soft assertion. We will learn more about the other assertion methods later in this tutorial.
assertEquals
assertEquals() is a method that compares the actual results with the expected results, and it can take a minimum of 2 arguments. If the actual result and expected result matches, then the assertion is considered as pass with no exception and the test case is marked as pass. If the actual and expected results do not match, then the assertion is considered as failed, it throws an assertion error, and the test case is marked as fail.
Syntax:
Assert.assertEquals(actual,expected);
Hard Assertion Example
Given below is an example of Hard Assertion using assertEquals() method. In this example we are going to verify the title of ebay.com
Here we are verifying the actual title and the expected title. If it matches, then the assertion will pass and also, we are printing the actual title and a message “The test is pass”.
In this example, the actualTitle variable will hold the title text from automation and the expectedTitle variable holds the expected string value. Assert.assertEquals() verifies if both the strings are equal. The above test case will pass and continue to the next line of execution since the actual title and the expected title are the same.
Now, for testing the negative scenario, we are going to change the expected title.
From the console, we can understand that the assertion has failed, and it threw an exception java.lang.AssertionError because the expected and the actual titles are different. We can also see that the execution is aborted at the assertion statement and the print statements are skipped.
Soft Assertion Example
We are now going to see how the same example works in case of soft assertion.
We have created an object of the SoftAssert class and comparing the actual title with the wrong expected title using assertEqual() method. At the end we are calling the method assertAll().
From the console it is clear that the assertion has failed but the execution continued to the next lines and the print statement printed the actual title and the message. The exception was thrown only after calling the assertAll() method.
If you have multiple test cases, it is preferable to have different instances of soft assert since, if all the test cases point to the same soft assert object, the assertAll() method will throw all the exceptions that are caught during execution when it is called.
So far, we have used assertEquals() method to understand the difference between hard assertion and soft assertion. Now, let’s explore the other assertion methods with example.
assertNotEquals
assertNotEquals() method is just the opposite of the assertEquals() method. If both the actual and expected results are not identical, then the assertion pass with no exception and the test case is marked as passed. If both the actual and expected results are the same, then the assertion fails with an exception and the test case is marked as failed.
Syntax:
Assert.assertNotEquals(actual,expected);
assertTrue
assertTrue() method verifies the boolean value returned by a condition. If the boolean value is true, then assertion is considered as pass, and if the boolean value is false, then assertion is considered as fail.
Syntax:
Assert.assertTrue(condition);
assertFalse
assertFalse() method is just the opposite of assertTrue() method. The Assertion verifies the Boolean value returned by the condition. If the Boolean value is false, the assertion is considered as pass.
Syntax:
Assert.assertFalse(condition);
assertNull
assertNull() verify if the provided object contains a null value or not. If the object is null, then assertion passes the test case, and if the object is not null, then assertion aborts the test case.
Syntax:
Assert.assertNull(object);
assertNotNull
assertNotNull() method is just the opposite of assertNull() method. If the object is not null, then assertion passes the test case, and if the object is null, then assertion aborts the test case.
Syntax:
Assert.assertNotNull(object);
Comments