Selenium and TestNG Setup With Maven

Why we need Maven?

Maven is the best dependency management tool, a project created by Maven is easy to manage transport. Selenium is widely browser automation tool while TestNG is the best testing framework which provides an enormous number of methods to assert actual result with the expected result. In other words, Selenium and TestNG with maven is the most popular combination in Industries. This tutorial helps you in maven project creation, adding Selenium and TestNG Dependency. Also, one sample test is written to ensure correct installation.


In this tutorial, we guide you to do complete Selenium and TestNG set up and also write first selenium test. This tutorial explained each step with a clear snapshot which helps you to do this set up independently.

Pre-Requisites:

  1. One Machine with 32-bit or 64-bit operating system and min RAM 4 GB
  2. Java should be Installed
  3. Eclipse should be installed on your machine

How to check Java version or Installation in cmd?

Steps to check Java Installation: Open command prompt and type “java -version” and enter.

Check java version

Install Java If not already installed on your machine:

If java is not installed on your machine then read this tutorial Step by Step tutorial for Java installation

Or, https://thoughtcoders.com/blog/install-java-and-setup-environment-variable/

How to check Maven Installation or version in cmd?

Steps to check Java Installation: Open command prompt and type “mvn -version” and enter.

Check maven version

If Maven is not installed on your machine then read this tutorial: Step by Step Tutorial for Maven installation

Or, https://thoughtcoders.com/blog/step-by-step-tutorial-for-maven-installation/

How to install or setup Selenium?

Steps to install Selenium:

  • Step 1: Go to Desktop and click on Eclipse icon to open eclipse
Open Eclipse IDE
  • Step 2: Select workspace: Eclipse is already set up then it will open with default workspace but if you are opening the first time then it will ask for workspace. Then you have to give one folder path as workspace.
Eclipse launcher
  • Step 3: Create Selenium Project (similar to Maven Project): Go to File> New and click on Project
Create Java Project
  • Step 4: Select a Wizard: To create Maven Project click on “Maven Project” and then click on Next
Maven project
  • Step 5: Click on “Next” on “New Maven Project” window
  • Step 6: Select Archetype – maven-archetype-quickstart and click on Next
Select Archetype for maven project
  • Step 7: Specify project archetype parameters and click on Finish. Now your automation project created with Maven.
Maven archetype parameters
  • Step 8: Click on Project and open POM.xml
POM.XML
  • Step 9: Add Selenium, TestNG, and WebDriverManager Maven Dependencies

When we add a dependency in POM.xml then Maven automatically downloads jar and build in your Project.


Selenium Dependency: (Current Stable Version) – To be added in POM.XML

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>

TestNG Dependency: (To be added in POM.XML)

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
<scope>test</scope>
</dependency>

WebDriver Manager Dependency: (To be added in POM.XML) – WebDriverManager is used to download browser dependency automatically. To know more about WebDriver Manager refer our this detailed blog- WebDriver Manager in Selenium

<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency> <groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.8.1</version>
</dependency>

POM.XML file depencies for WebDriver Manager, TestNG and Selenium:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.thoughtcoders</groupId>
<artifactId>blogsAutomation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>blogsAutomation</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>	<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>	<groupId>org.seleniumhq.selenium</groupId>	
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>	<artifactId>testng</artifactId>
<version>7.1.0</version>		<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>		<groupId>io.github.bonigarcia</groupId>		<artifactId>webdrivermanager</artifactId>		<version>3.8.1</version>
</dependency>
	
</dependencies>
  • Step 10: Right-click on Project, go to Maven and click on “Update Project”.
Maven Update Project
  • Step 11: Install TestNG Plugin in Eclipse: Go to Help and navigate to “Eclipse Marketplace” and install TestNG Plugin
Testng plugin - Eclipse Marketplace

Click on “Install” to install “TestNG for Eclipse”.

TestNG for Eclipse
  • Step 12: Create test package and then create one LoginTest class
package Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
import junit.framework.Assert;
public class LoginTest {
@Test
public void LoginTest() {
		WebDriverManager.chromedriver().version("80.0.3987").setup();
WebDriver driver = new ChromeDriver();
// driver.get method used to naviagte to website
		driver.get("http://newtours.demoaut.com/");
// maximize browser size
		driver.manage().window().maximize();
// Assert Website Title with Expected
String title =driver.getTitle();
		Assert.assertEquals("Welcome: Mercury Tours", title);
// Close Browser
driver.close();
	}
}
  • Step 13: Run Test as Test as “TestNG Test”. Now your test run and browser open perform the test and verify the result.
Run As TestNG

Console Output:

  • Step 14: Right-click on Project and Refresh Project (Press F5) and go to test-output Folder and open emailable-report.html file.
Test Report

Great! Now Selenium and TestNG Project created with Maven. And also you successfully write one test, executed and TestNg Report generated.

Hope this simplified steps boost your confidence. Still if you face issue in this tutorial then feel free to write us on query@thoughtcoders.com.

Explore our other blogs for more step by step tutorial. Also request you to write your feedback.

close

Subscribe to our Newsletter:

Open chat
Feel free to contact