Assert class is provided by TestNG and not by Selenium. Assertion in selenium are used to perform validation checks in test cases, which helps us to decide if the test case is passed or failed.
There are two types if asserts.
1. HardAsserts (or simply Assert )
HardAssert – throws AssertException immediately, test is marked as failed and test suited continues with next test cases. If hard Assert fails, other assertions in the test case will not be executed and test fails immediately.
If there are multiple assert statements in the test, and suppose your first assert fails, then in case of Hard Assert (or simply termed as assert), test case will fail immediate after AssertException occurs. If does not continues the processing for next statements. Stops the execution of test case there itself.
The disadvantage of HardAssert is, it stops the execution of test after assertion error has occurred.
2. SoftAssert
To overcome the disadvantage that HardAssert has (it stops the execution of test after assertion error has occurred), a customized error handler is provided by TestNG, called SoftAssert.
SoftAssert – collects errors during @Test (no exception is thrown) and if you call org.testng.asserts.SoftAssert#assertAll at the end of @Testexception is thrown if there was any and test suite again continue with next @Test
If there are multiple assert in test case, and suppose your first assert fails, then in case of SoftAssert, test case will not fail immediately after assertion error has occurred. It continuous the processing for next statements. If will go till the end of the test even though multiple assertion error occurs. Finally we need to use assertAll(); method of SoftAssert, which will check if any of the assert exception has occurred, if any, then it will fail the test case (but after executing all the statements in the test).
Note: make sure that you use assertAll(); method at last. If you don’t use it, your test case will pass even if multiple assert errors are in test.
Below example shows the difference between softAssert & Hard Assert
Example –
package samplePackage;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class Asserts {
@Test
public void hardAssertTest(){
System.out.println("------------OutPut for HardAssert --------------------------");
System.out.println("I am Before HardAssert statment");
//failing the assert, so that it will not execute any statment after assert fails (in case of assert (soft assert))
Assert.assertTrue(false);
System.out.println("I am After HardAssert statment");
}
@Test
public void softAssertTest(){
System.out.println("------------OutPut for SoftAssert --------------------------");
//In case of SoftAssert, we need to create object of it, & then use that object (unlike normal assert)
SoftAssert a = new SoftAssert();
System.out.println("I am Before SoftAssert statment");
//Failing assert so that assertion error should occur
a.assertTrue(false);
System.out.println("I am After SoftAssert statment1");
a.assertTrue(false);
System.out.println("I am After SoftAssert statment2");
a.assertTrue(false);
System.out.println("I am After SoftAssert statment3");
}
}
Observe the below console output,
In case of HardAssert, if assertion error occurs, it has stopped execution
In case of SoftAssert, even if assertion error occurs, it continued execution till end, & lastly it failed test.
Console Output:
[TestNG] Running:
C:UsersPrakashAppDataLocalTemptestng-eclipse-1927411577testng-customsuite.xml
————OutPut for HardAssert ————————–
I am Before HardAssert statment
————OutPut for SoftAssert ————————–
I am Before SoftAssert statment
I am After SoftAssert statment1
I am After SoftAssert statment2
I am After SoftAssert statment3
FAILED: hardAssertTest
java.lang.AssertionError: expected [true] but found [false]
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:496)
at org.testng.Assert.assertTrue(Assert.java:42)
at org.testng.Assert.assertTrue(Assert.java:52)
at samplePackage.Asserts.hardAssertTest(Asserts.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:821)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1131)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:773)
at org.testng.TestRunner.run(TestRunner.java:623)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
at org.testng.TestNG.run(TestNG.java:1018)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
FAILED: softAssertTest
java.lang.AssertionError: The following asserts failed:
expected [true] but found [false],
expected [true] but found [false],
expected [true] but found [false]
at org.testng.asserts.SoftAssert.assertAll(SoftAssert.java:48)
at samplePackage.Asserts.softAssertTest(Asserts.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:821)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1131)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:773)
at org.testng.TestRunner.run(TestRunner.java:623)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:357)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310)
at org.testng.SuiteRunner.run(SuiteRunner.java:259)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
at org.testng.TestNG.run(TestNG.java:1018)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
===============================================
Default test
Tests run: 2, Failures: 2, Skips: 0
===============================================
Hope this helps !!!!