Asserts are commonly used in Selenium for verifying or validating applications. In this tutorial, we will learn about assert methods to make selenium testing more efficient.
Assertions state confidently that application behavior is working as expected. The assertion is considered to be met if the actual result of an application matches with that of the expected result.
A test case is considered to be passed only if all the assertions have been met. Assertions in Selenium can be handled by pre-defined methods of Junit and TestNG frameworks, which will be explained in detail below.
Types Of Assertions In Selenium
Hard Assertion – Here, test execution will be aborted if the assert condition is not met. A hard assert throw AssertException immediately after a test fails and the test is marked as failed. Perhaps test suite continues with next @Test annotation.
Soft Assertion – In this assertion, the test execution will continue till the end of the test case even if the assert condition is not met. Soft Asserts will report the errors at the end of the test. Here, tests will not be aborted if any condition is not met. Testers need to invoke the assertAll() method to view the results.
Different Types of Hard Assertion Methods
TestNG Assert methods will be the same as the Junit assertion methods. TestNG provides more advanced assertion handling techniques such as dependent classes, Group tests, Parameterized tests, etc. The various types of TestNG Hard Assert methods are explained below in detail:
assertEquals
assertNotEquals
assertTrue
assertFalse
assertNull
assertNotNull
#1) assertEquals
assertEquals() is a method that takes a lot of two arguments and compares actual results with the expected results. If both match, the assertion is passed, and the test case is marked as passed.
assertEquals() can compare Strings, Integers, Doubles, and many more variables. But, if the actual and expected values are not just same, the assert fails with an exception and the test is marked as “failed”. The syntax is given below:
assertEquals(actual,expected);
Parameters:
Actual – The actual value that we expect from automation. Expected –The expected value.
Example for assertEquals() : To verify the title of the webpage which meets the expected value
“About | Numpy Ninja”.
In the above example, two strings were verified for equal values.
Console :
The title of webpage is: About | Numpy Ninja, Assert passed.
PASSED: testAssertEqualsFunction
#2) assertNotEquals
assertNotEquals is just opposite to the functioning of assertEquals assertion. In this case, the method compares the actual and expected result. The test case is marked as passed only if actual and expected results are not identical. But, if the actual and expected values are just same, the assert throws an exception and the test is marked as “failed”. The syntax is given below:
assertNotEquals(actual,expected);
Parameters:
Actual – The actual value that we expect from automation. Expected –The expected value.
Example for assertNotEquals() : To verify the title of the webpage unalike.
In the above example, two strings were verified for unequal values.
Console :
Assert passed.
PASSED: testAssertNotEqualsFunction
#3) assertTrue
This assertion method verifies the Boolean value returned by the condition. If the Boolean value is true, then the assertion passes the test case. If the condition is false/fails, this assertion skips the current method from execution. The syntax is given below:
assertTrue(condition);
Parameters: BooleanCondition – Condition to check for its return type to be True.
Example for assertTrue() : To verify if a button is present on the webpage.
In the above example, it verifies if the Boolean condition –LoginButton.IsDisplayed() returns TRUE.
Console :
Button is displayed, Assert passed.
PASSED: testAssertTrueFunction
#4) assertFalse
This method is basically opposite to assertTrue. The assertFalse method checks the Boolean value returned by a condition is false or not. When a condition value is true, the assertion aborts the method by an exception. The syntax is given below:
assertFalse(condition);
Parameters: BooleanCondition – Condition to check for its return type to be False.
Example for assertFalse() :
In the above example, it verifies if the Boolean condition –returns FALSE. But, the condition returned is true, so it fails the test case and throws an exception error as java.lang.AssertionError.
Console :
FAILED: testAssertFalseFunction
java.lang.AssertionError: expected [false] but found [true]
#5) assertNull
This assertion method checks for an object, if it has a null return value. When an object is Null, the assertion is passed without any exception. The syntax is as follows:
assertNull(object);
Parameters: Object – Any data value which holds a null value.
Example for assertNull() : Assert if a string is null.
Console :
String holds null value – Assert passed.
PASSED: testAssertNullFunction
#6) assertNotNull
This assertNotNull method is vice-versa of assertNull. The assertion condition is met when the method validates the expected output to be not null. The syntax is given below:
assertNotNull(object);
Parameters: Object – Any variable which holds a value.
Example for assertNotNull() : Assert if a string is not null.
Console :
Driver is not null – Assert passed.
PASSED: testAssertNotNullFunction
Soft Assertion In WebDriver Using TestNG
Till now, we have learnt about the hard assertions in WebDriver using testNG framework. In hard assertion, when the assertion fails it terminates/aborts the test (method). If the tester does not want to terminate the script, they cannot use hard assertions. To overcome this, one can use soft assertions.
Soft Assertions are the type of assertions that do not throw an exception automatically when an assertion fails unless it is asked for. This is useful if you are doing multiple validations in a form, out of which only a few validations directly have an impact on deciding the test case status.
Here, we use a class called SoftAssert and the method assertAll() is called to throw all exceptions caught during execution. When softAssert is used, it performs assertion and if an exception is found, its not thrown immediately, rather it continues until we call the method assertAll() to throw all exceptions caught.
In the below example, two objects of ‘SoftAssert’ class are created to be used in two different test cases.
Console:
Actual Title :About | Numpy Ninja
Assertion 1 is executed
Button is displayed, Assert passed
Assertion 2 is executed
PASSED: assertions.softassertion.verifyElement
FAILED: assertions.softassertion.verifyTitle
java.lang.AssertionError: The following asserts failed:
expected [Numpy Ninja] but found [About | Numpy Ninja]
From the console, we can understand that even though the assertion was a failure in the first testcase (verifyTitle), the execution continued to the next line wherein the statement – "Assertion 1 is executed" was printed and only after softAssert was called, the exception was thrown.
Conclusion
As discussed above, we have seen various Assertion methods which can be used for the test case validation and ensure whether the test has a Pass or Fail status.
Thanks for Reading….