×

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

Selenium WebDriver Commands

Selenium WebDriver Commands: As we learn previously in the Selenium-IDE tutorial, Selenium commands are the set of commands that are used to run a Selenium test script. But whereas, Selenium WebDriver had...

2 minutes read.

XPath Axes Selenium WebDriver

XPath Axes XPath axes are those axes that are used to search for the multiple nodes in the XML document from the current node context. These methods are mainly used when the...

14 minutes read.

Advantages and Disadvantages of Selenium-IDE

Advantages and Disadvantages of Selenium-IDE Advantages of Selenium IDE: UI based record and playback tool. Easy to understand & developed a test script. Selenium-IDE test script export into web driver code & Selenium–RC It is...

1 minute read.

Selenium WebDriver-XPath Locator

Selenium WebDriver-XPath Locator The locators can make the object identification of all the selenium tools. There are eight locators available in selenium web driver, but most of the time we go for...

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

Selenium WebDriver-Locators

Selenium WebDriver-Locators The WebDriver uses the same set of locating strategies like selenium -IDE for specifying the location of the web element. Each locating strategy has its command in Java to locate...

1 minute read.

Selenium WebDriver-CSS Selector: Tag and Attribute

Selenium WebDriver-CSS Selector: Tag and Attribute We will locate the web element with the help of Tag and attribute CSS selector in this section of the tutorial. Where Tag and attribute are...

5 minutes read.

Selenium vs QTP

In this section, we are comparing the most frequently used automation tool –Selenium and QTP. Selenium and QTP have relative advantages over each other- FeaturesSeleniumHP QTPLicensingSelenium is an open-source testing tool....

3 minutes read.

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.

Selenium Testing

Selenium is a functional testing tool and compatible with non–functional testing tool. Selenium is free and not required any licensing. There are specified types of testing that can be automated using Selenium:-   Functional Testing: Checking every component...

3 minutes 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- Radio button handling

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

3 minutes read.

WebDriver-Name Locator:

WebDriver-Name Locator: The name locator is used to identify any element in UI with the help of the name backend attribute. The Syntax of the name attribute is as below: driver.findElement(By.name(“value of the...

4 minutes read.

CSS Selector: ID in Selenium IDE

CSS Selector: ID The id attribute is used to identify the web element uniquely in the web pages. Syntax of ID Attribute:  [HTML tag][#][Value of ID attribute] The hash sign (#): it is mandatory while using the...

1 minute read.

CSS Selector: ID/Class and Attribute in Selenium IDE

CSS SELECTOR: ID/Class and Attribute The Id, class, and attribute of CSS selector are used to identify the web element to access the web pages.  Syntax for Id/Class and Attribute: css= <./#> {[attribute=Value of...

2 minutes read.

CSS Selector: Class in selenium IDE

CSS Selector: Class Identify a web element with the help of Class as a CSS Selector is like using id attribute. The only difference between Class and id is in their...

2 minutes read.

Name Locator in Selenium IDE

NAME LOCATOR: It is used to find the name in the web element. If we have multiple web elements of the same name, then the first matched element would be returned. Open the...

3 minutes read.

First test case of Selenium-IDE

First Test Case of Selenium-IDE: In this part, we are discussing how to create a basic test case in Selenium-IDE: The entire test script execution process in Selenium -IDE can be divided...

4 minutes read.

Difference between Selenium WebDriver and Selenium RC

Difference between Selenium WebDriver, and Selenium RC: Selenium WebDriver and selenium RC (Remote Control) both are different in any manners which we see later in this tutorial, let us see the...

3 minutes read.