Home Guide Learn about TestNG Framework – How to Automate using Selenium

Learn about TestNG Framework – How to Automate using Selenium

By Sadhvi Singh, Community Contributor and Pradeep Krishnakumar, Manager -

In this article, we provide a step by step guide on starting with TestNG and automating it using Selenium.

TestNG is an open-source test automation framework for Java. It is developed on the same lines of JUnit and NUnit. Few advanced and useful features provided by TestNG makes it a more robust framework compared to its peers. The NG in TestNG stands for ‘Next Generation’. Created by Cedric Beust, it is used more frequently by developers and testers in test case creation, owing to its ease of using multiple annotations, grouping, dependence, prioritization and parametrization features.

TestNG: Installation and Setup

 
It’s pretty easy to install TestNG. If you are using Eclipse IDE, it comes as a plugin to it. Below are the steps to install TestNG:

  1. Install Eclipse IDE from the Eclipse website. It serves as a platform for you to code on and write your test cases
  2. Once installed, go to help and navigate to the ‘Eclipse Marketplace’. The referenced snapshot is below: TestNG with Selenium - step 1
  3. Click on ‘Eclipse Marketplace’. You will be directed to the marketplace modal. Type TestNG in the search keyword and hit ‘Go’. The referenced snapshot is below:TestNG with Selenium - step 3
  4. If TestNG is not installed in your Eclipse, rather than the ‘Installed’ button you would see ‘install’. Click on install and your TestNG framework will be installed in your Eclipse. As a good practice, Eclipse would recommend you to restart to use the features of the installed plugin
  5. Post-restarting your Eclipse, re-verify whether you can see options for creating a TestNG class or not as below: TestNG with Selenium - step 2

TestNG: Annotations

 
An annotation is a tag that provides information about the method, class, and suite. It helps to define the execution approach of your test cases and the different features associated with it. Below are the major annotations used:

  • @Test– This is the root of TestNG test cases. In order to use TestNG, all methods should be annotated with this annotation. Below is an example:
@Test
public void setupTestNG()
{
System.out.println(“this is a test annotation method”)
}

Few attributes associated with the TestNG annotation are:

  1. Description: You can describe your test case under the description, stating what it does
    @Test(description=”This test validates login functionality”)
  2. Priority: You can prioritize the order of your test methods by defining a priority. Based on the defined priority, the test shall execute in that order.
    @Test(priority=1)
  3. DependsOnMethod: This attribute works miracles if one test case is dependent on the other test case. For example, to view your profile details, you need to login to the application. So, your profile test case is dependent on the login test case
    @Test(dependsOnMethod=”Login”)
  4. Enabled: Using this attribute, you can choose to execute or skip the execution of this test case. Setting it to true execute it and setting it to false will skip the test from the execution cycle
    @Test(enabled=’true’)
  5. Groups: Using this attribute, you can club your test cases into a single group and specify the specified group you wish to execute in your TestNG XML file. The test cases clubbed to that group will only be executed and the rest will be skipped
    @Test(groups=”Smoke Suite”)

While the above ones should help you get started, other major annotations are:

  • @BeforeMethod and @AfterMethod – These annotations run before and after each test method
  • @BeforeClass and @AfterClass – These annotations run once before and after the first @test method in a class
  • @BeforeTest and @AfterTest – The BeforeTest annotation runs before the @BeforeClass annotation and the AfterTest annotation runs after the @AfterClass annotation
  • @BeforeSuite and @AfterSuite– These annotations run before and after any test annotated method in a class respectively. These annotations start the beginning of a test and the end of it, for all the classes in a suite

Talking about the execution order of these annotations, they execute in the below order:

@BeforeSuite -> @BeforeTest -> @BeforeClass -> @BeforeMethod -> @Test -> @AfterMethod -> @AfterClass -> @AfterCTest -> @AfterSuite

Note: The key point to remember is apart from @BeforeMethod and @AfterMethod, all other annotations run only once, whereas the @BeforeMethod and @AfterMethod run post every @Test method.

TestNG: Writing your First Test

 
Now, using the above annotations and attributes, we will be writing our first test case with TestNG framework. Let’s take the example of Browserstack, where we will open the landing page and sign up on the platform. Assuming, you have a fair understanding of Selenium and its syntaxes, let’s jump into writing the code.

Step #1 – Create your first TestNG class by right-clicking on the package and from the ‘New’ option, select the ‘Other’ folder.
Selenium with TestNG - Step 6

Step #2 – From the wizard modal, select the TestNG folder and select the TestNG class as below: 
TestNG with Selenium - step 5

Step #3 – Click the ‘New’ button and choose any predefined annotations you wish to have in your class as below: 
TestNG with Selenium - step 4

In our case, we will be using the BeforeMethod, AfterMethod, BeforeClass, and AfterClass along with our test annotation. Do note, the XML file name is given on this modal. Using this TestNG XML file, we can choose to define our execution for the defined class or classes.

Step #4 – Click on finish and you are ready to start writing your first TestNG class. Reference screenshot below, containing the defined methods with the annotations chosen in the step above: Selenium with TestNG - Step 8

Step #5 – In the below code snippet, we will be automating the sign-up flow using these annotations.

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;

