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:
- How to use @Factory annotation in Selenium with @DataProvider?
- Implement @Parameters annotation to pass test data
- How to retry failed test cases in Selenium?
- A technique to handle exceptions using TestNG
- How to implement TestNG Listener in Automation framework?
- Implement multi-threading in Selenium
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.
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
package Test; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Test; public class AssertsExample { @Test public void assertTestMethod() { System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://www.inviul.com/"); driver.manage().window().maximize(); String expectedTitle = "Testing"; Assert.assertEquals(driver.getTitle(), expectedTitle, "Title didn;t match"); driver.close(); driver.quit(); } }
Console Output
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.