Table of contents
1.
Introduction
2.
Assertions
2.1.
Type of Assertions
3.
Hard Assertions
4.
Soft Assertions
5.
Frequently Asked Questions
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Selenium Assertions

Author Aman Thakur
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Selenium's Assert and Verify methods are frequently used to verify or validate applications.We'll learn why, when, and how to use these methods to make Selenium tests more efficient in this lesson.

Assertions

To assert means to boldly or aggressively declare a truth or belief. Asserts are checkpoints or validations in Selenium. Assertions assert that the behaviour of the application is as expected. Asserts are used to validate test cases in Selenium. They assist testers in determining whether or not tests were successful.

Type of Assertions

There are two types of assertions available in Selenium
1. Hard Assertions
2. Soft Assertions(Verify Method)

Hard Assertions

Hard assertions are those in which the test execution is aborted if the assertion condition is not met. The test case has been marked as unsuccessful. If an assertion fails, the "java.lang.AssertionError" exception is thrown.
 

Let's look at some examples of different forms of hard claims:

1. assertEquals() is a method that compares actual and expected results with a minimum of two arguments. If both are true, the assertion is true, and the test case is complete. As demonstrated in the figure below, assertEquals() may compare Strings, Integers, Doubles, and a variety of other variables.

Below is an example of assertEquals().

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


public class AssertionEquals {
   public static void main(String[] args) {
       System.setProperty("webdriver.chrome.driver", "../Dependencies/chromedriver");
       WebDriver webdriver = new ChromeDriver();


       webdriver.navigate().to("https://www.codingninjas.com/");


       String ActualTitle = webdriver.getTitle();
       String ExpectedTitle = "Coding Ninjas – Learn coding online at India’s best coding institute";
       Assert.assertEquals(ExpectedTitle, ActualTitle);
   }
}
You can also try this code with Online Java Compiler
Run Code

2. assertNotEquals() function accomplishes the exact opposite of assertEquals(). The approach compares the actual and predicted results in this scenario. If the assertion condition is satisfied, but the two are not same, the assertion is false. If the actual and expected outcomes are not the same, the test case is marked as failed.

3. assertTrue(): This Assertion determines if the Boolean value of the condition is true.If the Boolean value is true, the assertion passes the test case.

import org.junit.Assert;
import static org.testng.Assert.assertTrue;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class AssertionEquals {
   @Test
   public void testAssertFunction(){
       System.setProperty("webdriver.chrome.driver", "../Dependencies/chromedriver");
       WebDriver webdriver = new ChromeDriver();


       webdriver.navigate().to("https://www.codingninjas.com/");
      
       Boolean verifyTitle = webdriver.getTitle().equalsIgnoreCase("Coding Ninjas – Learn coding online at India’s best coding institute");
       assertTrue(verifyTitle);
   }
}
You can also try this code with Online Java Compiler
Run Code

4. assertFalse() is the inverse of assertTrue().The Assertion checks if the condition's Boolean value is true. The assertion succeeds the test case if the Boolean value is false.

For more information you can check out the official documentation.

Soft Assertions

We've learned about Hard Assertion in Web Driver using the Testng framework so far.

If an assertion fails in hard assertion, the test case is aborted; otherwise, the execution continues. Even if the assertion fails, we may want to run the whole script. In Hard Assertion, this is not feasible. We must utilise a soft assertion in testng to solve this problem.

Check this out : Xpath in Selenium

Frequently Asked Questions

1. What do Assertions do?
Ans: Assertions can serve as documentation by describing the state that the code expects to find before it runs (preconditions) and the state that the code expects to produce after it runs (postconditions); they can also indicate class invariants.

2. What is difference between assert and verify?
Ans: When using the "Assert" command, the execution of that particular test method is halted as soon as the validation fails. The test method will then be recorded as failed.In the case of "Verify," however, the test method continues to run even if an assertion statement fails.

3. What will happen if assert fails?
Ans: The test is cancelled when a "assert" fails. The test will continue to run even if a "verify" fails, noting the failure. A "waitFor" command is used to wait for a condition to be met. They will fail and end the test if the condition does not become true within the current timeout setting.

4. What do Assertions do?
Ans: Assertions can serve as documentation by describing the code's state before it runs (preconditions) and the state that the code expects to produce after it runs (postconditions); they can also indicate class invariants.

Conclusion

If you have reached till here that means, you really enjoyed this article. This article covers the important selenium assertions which are commonly used in under selenium with code snippets and their use cases. And a program which is automated  by the java program.

There are few articles that you might be interested in, such as installation of selenium Interview QuestionsDifference between RTL vs Enzyme . Do upvote our blog to help other ninjas grow, and head over to our practice platform Coding Ninjas Studio to practise top problems, attempt mock tests, read interview experiences, and much more.

Happy Learning !

Live masterclass