public class FirstTestWithTestNGFramework {

WebDriver driver;

@BeforeClass
public void testSetup()
{
System.setProperty("webdriver.chrome.driver", ".\\Driver\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();

}

@BeforeMethod
public void openBrowser()
{
driver.get("https://www.browserstack.com/");
driver.findElement(By.id("signupModalButton")).click();
System.out.println("We are currently on the following URL" +driver.getCurrentUrl());
}

@Test(description="This method validates the sign up functionality")
public void signUp()
{
driver.findElement(By.id("user_full_name")).sendKeys("Sadhvi Singh");
driver.findElement(By.id("user_email_login")).sendKeys("sadhvisingh9049+1@gmail.com");
driver.findElement(By.id("user_password")).sendKeys("BrowserStack123*");
driver.findElement(By.xpath("//input[@name='terms_and_conditions']")).click();
driver.findElement(By.id("user_submit")).click();

}

@AfterMethod
public void postSignUp()
{
System.out.println(driver.getCurrentUrl());

}

@AfterClass
public void afterClass()
{
driver.quit();
}

}

As you can see, in this snippet above, we have used @BeforeClass annotation to setup the browser.

  • In the @BeforeMethod, we have opened the BrowserStack homepage and navigated to the signup page.
  • In the @Test annotated method, we are performing the sign-up functionality.
  • In the @AfterMethod, we are printing the currentURL we are on
  • And in the @AfterClass method, we are closing the browser.

Also, note the associated attribute ‘description’ used with the test annotation. Now, all you need to do is execute this test and validate your TestNG reports and console output.

Step #6 – In order to execute your report, you can either choose to run directly as a TestNG class or run the XML file created which contains the class name and details. Below is the auto-created TestNG XML file:

<?XML version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test">
<classes>
<class name=".FirstTestWithTestNGFramework"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

In a scenario where you wish to execute multiple classes, you can modify the XML file and mention the various class names. You can also define the groups in the XML file which you specifically want to execute here. Once the modification is done, you can right-click on the file and run it. Your class containing the test shall be executed. Below is the referenced console output of the executed test: Selenium TestNG Example

Step #7 – In order to gain access to the TestNG report, all you need to do is refresh your project folder. A folder as ‘test output’ will be auto-generated. Within this folder, you shall see a file ‘index.html’ as below: Testng example

Step #8 – Double click on it and you will be able to view your TestNG reports, showing the passed and failed methods of the execution as below. Testng Tutorial

TestNG provides various listeners to customize this default report according to your needs. But at the beginner level, this default report will be more than enough.

Step #9 – As a QA or tester, the next step is to validate your tests. If you did not validate your test methods, it basically means your testing process is incomplete. To validate your tests, TestNG provides assertions for it. So, let us investigate them to complete your TestNG tutorial.

TestNG: Assertions

 
Like JUnit, TestNG provides multiple level assertions to validate your actual results against your expected results. Few of the commonly used assertions are:

  1. assertTrue– This assertion verifies whether the defined condition is true or not. If true, it will pass the test case. If not, it will fail the test case
    Assert.assertTrue(condition);
  2. assertFalse– This assertion verifies whether the defined condition is false or not. If false, it will pass the test case. If not, it will fail the test case
    Assert.assertFalse(condition);
  3. assertEquals– This assertion compares the expected value with the actual value. If both are the same, it passes the test case. If not, it fails the test case. You can compare strings, objects, integer values etc. using this assert
    Assert.assertEquals(actual,expected);
  4. assertNotEquals: This is just opposite to what assertEquals does. If actual matches the expected, the test case fails, else the test case passes
    Assert.assertNotEquals(actual,expected,Message);

An important part to note in assertions is that your tests will not execute to the next line of code if your assertions failed. It will automatically jump to the next test annotated method.

Now, let us try to validate our code snippet below using assertions. In the snippet above, in the @AfterClass method, we will be verifying the current URL we are on and the expected URL, which should be the signup page.

@AfterMethod
public void postSignUp()
{
Assert.assertEquals(driver.getCurrentUrl(), "https://www.browserstack.com/users/sign_up");
}

Make this change in your code snippet above and execute it. The test case should pass. Also try giving another expected URL, to validate if the tests fail.

Advantages of using TestNG over other frameworks

TestNG provides multiple powerful features compared to other test automation frameworks. This can be explained better by seeing the advantages of TestNG over another popular framework, JUnit.

  1. Annotations of TestNG are easier to understand compared to JUnit
  2. TestNG does not require you to mandatorily declare @BeforeClass and @AfterClass, compared to JUnit
  3. The feature of parametrization provided by TestNG is more convenient and easier to use through dataprovider
  4. Features like prioritization and grouping of tests provided by TestNG makes it more realistic and adaptable as compared to JUnit
  5. TestNG allows dependency on multiple methods, making your tests more maintainable and flow specific as compared to JUnit
  6. TestNG provides the facility of parallel execution in multiple ways compared to JUnit
  7. TestNG gives default reporting in HTML and XML format. These can further be customized using multiple listeners. This makes it much more friendly as compared to JUnit

Having said that, JUnit also has certain advantages compared to TestNG, which we will cover separately. Do note that BrowserStack supports both running Selenium tests using TestNG and running Selenium tests using JUnit.

As an engineering function, if you are automating your test cases and are thinking about choosing the right framework, start by first listing down your current needs and usage. Also, think about what you will need as you scale. Then decide on the framework that works best for you. Avoid picking the latest framework in the market, since that might hamper you from scaling down the line.

Key Takeaways

TestNG makes automated tests more structured, readable, maintainable and user-friendly. It provides powerful features and reporting. Its high-end annotations like dataprovider, makes it easier to scale up, as you perform cross browser testing across multiple devices, browsers, and their versions. Automated Selenium testing tools like BrowserStack supports frameworks like TestNG to write and execute test cases.

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free