Selenium Assertion
Selenium Assertion: In this section, we will learn about the Assertion in Selenium WebDriver.
An Assertion is a feature available in TestNG, which is used to verify the expected result of the test case. If the actual result of an application matches the expected result, then only we can use the assertion.
Assertions can be handled with the help of the predefined method of Junit and TestNG frameworks.
Type of Assertion
There are two types of assertion available in selenium WebDriver:
- Soft Assert
- Hard Assert

To use the Assertion in WebDriver, we need to download the TestNG and add it to the eclipse.
To download the TestNG, refer to the below link.
http://dl.bintray.com/testng-team/testng-eclipse-release/zipped/
Below packages are used while using assertion in the selenium WebDriver:
import org.testng.AssertJUnit;
import org.testng.asserts.SoftAssert;
Hard Assert
The Hard assertion is used when the test case got failed and threw an AssertException.
- All the method available in hard assert is static.
- Whenever the hard assert() method fails, TestNG generates the assert error message exception along with line under, and error traces (information), then stop the current test script execution and continue the implementation with the remaining test script.
- We go for the hard assert to verify the mandatory information or crucial information.

Following methods are available in assert class of hard assert:
- assertEquals()
- assertNotSame()
- assertTrue()
- assertFalse()
- assertNull()
- assertNotNull()
assertEquals()
The assertEquals() method is used to match the actual and expected results.
- If both the expected and actual result does not match, it will throw an AssertionError and terminate the execution.
- And if both actual and excepted results are matched or the same, the test case will be passed.
Syntax for assertEquals()
AssertJUnit.assertEquals(String actual,String expected);
For example:
Where actual and expected string value are equal
package testpackage; import org.testng.AssertJUnit; public class Assert_4 { public static void main(String[] args) { AssertJUnit.assertEquals("tutorial", "tutorial"); System.out.println("Selenium WebDriver"); } }
The output of the above code is as below:

when actual and expected string value are not equal
package testpackage; import org.testng.AssertJUnit; public class Assert_4 { public static void main(String[] args) { AssertJUnit.assertEquals("tutorial", "example"); System.out.println("tutorialandExample"); } }
The output of the above code is as follows:

assertNotSame()
The assertNotSame() method is used to match the actual and excepted result.
- The test case will be passed with no exception if both an actual and the expected results are not similar.
- The test case will be failed if both the actual and expected results arethe same.
Syntax for assertNotSame() method
AssertJUnit.assertNotSame(actual,expected,message);
For example:
When actual string is not similar to the expected string
package testpackage; import org.testng.AssertJUnit; public class Assert_4 { public static void main(String[] args) { AssertJUnit.assertNotSame("Hey", "what's up"); System.out.println("Hello.....cool"); } }
In the above code, actual string value is “hey” which is not equal to the expected string value “what’s up”. So, the assertion pass the test case and perform the next statement which is System.out.println("Hello.....cool")
The output of the above code:

