TestNG Framework – How to download and install TestNG in Eclipse

HOME

 

In the previous tutorial, we have discussed about what is TestNG and why it is important. In this tutorial, will discuss how can we download and install TestNG in Eclipse and how to use it.

Pre-Requisite 
1) Eclipse should be installed and configure. Please refer Install and Configure to setup Eclipse to your system.

Install/Setup TestNG
1) Launch Eclipse and go to Help option present at the top and select -“Install New Software”.

 
2) A dialog box will appear, click the Add button.
 
 
3) A new dialog box will appear. Mention Name as TestNG and location as “TestNG P2 – https://testng.org/testng-p2-update-siteand click the Add button.
 
 
4) This time we will see TestNG is added to Install dialog box.
 
 
 
5) Accept the terms and condition and then click the “Finish” button.
 
 
6) Once the installation is completed, you will get a message to Restart the Eclipse .Select to Restart the Eclipse
 
7) To verify if TestNG is installed successfully or not, go to Window, select Show View and then Other.
 
 
8) Select Java and see within Java folder, you will see TestNG. This shows that TestNG is successfully installed on the machine.
 
 
Steps to follow to create a TestNG class
 
1) Create a new TestNG class. Right click on Folder where you want to create the TestNG class. Select TestNG and then Create TestNG class as shown in the below image.
 
 
 
2) In the below image, we can see that Source folder is the name of the folder we want to create the class and we can mention the name of the class in Class name. Under annotations, I have checked @BeforeTest and @AfterTest and click the Finish button.
 
 
 
 
3) We can see that the structure of new TestNG class looks like as shown below.
 
 
4) In the below example, we want to navigate to Amazon page and search for Hard Drive.
@BeforeTest : Launch Firefox and direct it to the Base URL
@Test : Search for HardDrive
@AfterTest : Close Firefox browser

import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;

public class TestNG_Demo {

   public WebDriver driver;

   @BeforeTest
    public void beforeTest() {
      
     System.setProperty("webdriver.gecko.driver","C:\\Users\\vibha\\Downloads\\geckodriver-v0.26.0-win64\\geckodriver.exe");
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    driver.get("https://www.amazon.com//");
 }

@Test
public void Validation() {
    driver.findElement(By.xpath("//*[@id='twotabsearchtextbox']")).sendKeys("hard drive");
    //XPath for search button
      driver.findElement(By.xpath("//*[@class='nav-input']")).click();
   }

@AfterTest
public void afterTest() {
    driver.quit();
  } 
}

5) To execute this program, we need to Right click and select Run as – TestNG Test.

6) The result will look like something shown below. Here, we can see that Test Case Passed is 1, Failed 0 and Skipped 0.

7) As we know that TestNG also produce HTML Reports. To access the report, go to the Eclipse folder and you can see a folder with name test-output inside the Project where we have created TestNG class. Here, it is  C:\Users\vibha\Downloads\eclipse-workspace\Demo

8) Open ‘emailable-report.html‘, as this is a html report open it with browser. It will look like something below.   TestNG also produce ‘index.html‘ report and it resides in the same test-output folder. This reports gives the link to all the different component of the TestNG reports like Groups& Reporter Output. On clicking these will display detailed descriptions of execution.  

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

3 thoughts on “TestNG Framework – How to download and install TestNG in Eclipse

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s