×

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
Selenium Assertion

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.
Selenium Assertion

 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:

Selenium Assertion

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:

Selenium Assertion

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:UsersJTPDownloadschromedriver_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:

Selenium Assertion

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:UsersJTPDownloadschromedriver_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:

Selenium Assertion

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

Selenium Assertion

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

Selenium Assertion

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

Selenium Assertion

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

Selenium Assertion

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

Selenium Assertion

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

Selenium Assertion

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.
Selenium Assertion

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:

Selenium Assertion

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.


Related Topics

Link Text Locator in Selenium IDE

LINK TEXT Link textlocator is used to identify only the weblink element based on the name of the link. Whenever multiple links are there, it will not work; it will work only...

1 minute read.

Id Locator in Selenium IDE

Id Locator Id locator is used to find the web element by the help of id of that element because ID is likely to be unique for each web element. Target Format: Id=id of the...

2 minutes read.

Selenium WebDriver- Checkbox handling

Selenium WebDriver- Checkbox handling In this tutorial, we will be learning about handling the checkbox in selenium WebDriver. Let us take one example to give you a better understanding of Checkbox handling,...

4 minutes read.

Selenium Tutorial

What is Selenium? Selenium is a functional testing tool and compatible with the non-functional testing tools also. Selenium is free and does not require any licensing. Functional tests script executed by...

6 minutes read.

Selenium-IDE Locators

Selenium-IDE Locators: Before we start learning about Selenium –IDE Locators, firstly we have to learn about UI elements. UI Elements -UI elements are the elements which are displayed on the web application....

1 minute read.

Selenium Tool Suite

The selenium tool suite is a combination of multiple software tools, and the entire tools having a different approach to support automation testing. Selenium test tool suites are as follows- Selenium...

2 minutes read.

Selenium-IDE Installation in Mozilla Firefox

A beginners guides to Selenium-IDE Installation Since Selenium IDE is designed only for Firefox and Chrome plug-in, we are assuming that you have already installed the Mozilla Firefox browser in your...

1 minute read.

Selenium-IDE Login test

Login test for Selenium-IDE Now we are learning, how to create a Login/sign-up Test case in Selenium IDE. For our testing purpose, we are going to test the login page provided by...

2 minutes read.

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...

7 minutes read.

Selenium WebDriver – Web Element Controls

Selenium WebDriver – Web Element Controls A web element is an interface that is implemented by a remote web element class. The Web element control can be used to perform the data...

1 minute read.

Data-Capture Controls Methods in Selenium WebDriver

Data-Capture Controls Methods: The data-capture controls are used to determine the text, size, location, and the CSS value of the web element, etc. Some of the most commonly used data-capture controls are...

7 minutes read.

Selenium WebDriver-Alert Popup

Selenium WebDriver-Alert Popup  An alert popup is one of the small message boxes, which used to give some information to the user that displays on the browser. The Alert popup will be...

6 minutes read.

Drawback or Limitations of Selenium

Drawback or Limitations of Selenium Followings are the Drawback or limitations of Selenium:- Drawback /LimitationsDescriptionDesktop applications cannot be automated by Selenium.To test the desktop application, we have tools like Test Complete, Winnium, and...

2 minutes read.

Selenium WebDriver - IE [Internet Explorer] browser

Selenium WebDriver - IE [Internet Explorer] browser In this segment, we will understand how to run a test script with the help of the IE (Internet Explorer) browser. It is a standalone...

4 minutes read.

CSS Selector: Attribute in Selenium IDE

CSS Selector: Attribute Locating the attribute of a CSS selector, we would access the Password textbox, which is present in the login form of Facebook. The Password textbox has a type attribute...

1 minute read.

Verification Controls Methods in Selenium WebDriver

Verification Controls Methods  The verification controls methods are used to check whether the web element is displayed, enabled, and selected. Some of the most commonly used verification controls are as follow: Verification controls Description Return...

4 minutes read.

Selenium WebDriver – Window handling

Selenium WebDriver – Window handling In this tutorial, we will understand how we can handle the window, multiple windows, and pop-ups in selenium WebDriver. We have different types of window, and pop-ups...

7 minutes read.

Selenium WebDriver - Browser Controls Commands

Selenium WebDriver - Browser Controls Commands WebDriver includes operations like opening a browser, fetch the title, and closing the browser, etc. Some of the most commonly used Browser controls commands for Selenium WebDriver...

6 minutes read.

Selenium WebDriver-CSS Selector: Tag and ID

Selenium WebDriver-CSS Selector: Tag and ID We will locate the web element with the help of tag and id CSS selector in this section of the tutorials Where Tag and id are...

4 minutes read.

Mouse and Keyboard Controls in Selenium WebDriver

Mouse and Keyboard Controls in Selenium WebDriver In this tutorial, we will learn about how to handle mouse and keyboard event using action class in selenium WebDriver. Action Class in Selenium WebDriver: Action...

13 minutes read.