Assertion in Selenium WebDriver using TestNg
Last Updated : 06 Oct, 2021
Prerequisite - Selenium
Actual result is compared with expected result with the help of Assertion. It means verification is done to check if the state of the application is the same to what we are expecting or not. For creating assertion we are going to use the Assert class provided by TestNG.
There are two types of Assertion:-
- Hard Assertions.
- Soft assertions.
These are explained as following below.
1. Hard Assertions :
When any assert statement fails this type of assertion throws an exception immediately and continues with the next test in the test suite.
Hard Assertion can be of following types:-
1. assertEquals -
This is used to compare expected and actual values in the selenium webdriver. The assertion passes with no exception whenever the expected and actual values are same. But, if the actual and expected values are not same then assert fails with an exception and the test is marked as failed.
Syntax :
Assert.assertEquals(actual, expected);
2. assertNotEquals -
This is just the opposite of assertEquals. The assertion passes with no exception whenever the expected and actual values are not same. But, if the actual and expected values are same then assert fails with an exception and the test is marked as failed.
Syntax :
Assert.assertNotEquals(actual, expected, message);
3. assertTrue -
This type of assertion is used when you are checking if condition is true. That is when we are dealing with boolean values this assertion is used. Whenever test case passes it returns true and if condition is false then it skips the current method and jumps to next.
Syntax :
Assert.assertTrue(condition);
4. assertFalse -
It checks if value returned is false or not. Whenever test case passes it aborts the method and gives an exception.
Syntax :
Assert.assertFalse(condition);
5. assertNull -
This assertion checks if the object is null or not. It aborts the test if object is null and gives an exception.
Syntax :
Assert.assertNull(object);
6. assertNotNull -
This assertion checks if object is null or not. It aborts the test if object is not null that is if object is having any value and gives an exception.
Syntax :
Assert.assertNotNull(object);
2. Soft Assertion :
These types of Assertions are the type of assertions do not throw an exception when an assertion fails and continues with the next step after the assert statement.
Similar Reads
How to run Selenium Running Test on Chrome using WebDriver Selenium is a popular open-source tool for automating web browser interactions. It works with different web browsers like Chrome, Firefox, and more, and different programming languages like Python or Java to write tests. What is a Selenium ChromeDriver?ChromeDriver is a separate executable that Sele
3 min read
How to Take a Screenshot in Selenium WebDriver Using Java? Selenium WebDriver is a collection of open-source APIs used to automate a web application's testing. To capture a screenshot in Selenium, one must utilize the Takes Screenshot method. This notifies WebDriver that it should take a screenshot in Selenium and store it. Selenium WebDriver tool is used t
3 min read
Selenium WebDriver-Installation Selenium WebDriver is a powerful tool for automating web applications for testing purposes. It allows developers and testers to write automated tests in various programming languages like Java, Python, C#, etc. Also, it supports different browsers like Firefox, Chrome, Edge, etc. for testing. Approa
2 min read
How to Use Soft Asserts in TestNG? TestNG is an open source automation framework that is used to perform software testing more easily, it provides various annotations and is used to perform parallel testing and produce the test reports. TestNG provides asserts to check the actual results with the expected results, Asserts are necessa
3 min read
Selenium IDE-First Test Case Selenium IDE is an open-source tool that is widely used in conducting automated web testing and browser automation. This tool is intended mainly for Web Application testers and developers to develop, edit, and run automated test cases for Web Applications. Selenium IDE lets you easily playback and r
5 min read
Difference between Selenium and TestNG Selenium and TestNG are two powerful tools widely used in the field of software testing. Selenium is an open-source toolset specifically designed for automating web browsers, enabling testers to write scripts in various programming languages like Java, C#, Python, and Ruby. It supports multiple brow
5 min read