Input your search keywords and press Enter.

TestNG Asserts- How to perform verification in Selenium?

TestNG Asserts Banner

You must be knowing that we categorize user extensions into three parts, which are- Actions, Accessors, and Assertions. The agenda of this tutorial is to explain you different TestNG Asserts command which simply helps to identify whether the scenario is working as expected or not. You can use these TestNG Asserts command with your reporting, which allows you implementing failure exception to get printed to the console. We have a lot to discuss and a lot to know about different TestNG Asserts command, but before that let’s have a look over some of the premier tutorials on TestNG:

These are some of the important tutorials which will help you in setting up an efficient Selenium project by using the TestNG framework.

What is TestNG Asserts?

TestNG Asserts are some of the commands which are used to perform verification of the certain conditions. They are used for assertion purposes.

Difference between Assert and Verify

Both Assert and Verify commands are used to perform verification on the test scenario so what is the difference between them? There is a difference of very thin line between Assert and Verify. Let’s discuss.

When we are using Assert command, expected and action results do not match then test will be aborted instantly, hence, the next test steps will be skipped. Unlike Assert, when we use Verify command, tests do not get aborted on a mismatch between Actual and Expected, rather, it sends failure log to the console and test continues further.

TestNG Asserts Banner

What are TestNG Assert commands?

TestNG Asserts have many commands. It depends on the consciousness of the Automation engineer as for how he wants to use these commands. Let’s have a look at them.

1. assertEquals(condition actual, condition expected)

Here it could be anything in a condition like String, Boolean, Integer, Collection or any other Object.

2. assertEquals(condition actual, condition expected, String message)

The condition will be applicable as above, additionally, we pass a message to display on the true result.

3. assertEqualsNoOrder(Object[] actual, Object[] expected)

It checks equality without any order.

4. assertEqualsNoOrder(Object[] actual, Object[] expected, String message)

It checks equality without any order, here it passes the message.

5. assertNotEquals(condition actual, condition expected)

Here it could be anything in a condition like String, Boolean, Integer, Collection or any other Object. It checks the non-equality condition.

6. assertNotEquals(condition actual, condition expected, String message)

The condition will be applicable as above, additionally, we pass a message to display on the true result. It checks the non-equality condition.

7. assertFalse(boolean condition)

It checks the false condition.

8. assertTrue(boolean condition)

It checks the true condition.

9. assertFalse(boolean condition, String message)

It checks the false condition, but it passes the message.

10. assertTrue(boolean condition, String message)

It checks the true condition, but it passes the message.

11. assertNull(condition actual, condition expected)

It validates whether a condition is null.

12. assertNull(condition actual, condition expected, String message)

It validates condition is null, additionally, we pass the message to display on a true result.

13. assertNotNull(condition actual, condition expected)

It validates condition is not null.

14. assertNotNull(condition actual, condition expected, String message)

It validates condition is not null, here it passes the message.

15. assertSame(condition actual, condition expected)

It validates whether the condition is the same.

16. assertSame(condition actual, condition expected, String message)

It validates condition is same, additionally, we pass the message to display on the true result.

17. assertNotSame(condition actual, condition expected)

It validates condition is not the same.

18. assertNotSame(condition actual, condition expected, String message)

It validates condition is not same, here it passes a message

19. fail(String message)

These are the TestNG Asserts command which is used to assert the scenario. Let’s have a look at its sample program.

Sample Program

Console Output

 TestNG Asserts image

That’s all about TestNG Asserts, please post your queries in the comment section below and don’t forget to join our Facebook group for the latest updates.

Join Inviul fb group

Avinash Mishra is the author of Inviul blog. He is a software engineer and blogger by choice. He loves to write blogs, and apart from blogging, he is interested in documentary film making, listening to music, traveling around the world and philanthropic activities.

Leave a Reply

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