What is TestNG? Installation with Simple Example

Leyla GORMEL
3 min readMay 3, 2021

In this article, I will show you how we install and import TestNG to our project.

What is TestNG?

TestNG’s name comes from Next Generation Test. TestNG is a testing framework for a run to test cases more easily.

Advantages of TestNG:

  • End-to-end testing is much easier
  • Test cases can be grouped more easily
  • Parallel testing is possible
  • Easy to take reports
  • Easy to learn and use

I am going to add Annotations that we will use for this automation. I share just these if you want to see more details you can read this URL: https://testng.org/doc/documentation-main.html#annotations.

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.

@Test: Marks a class or a method as part of the test.

Import TestNG to IntelliJ IDEA project

First of all, click this URL https://mvnrepository.com/artifact/org.testng/testng to copy TestNG code.

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>

Enter library code to between these two areas <dependencies></dependencies> to pom.xml file and save it.

Now open a new Java Class file under this path src -> test ->java and enter a name to the java class file and save.

Test case title: Search Text on Google

Test case steps:

  • Open Browser
  • Open “https://www.google.com/”
  • Enter text and search it
  • Check results
  • Close browser

There is a java class file’s content. I check test code blocks with try-catch and I enter failed message to inside catch so if the test case fails then I can more easily find which part of the failure.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.*;

public class FirstChromeSearch {
public final String DRIVER_PATH = "driver/chromedriver";
public final String DRIVER_TYPE = "webdriver.chrome.driver";
public final String BASE_URL = "https://www.google.com/";
public final String CHROME_SEARCHBAR_XPATH = "/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input";
public final String SEARCH_WORD = "What is TestNG";
public WebDriver driver;

@BeforeTest
public void beforeTest() {
try{
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
System.setProperty(DRIVER_TYPE, DRIVER_PATH);
driver = new ChromeDriver(options);
driver.get(BASE_URL);
}
catch (Exception e){
System.out.println("Open browser failed");
}
}

@Test
public void OpenChromeAndSearchText() throws Exception {
try{
WebElement element = driver.findElement(By.xpath(CHROME_SEARCHBAR_XPATH));
element.sendKeys(SEARCH_WORD);
element.submit();
}
catch (Exception e){
System.out.println("Search text failed");
}
}

@AfterClass
public void CloseBrowser() throws Exception{
try{driver.quit();}catch (Exception e){System.out.println("Close Browser Failed");}
}
}

You can see detail about the test run and result. You can see the browser version, logs, number of passes test cases, or failure of test cases from here.

P.S

Maybe you can see this error when you tried to open the chrome browser “Error: “chromedriver” cannot be opened because it is from an unidentified developer.” You should give permission from System Preferences > Security & Privacy, under the General tab.

--

--