When the actual string is same as the expected string
package testpackage; import org.testng.AssertJUnit; public class Assert_4 { public static void main(String[] args) { AssertJUnit.assertNotSame("Hey", “Hey"); System.out.println("Hello.....cool"); } }
The output of the above code:

assertFalse()
The assertFalse() method is used to take one Boolean argument, and check if the condition is false; it will pass the test case. Else, it will throw an AssertionError if the condition is true.
Syntax for assertFalse() method
AssertJUnit.assertFalse(condition);
For example
When condition is false
package testpackage; import org.testng.AssertJUnit; import static org.testng.AssertJUnit.assertFalse; import org.openqa.selenium.By; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Assert_1 { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\\Users\\JTP\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe"); // Create driver object for CHROME browser WebDriver driver = new ChromeDriver(); //maximize the window size driver.manage().window().maximize(); driver.manage().timeouts().pageLoadTimeout(20,TimeUnit.SECONDS); driver.manage().deleteAllCookies(); //navigate to the url driver.navigate().to("https://www.irctc.co.in/nget/train-search"); //using hard assert method, when the condition is false AssertJUnit.assertFalse(driver.findElement(By.xpath("//label[contains(text(),'Flexible With Date')]")).isSelected()); System.out.println(driver.findElement(By.xpath("//label[contains(text(),'Flexible With Date')]")).isSelected()); //closing the driver driver.close(); } }
In the above code, the assertFalse() methodhaving the condition, which will return the false value. So, the test case is passed.
As we can see in the below output console window:

When the condition is true
package testpackage; import org.testng.AssertJUnit; import static org.testng.AssertJUnit.assertFalse; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class Assert_1 { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\\Users\\JTP\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe"); // create driver object for CHROME browser WebDriver driver = new ChromeDriver(); //maximize the window size driver.manage().window().maximize(); driver.manage().timeouts().pageLoadTimeout(20,TimeUnit.SECONDS); driver.manage().deleteAllCookies(); //navigate to the url driver.navigate().to("https://www.irctc.co.in/nget/train-search"); //when the condition is true AssertJUnit.assertFalse(true); System.out.println(driver.findElement(By.xpath("//label[contains(text(),'Flexible With Date')]")).isSelected()); //closing the driver driver.close(); } }
In the above code, the assertFalse() method is having the true condition. So, the assertion got failed, which implies that the test case is also got failed.
And it will stop the execution and give the AssertionError, which is as shown below:

assertTrue()
The assertTrue() method is used to take the one Boolean argument and checks that a condition is true, then assertion will pass the test case; If the condition is not true, it will throw an AssertionError.
Syntax for assertTrue() method
AssertJUnit.assertTrue(condition);
For example:
When the condition is true
package testpackage; import org.testng.AssertJUnit; public class Assert_5 { public static void main(String[] args){ AssertJUnit.assertTrue(true); System.out.println("selenium"); } }
Output

When the condition is false
package testpackage; import org.testng.AssertJUnit; public class Assert_5 { public static void main(String[] args){ AssertJUnit.assertTrue(false); System.out.println("selenium"); } }
Output

assertNull()
The assertNull() is used to check if the given object is having a null value or not.
- If an object has a null value, it will pass the test case.
- Otherwise, the test case will get failed and throw an AssertionError.
Syntax for assertNull() method
AssertJUnit.assertNull(object);
Let see one example, where
The value of the object is null
package testpackage; import org.testng.AssertJUnit; public class Assert_4 { public static void main(String[] args) { AssertJUnit.assertNull(null); System.out.println("Hello world!!!!"); } }
Output

When the value of the object is not null
package testpackage; import org.testng.AssertJUnit; public class Assert_4 { public static void main(String[] args) { AssertJUnit.assertNull(5); System.out.println("Hello world!!!!"); }
Output

assertNotNull()
An assertNotNull() method is used to check whether the object is null or not.
- If the object is not null, it will pass the test case.
- Otherwise, the test case will be failed and throws the AssertionError.
Syntax for assertNotNull() method
AssertJUnit.assertNotNull(object);
For example
The object is not null
package testpackage; import org.testng.AssertJUnit; public class Assert_5 { public static void main(String[] args) { AssertJUnit.assertNotNull(5); System.out.println("tutorialandExample"); } }
Output

When the object is null
package testpackage; import org.testng.AssertJUnit; public class Assert_5 { public static void main(String[] args) { AssertJUnit.assertNotNull(null); System.out.println("TutorialandExample"); } }
Output

Soft assertion
A soft assertion is used when the assertion condition is not matched.
Mostly, the soft assertion does not throw any error, but when the assertion condition fails, it continues with the next step of the test case.
- All the methods available in soft assert is non-static method.
- Whenever softassert() failed, TestNG generates error message along with assertion exception, then continue the same test script execution.
- We go for soft assert to verify non-mandatory fields.
- In the case of soft assert, assertAll() should be used at the end of the test scripts because it always collects all the log traces and displays on the console.

Following method available in assert class of soft assert:
- assertEquls()
- assertNotEquals()
- assertTrue()
- assertFalse()
- assertAll()
Let us see one example program where we will understand the concept of Soft assert:
package testpackage; import org.testng.Reporter; import org.testng.asserts.SoftAssert; public class Assert_4 { public static void main(String[] args) { //creating the soft assert object SoftAssert sa=new SoftAssert(); Reporter.log("step-1",true); Reporter.log("step-2",true); sa.assertEquals("A", "B"); Reporter.log("step-3",true); sa.assertEquals("B", "Y"); Reporter.log("step-4",true); //mandatory used in soft assert sa.assertAll(); } }
In the above code, we are using reporter class, which is used for test methods to log the message that will be included in the Html reports generated by the TestNG.
The output of the above example:

In the above output, we are using the assertAll() method, which is used to capture the error message and displayed it to the console window.