How to Use TestNG Asserts in Selenium?

TestNG, a widely-used testing framework for Java, offers a way to use assertions. These assertions in TestNG help us compare what we expect to happen with what actually happens during a test. They allow us to determine if a test passed or failed based on specific conditions. In this blog, we’ll explore how TestNG asserts work in conjunction with Selenium for effective validation.

What are Assertions in TestNG?

  • An Assert in Selenium is like a checkmark that confirms if something is true during an automated test.
  • In TestNG, assertions are like detectives, making sure what we expect matches what actually happens.
  • TestNG Asserts act as our checkpoint during a test, helping us see if everything’s going according to plan while the test is running.

Setting Up Your Selenium Project

Before you start using TestNG asserts, you need to set up your Selenium project. Here are the basic steps:

  • Install Selenium: 

You can download the Selenium WebDriver libraries from the official Selenium website. Ensure you have the necessary browser drivers (e.g., ChromeDriver, GeckoDriver) for your chosen browser.

  • Create a Java Project: 

You can use any Java development environment (Eclipse, IntelliJ IDEA, etc.) to create a Java project.

  • Add Selenium Libraries: 

Include the Selenium WebDriver libraries in your project. You can add them to your project’s build path.

  • Download TestNG: 

You can download and install TestNG as a plugin for your IDE. TestNG is a widely used testing framework for Java, and it simplifies the test case execution process.

  • Create a TestNG Class: 

Create a new class in your project and annotate it with @Test. This annotation signifies that this class contains your test methods.

Types of Assertions in TestNG

There are two types of assertions in TestNG:

  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 the following types:

  • assertEquals: 

This is used to check if the expected and actual values in the Selenium WebDriver are the same. When they match, the assertion goes through without any issues. However, if the actual and expected values differ, the assertion fails, causing the test to be marked as unsuccessful..

  • assertNotEquals: 

This is just the opposite of assertEquals.

  • assertNotNull: 

This is used to check if an object is not null.

  • assertNull:

This is used to check if an object is null.

  • assertTrue: 

It examines whether the condition is true or not. If the test case succeeds, it reports as true. However, if the condition is false, it skips the current method and proceeds to the next.

  • assertFalse: 

It verifies if the condition is untrue. If the test case passes, it stops the method and raises an exception.

  1. Soft Assertions: 

Soft assertions are used when we want to execute all the assertions in the test case, even if one of them fails. Soft Assertion can be of the following types:

  • assertAll: 

This is used to verify all the assertions in the test case.

  • assertThat:

This is used to check if the actual value matches the expected value.

How to Use TestNG Asserts with Selenium?

Here is an example of how to use TestNG asserts with Selenium to perform validation:

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

public class TestNGAsserts {
   @Test
   public void testNGAsserts() throws Exception {
      System.setProperty("webdriver.chrome.driver","path/to/chromedriver");
      WebDriver driver = new ChromeDriver();
      driver.navigate().to("https://www.example.com/");
      String actualTitle = driver.getTitle();
      String expectedTitle = "Example Domain";
      Assert.assertEquals(actualTitle, expectedTitle);
      driver.quit();
   }
}

In the above example, we are verifying that the actual title of the webpage matches the expected title. If the actual title and expected title do not match, then the assertion fails, and the test is marked as failed.

TestNG Annotations

TestNG offers various annotations to manage how your tests run. Here are a few of the commonly used ones:

  • @BeforeTest and @AfterTest: 

These annotations specify methods that run before and after all the test methods in a test suite.

  • @BeforeMethod and @AfterMethod: 

These annotations specify methods that run before and after each test method.

  • @BeforeClass and @AfterClass: 

These annotations specify methods that run before and after all the test methods in a test class.

  • @BeforeSuite and @AfterSuite: 

These annotations specify methods that run before and after all the test methods in the entire test suite.

By using these annotations, you can set up and tear down your test environment as needed.

Best Practices for Using TestNG Asserts with Selenium

When using TestNG asserts with Selenium, it is essential to follow some best practices to ensure that our tests are reliable and maintainable. Here are some best practices for using TestNG asserts with Selenium:

  • Use descriptive test method names: 

Test method names should be descriptive and should indicate what the test is testing. This makes it easier to understand the purpose of the test and to debug issues when they arise.

  • Use assertions to validate expected results: 

Assertions should be used to validate expected results. This ensures that our tests are testing what they are supposed to test and that our application is functioning correctly.

Use try-catch blocks: When using hard assertions, it is essential to use try-catch blocks to catch any exceptions that are thrown. This ensures that our tests do not fail prematurely and that we can continue to execute the remaining tests in the test suite.

  • Use soft assertions when necessary: 

Soft assertions should be used when we want to execute all the assertions in the test case, even if one of them fails. This ensures that we can identify all the issues with our application and that we can fix them before releasing our application to production.

  • Use data-driven testing: 

Data-driven testing is a technique where we use different sets of data to test the same functionality. This ensures that our application is functioning correctly for different input values and that we can identify any issues that arise for specific input values.

To conclude:

In the end, using TestNG asserts with Selenium makes sure your web tests work correctly. It helps you check if things are as expected or not. By following some good practices, like giving clear names to your tests and using assertions for validation, you can make your web applications more reliable. So, with TestNG asserts and Selenium, you’re on the right track for successful web testing.

Written by

The Test Tribe

Leave a Reply

Your email address will not be published.

Related Posts

Testing Courses at Thrive EdSchool

Advertisement (Know More)

Get Top Community News

    Top Event and other The Test Tribe updates to your Inbox.

     

    Categories

    Tags