top of page

Selenium Assertions

​

​

What is Assertion in Selenium ?

​

Assertion is for verifying or validating an application that compares the actual and expected results anf if it fails then execution stops.

To achieve assertion in webdriver,you need to download testNg jar file and add it to your project.

​

Assertion is of two types :

  • Hard Assertions

  • Soft Assertions

​

Methods in Assertion

The methods of assertion returns boolean value :

assertEquals(ActualOutput,ExpectedOutput);          //returns true if  matched else false

assertNotEquals(ActualOutput,ExpectedOutput);   //returns true if not matched else false

assertTrue(condition);                                                    //returns true if condition is fulfilled else false

assertFalse(condition);                                                   //returns true if condition is not fulfilled else false 

assertNull(output);                                                          //returns true if null else false

assertNotNull(output);                                                   //returns true if not null else false

​

Hard Assertions

In hard assertions, test execution is terminated if the assertion condition fails :

verifyTitle=Title.equals("Maven Repository: Search/Browse/Explore");

Assert.assertTrue(verifyTitle);

verifyTitle=Title.equals("Maven Repository:");

Assert.assertFalse(verifyTitle);

​

Title=null;

Assert.assertNull(Title);

Title="Maven Repository: Search/Browse/Explore";

Assert.assertNotNull(Title);

​

​

Hard assert in selenium =>

​

package seleniumexamples;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import graphql.Assert;

public class HardAssert {

    public static void main(String[] args) {
       // TODO Auto-generated method stub
       WebDriver driver;
       String
baseURL="https://mvnrepository.com/";
       

       //Launch the web browser
       System.setProperty("webdriver.chrome.driver","..\\SeleniumJava\\drivers\\chromedriver99.exe");
       
driver=new ChromeDriver();
       

       //To maximize the size of window
       driver.manage().window().maximize();
       

       //Navigate through an URL
       driver.get(baseURL);
       Boolean
verifyTitle;
       String
Title=driver.getTitle();
       

       //assertTrue
       verifyTitle=Title.equals("Maven Repository: Search/Browse/Explore");
       Assert.assertTrue(
verifyTitle);
       System.
out.println("Pass : Title Matched !!");
       

       //assertFalse
       verifyTitle=Title.equals("Maven Repository:");
       Assert.assertFalse(
verifyTitle);
       System.
out.println("Pass : Title is not Matched !!");
       

       //assertNull
       Title=null;
       Assert.assertNull(
Title);
       System.
out.println("Pass : Expected Title is Null !!");
       

       //assertNotNull
       Title="Maven Repository: Search/Browse/Explore";
       Assert.assertNotNull(
Title);
       System.
out.println("Pass : Expected Title is not Null !!");
       
       
driver.quit();
   }

}

​

​

Output :

​

Pass : Title Matched !!
Pass : Title is not Matched !!
Pass : Expected Title is Null !!
Pass : Expected Title is not Null !!

​

​

Soft Assertions

Soft assertion simply verifies the application and test execution does not terminate whether assertion condition fails or not.

For soft assert import following package :

import org.testng.asserts.SoftAssert;

​

Instantiate the SoftAssert class :

SoftAssert softAssert=new SoftAssert();

​

Invoke the assert methods :

verifyTitle=Title.equals("Maven Repository:");

softAssert.assertTrue(verifyTitle);

​

​

Soft assert in testNG =>

​

package seleniumTest;

import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import org.testng.annotations.BeforeClass;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;

public class SoftAssertInTestNG {
   WebDriver
driver;
  @Test
 
public void f() {
     Boolean
verifyTitle;
     String
Title=driver.getTitle();
     //Instantiate The SoftAssert class...
     SoftAssert softAssert=new SoftAssert();
     

    //Soft assertEquals
    String ExpectedTitle="Maven Repository:";
   
softAssert.assertEquals(Title, ExpectedTitle);
    System.
out.println("assertEquals FAILS : Still Executing...");
     

    //Soft assertNotEquals
    ExpectedTitle="Maven Repository: Search/Browse/Explore";
   
softAssert.assertNotEquals(Title, ExpectedTitle);
    System.
out.println("assertNotEquals FAILS : Still Executing...");
       

    //Soft assertTrue
    verifyTitle=Title.equals("Maven Repository:");
   
softAssert.assertTrue(verifyTitle);
    System.
out.println("assertTrue FAILS : Still Executing...");
    
 
  //Soft assertFalse
    verifyTitle=Title.equals("Maven Repository: Search/Browse/Explore");
   
softAssert.assertFalse(verifyTitle);
    System.
out.println("asserFalse FAILS : Still Executing...");
    

    //Soft assertNull
    Title="Maven Repository: Search/Browse/Explore";
   
softAssert.assertNull(Title);
    System.
out.println("assertNull FAILS : Still Executing...");
           

    //Soft assertNotNull
    Title=null;
   
softAssert.assertNotNull(Title);
    System.
out.println("assertNotNull FAILS : Still Executing...");
   
    System.
out.println("All the Soft Asserts Failed but Test Execution does not Terminate !!");

  }
  @BeforeClass
 
public void beforeClass() {
      String
baseURL="https://mvnrepository.com/";
       //Launch the web browser
       System.setProperty("webdriver.chrome.driver","..\\SeleniumJava\\drivers\\chromedriver99.exe");
       
driver=new ChromeDriver();
       
//To maximize the size of window
       driver.manage().window().maximize();
       
//Navigate through an URL
       driver.get(baseURL);
  }

  @AfterClass
 
public void afterClass() {
     
driver.quit();
  }

}

​

 

Output :

 

assertEquals FAILS : Still Executing...
assertNotEquals FAILS : Still Executing...
assertTrue FAILS : Still Executing...
asserFalse FAILS : Still Executing...
assertNull FAILS : Still Executing...
assertNotNull FAILS : Still Executing...
All the Soft Asserts Failed but Test Execution does not Terminate !!

​

​

bottom of page