Asserts and Verify methods are commonly used in Selenium for verifying or validating applications. In this tutorial, we will learn the why, when, and how of using these methods to make Selenium tests more efficient.

Assertions

In Selenium, Asserts are validations or checkpoints for an application. Assertions state confidently that application behaviour is working as expected. One can say that Asserts in Selenium are used to validate the test cases. They help testers understand if tests have passed or failed.

Types of Assertions

  1. Hard Assertions.
  2. Soft Assertions.

Hard Assertions

Hard Assertions are ones in which test execution is aborted if the test does not meet the assertion condition. The test case is marked as failed. In case of an assertion error, it will throw the “java.lang.AssertionError” exception.

Let us explore different types of hard assertions with examples.

assertEquals() is a method that takes a minimum of 2 arguments and compares actual results with expected results. If both match, then the assertion is passed and the test case is marked as passed. assertEquals() can compare Strings, Integers, Doubles and many more variables.

  • package tests;

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

    public class Asserts {
        public static void main(String[] args) {
            System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            //code to maximize chrome browser
            driver.manage().window().maximize();

            String baseURL = "https://rahulshettyacademy.com/AutomationPractice/";
            driver.get(baseURL);

            String actualTitle = driver.getTitle();
            String expectedTitle = "Practice Page";

            Assert.assertEquals(actualTitle,expectedTitle);

            //code to close chrome driver.
            driver.close();

        }
    }
  • assertNotEquals() is a method that does the opposite of the assertEquals() method. In this case, the method compares the actual and expected result. But if the assertion condition is met if the two are not identical. If actual and expected results are not the same, the test case is marked as passed.

  • package tests;

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

    public class Asserts {
        public static void main(String[] args) {
            System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            //code to maximize chrome browser
            driver.manage().window().maximize();

            String baseURL = "https://rahulshettyacademy.com/AutomationPractice/";
            driver.get(baseURL);

            String actualTitle = driver.getTitle();
            String expectedTitle = "Practice Page demo";

            Assert.assertNotEquals(actualTitle,expectedTitle);

            //code to close chrome driver.
            driver.close();

        }
    }
  • assertTrue(): This Assertion verifies the Boolean value returned by the condition. If the Boolean value is true, then the assertion passes the test case.

    package tests;

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

    public class Asserts {
        public static void main(String[] args) {
            System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            //code to maximize chrome browser
            driver.manage().window().maximize();

            String baseURL = "https://rahulshettyacademy.com/AutomationPractice/";
            driver.get(baseURL);

            boolean verifyTitle = driver.getTitle().equalsIgnoreCase("Practice Page");

            Assert.assertTrue(verifyTitle);

            //code to close chrome driver.
            driver.close();

        }
    }

    assertFalse(): This method works opposite of that of assertTrue(). The Assertion verifies the Boolean value returned by the condition. If the Boolean value is false, then the assertion passes the test case.

  • package tests;

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

    public class Asserts {
        public static void main(String[] args) {
            System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            //code to maximize chrome browser
            driver.manage().window().maximize();

            String baseURL = "https://rahulshettyacademy.com/AutomationPractice/";
            driver.get(baseURL);

            boolean verifyTitle = driver.getTitle().equalsIgnoreCase("Practice Page");

            Assert.assertFalse(verifyTitle);

            //code to close chrome driver.
            driver.close();

        }
    }
  • assertNull(): This method verifies if the expected output is null. If not, the value returned is false.

    assertNotNull(): This method works opposite to that of the assertNull() method. The assertion condition is met when the method validates the expected output to be not null.

  • package tests;

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

    public class Asserts {
        public static void main(String[] args) {
            System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            //code to maximize chrome browser
            driver.manage().window().maximize();

            String baseURL = "https://rahulshettyacademy.com/AutomationPractice/";
            driver.get(baseURL);

            boolean verifyTitle = driver.getTitle().equalsIgnoreCase("Practice Page");

            Assert.assertNotNull(verifyTitle);

            //code to close chrome driver.
            driver.close();

        }
    }
  • Verify in Selenium (also known as Soft Assertion)

    In a hard assertion, when the assertion fails, it terminates or aborts the test. If the tester does not want to terminate the script they cannot use hard assertions. To overcome this, one can use soft assertions.

    Let us now go through different types of soft assertion with example.

    package tests;

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.asserts.SoftAssert;

    public class SoftAsserts {
        public static void main(String[] args) {
            System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            //code to maximize chrome browser
            driver.manage().window().maximize();

            String baseURL = "https://rahulshettyacademy.com/AutomationPractice/";
            driver.get(baseURL);

            SoftAssert softAssert = new SoftAssert();
            String getActualTitle = driver.getTitle();
            boolean verifyTitle = driver.getTitle().equalsIgnoreCase("Practice Page");

            softAssert.assertEquals(getActualTitle, "Practice Page");
            softAssert.assertNotEquals(getActualTitle, "Practice Page");
            softAssert.assertNull(verifyTitle);
            softAssert.assertNotNull(verifyTitle);
            softAssert.assertAll();

            //code to close chrome driver
            driver.close();
        }
    }

    Hard vs Soft Asserts in Selenium

    Hard Assertions

    Soft Assertions

    Test Execution will be aborted if assert condition is not met

    Test execution will continue till the end of the test case even if assert condition is not met

    Does not have to invoke any methods to capture the assertions

    To view assertions result at the end of the test, tester has to invoke assertAll()


    Difference between Assert and Verify in selenium

    In the case of assertions, if the assert condition is not met, test case execution will be aborted. The remaining tests are skipped, and the test case is marked as failed. These assertions are used as checkpoints for testing or validating business-critical transactions.

    In case of verify, tests will continue to run until the last test is executed even if assert conditions are not met. Verify or Soft Asserts will report the errors at the end of the test. Simply put, tests will not be aborted if any condition is not met. Testers need to invoke the assertAll() method to view the results.

    Both Hard and Soft Assertions are very important for designing and running Selenium WebDriver tests. They are instrumental in verifying application behavior at critical stages. By using assertions, testing teams can determine if an application is working as it is expected to.


    Tags


    You may also like

    Groups attribute in TestNG 

    Groups attribute in TestNG 
    Leave a Reply

    Your email address will not be published. Required fields are marked

    {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}