(The precondition for this tutorial that the Selenium WebDriver is already installed.)
Firstly go to TestNG website’s download page, and follow the instructions to install the TestNG plugin to Eclipse. (http://testng.org/doc/download.html)

After the TestNG was installed, create a new java class file (without main method).

Create the setUp() function. To this function you can put that the WebDriver use for example the Firefox browser, and for example that the driver get the desired webpage.

This function will start before the test. To ensure that, before the setUp() function need to put @BeforeClass annotation.

@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.

If you use only one test in the java file then you can use the @BeforeTest annotation too.

@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the <test> tag is run.

If you have more than one test in the java file, better to use @BeforeClass annotation to set up the WebDriver, because it not economical to initialize the WebDriver before every test. It’s enough only once before the class.

You can use the @BeforeTest annotation beside the @BeforeClass for example to get the desired webpage.

When you write a TestNG annotation before the function, you need to import the TestNG annotations to the file. Just click to the annotation or to the red x before that, and select :
Import ‘Test’ (org.testng.annotations)

Image1

Beside the @BeforeClass we need an @AfterClass annotation too.
Use this annonation with tearDown() function. The tearDown() function is used to quit form WebDriver.

You can use @AfterTest annotation with the same logic as @BeforeTest.

Before every test you must use the @Test annotation.
The @Test annotation marks a class or a method as part of the test.

After the test was written, you can run the test with the following steps:

  • Select your java file in the Package – or in Project Explorer
  • Right click to it
  • Select Run As -> TestNG Test

 

When the test is finished you can check the result in the Console tab or/and in the TestNG tab in Eclipse.

Image2

Image3

Here is a simple example code how to use TestNG with the Selenium Webdriver:
(The test open the Wikipedia page, click to About link and write out the About page title.)

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestNGSeleniumTest {

// create a WebDriver
WebDriver driver;

// use the Firefox browser and go to the wikipedia site
@BeforeClass
public void setUp(){
driver = new FirefoxDriver();
driver.get("http://en.wikipedia.org/wiki/Main_Page");
}

// quit from WebDriver
@AfterClass
public void tearDown(){
driver.quit();
}

@Test
public void testFindElements()throws Exception{

//find the About link
WebElement about= driver.findElement
(By.xpath("//*[@id='n-aboutsite']/a"));

// click to the link
about.click();

// wait for 5 seconds
Thread.sleep(5000);

// write out the title of the page in console
System.out.println(driver.getTitle());

}
}

If you want more information about TestNG then check out the documentation in the TestNG website: http://testng.org/doc/documentation-main.html

 

Similar Posts from the author:

18 thoughts to “How to use TestNG with Selenium WebDriver

  • one heart

    Your mode of describing the whole thing in this piece of writing is
    genuinely pleasant, every one be able to easily know it,
    Thanks a lot.

  • Syed

    Good info, very clear. thanks

  • Harika

    Please find the below useful link which provides selenium webdriver tutorials which will also guide you to build a good framework.
    Selenium webdriver Tutorial

  • chang

    very good and very clear info.

  • Pankaj Gupta

    Hi ,

    Very Good Article and really very helpful for beginner…

    Vamshi it would be helpful if you can share the selenium framework or design..as i am working on it. There are lots of things to implemant in a framework, Pls share any dummy framework of selenium for rough idea.

    my mail id is : pankajguptadelhi.kumar@gmail.com

    thanks in advance!!!

  • azzura

    Good info and clear. Thanks 🙂

  • Mukesh Otwani

    Clear and informative post

  • ajay

    Hi,
    I am installing testNG in eclipse but I am getting error “Not able to connect with Repository”.
    Any idea why this is coming and what is the workaround for this.

    I tried many things from google.

    Any help in this would be appreciated.

    Thanks

    • Tihomir Turzai

      However if you created a Maven project it is not necessary to install the TestNg plug-in to your Eclipse because Maven can import it from repository.
      In this way you also need WebDriver to build your project but it can be installed through Maven as well. Make sure that your pom.xml contains the appropriate dependencies:



      central
      bintray
      http://jcenter.bintray.com




      org.testng
      testng
      6.9.4
      test


      org.seleniumhq.selenium
      selenium-firefox-driver
      2.46.0


      Firstly go to TestNG website’s download page, and follow the instructions to install the TestNG plugin to Eclipse. (http://testng.org/doc/download.html) After

  • Vikrant Bisht

    Error: Could not find or load main class .test-output

    i am getting this error while testing the website through TestNG.
    for the screen shot refer the URl
    https://www.sendspace.com/file/l349kg

    • Ivan Konjević

      Hello Vikrant,
      This can happen because of several reasons but mostly bad configuration or issues with the paths to the libraries.
      Check your config files, check if the libraries were added correctly, clean and rebuild your project.

  • praveeen

    Valuable information thanks for sharing.

  • Michael

    Hi István,

    Thanks for the post. This is an interesting read and your post has clarified some basic doubts.
    Incidentally, my team also drafted a great post on Selenium Testing and I believe your readers will love to browse “Migrating to Selenium? Know Your Costs, Challenges & Options”. Here is the link to access the post – http://www.gallop.net/blog/migrating-to-selenium-know-your-costs-challenges-options/
    I would love to know your comments on our blog.

    Thanking you Istvan.
    Cheers,
    Michael

Comments are closed.