AssertionsAssertion 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.7There are two types of Assertion:
Hard AssertionHard 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:
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:
In the above code, Assert.assertFalse() contains the condition which is returning false value. Therefore, it passes the test case. Output on the console
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 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 Output on the Console 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.
Output on the console 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.
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 javaTpoint");. Output
Output 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.
Output
Output 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.
Output
Output SoftAssertionTill 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 |