Selenium Webdriver Tutorial with JAVA and TestNG (2018 Update)

Selenium Webdriver Tutorial by using JAVA and TestNG article is a perfect starting point for Selenium Test Automation with TestNG. In this article, I will explain how to start to Selenium Webdriver with JAVA and TestNG step by step. If you want to start Selenium with JUnit, you can also read this article. Let’s start to Selenium Automation with TestNG and JAVA.

The Tool Belt for Selenium Webdriver Tutorial with JAVA and TestNG

Web Automation FrameworkSelenium Webdriver

Development KitJAVA JDK

Add-On For CSS and XPath Selection: Ranorex Selocity

Test FrameworkTestNG (The other popular test runner framework is JUnit)

IDE: IntelliJ Community Edition

Build SystemMaven

Environment Editor (Optional)Rapid Environment Editor (RapidEE)

Selenium Webdriver Tutorial Step by Step Installations

  • Install Firefox and Chrome
  • Install Ranorex Selocity
  • Install RapidEE
  • Install JAVA JDK
  • Install Maven
  • Install IntelliJ, Download Firefox Driver, Download Chrome Driver

You can check this article for setup instructions. 

Selenium Webdriver and TestNG

We will setup lastest settings and start to Selenium Webdriver Automation with TestNG with a basic example. After your first project, you will improve your skills by reading our other selenium webdriver tutorials.

Setup Drivers for Selenium 3.X

After Selenium 3.X, we need to declare browser drivers in our test codes. The easiest solution is to download all drivers in a folder and add this folder’s path in environmental path variable and after restart our PC, our tests will see the browser drivers.

– Download Chrome and Firefox Drivers and save them under C:\drivers folder.

selenium testng

– Then, add this folder in system path variable:

selenium testing

– Then, Restart your PC!

– For Mac users, you can learn how to change paths here.

Maven and POM.XML File

Maven is used to defining project structure, dependencies, build, and test management. Using pom.xml you can configure dependencies needed for building testing and running code. Maven automatically downloads the necessary files from the repositories while building the project.

This pom.xml comprises of:

  • Selenium Dependency is needed to use Selenium browser automation framework in our project.
  • TestNG Dependency is needed to use TestNG test runner framework in our project.
  • Maven-surefire-plugin is used to configure and execute tests. Here plugin is used to configure the TestNG.xml for TestNG test and generate test reports.
  • Maven-compiler-plugin is used to help in compiling the code and using the particular JDK version for compilation.

You can also add several libraries with their maven dependencies.

selenium testing

TestNG XML File

TestNG is a testing framework and we can run our Selenium Tests by using its annotations such as @BeforeMethod, @Test, @AfterMethod, etc.

We can structure and control our test classes with TestNG.xml file. We should add class names, methods, groups, parameters, in this file to run our test with specific configurations.

The basic structure of TestNG.xml file as shown below:

testng tutorial

TestNG Annotations and Their Meaning

@BeforeSuite: The annotated method runs before all tests in this suite.

@BeforeTest: The annotated method runs before test classes.

@BeforeGroups: The annotated method runs before the group’s tests.

@BeforeClass: The annotated method runs before the first test method which belongs to any of the test groups.

@BeforeMethod: The annotated method runs before each test method.

@Test: The annotated method is a test method. It executes the test.

@AfterMethod: The annotated method runs after each test method.

@AfterClass: The annotated method runs after all the test methods in the current test class.

@AfterGroups: The annotated method runs after the last test method which belongs to any of the test groups.

@AfterTest: The annotated method runs after test classes.

@AfterSuite: The annotated method runs after all tests.

Suite > Test > Class > Method

Test Automation Project with Selenium JAVA and TestNG

Open IntelliJ and create a New Project as shown below:

selenium automation

Select Maven and click Next.

testng vs junit

Fill GroupId and ArtifactId as “FirstSeleniumTestNGExample” and click Next.

selenium webdriver

Write your project name as FirstSeleniumTestNGExample and select your project folder and click Finish button.

testng tutorials

Click Enable Auto-Import (This option automatically imports maven dependencies into your project.)

what is testng

The project structure is shown below.

testng selenium example

Now, we need to add Selenium and TestNG libraries in our project by using their dependencies. In order to get those dependencies, we can go to and

We should add TestNG and Selenium Dependencies in pom.xml as follows. Maven will automatically download the required files into our project.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""


        <!-- -->

        <!-- -->


Then, right-click the project, select “New”, then click “File”.

selenium testng tutorial

Write “testng.xml” as a file name and click “OK”.

selenium webdriver tutorial

In order to create a test file, right click “java” folder under the “test” folder and select “New” and click “Java Class”.

learn selenium

Give a name to your test as “FirstTest”.

selenium example

Test Scenario:
– Open
– Get it’s title, prints it on the console, and check that it is correct or not.

import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

 * Author:      Onur Baskirt
 * Description: This is the first Selenium TestNG test.
 *              It opens swtestacademy homepage and prints and checks its title.

public class FirstTest {

    //-----------------------------------Global Variables-----------------------------------
    //Declare a Webdriver variable
    public WebDriver driver;

    //Declare a test URL variable
    public String testURL = "";

    //-----------------------------------Test Setup-----------------------------------
    public void setupTest (){
        //Create a new ChromeDriver
        driver = new ChromeDriver();

        //Go to

    public void firstTest () {
        //Get page title
        String title = driver.getTitle();

        //Print page's title
        System.out.println("Page Title: " + title);

        Assert.assertEquals(title, "Software Test Academy", "Title assertion is failed!");

    //-----------------------------------Test TearDown-----------------------------------
    public void teardownTest (){
        //Close browser and end the session

Now, we need to construct our testng.xml file. We can do it by using IntelliJ or manually. If you use IntelliJ, right-click your test file and click “Create ‘FirstTest’… icon as shown below.

testng example

After you click “OK” button, you will see below icon on the right top corner of IntelliJ and if you click Run icon, your selenium testng test will start.

testng xml

If you want to use manually configured testng.xml file, you need to open our testng.xml file and modify it as shown below.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "" >

<suite name="AllTestsSuite">
    <test name="My First Selenium TestNG Test">
            <class name="FirstTest"/>

After, right-click to testNG.xml file and run it.

Test Result:

You can find a sample project’s code here

For more information about TestNG please visit here.

[fusion_widget_area name=”avada-custom-sidebar-seleniumwidget” title_size=”” title_color=”” background_color=”” padding_top=”” padding_right=”” padding_bottom=”” padding_left=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””][/fusion_widget_area]

Thanks for reading Selenium Webdriver Tutorial by using JAVA and TestNG Tutorial article.
Onur Baskirt

4 thoughts on “Selenium Webdriver Tutorial with JAVA and TestNG (2018 Update)”

  1. Thank you for your tutorial.
    I got Failure which is because of this line: “at FirstTest.setupTest( ”
    driver = new ChromeDriver();

    I have Chromedriver and added it to my Environment variable.
    Could you please help me with this? thank you


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.