selenium and testing setup with maven

Selenium and Testng setup with maven – Why do we need Maven?

Selenium and Testng setup with maven! At the present time, Maven is the best dependency management tool, a project created by Maven is easy to manage transport. Selenium is a wide browser automation tool while TestNG is the best testing framework that provides an enormous number of methods to assert actual results with the expected result. In other words, Selenium and TestNG with maven is the most popular combination in Industries. All things considered, 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 of performing selenium and testng setup with maven, we guide you to do complete Selenium and TestNG set up and also write the first selenium test. This tutorial explained each step with a clear snapshot which helps you to do this setup independently.

Pre-Requisites:

  1. Most important, 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 the 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/

Selenium and Testng setup with Maven – How to install or setup Selenium?

Check below how to install Selenium:

  • At first, go to Desktop and click on the Eclipse icon to open eclipse
Open Eclipse IDE
  • Select workspace
Eclipse launcher
  • Create Selenium Project (similar to Maven Project): Go to File> New and click on Project
Create Java Project
  • Select a Wizard: To create Maven Project click on “Maven Project” and then click on Next
Maven project
  • Click on “Next” on “New Maven Project” window
  • Select Archetype – maven-archetype-quickstart and click on Next
Select Archetype for maven project
  • Specify project archetype parameters and click on Finish.
Maven archetype parameters
  • Click on Project and open POM.xml
POM.XML
  • Add Selenium, TestNG, and WebDriverManager Maven Dependencies

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


Selenium Dependency: (Current Stable Version) – To add 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 add 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) – We use WebDriverManager 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 dependencies 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>
  • Right-click on Project, go to Maven and click on “Update Project”.
Maven Update Project
  • 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
  • Create a 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();
	}
}
  • 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:

  • Lastly, 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 you have created Selenium and TestNG Project with Maven. And also you successfully write one test, executed and TestNg Report generated.

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

Additionally, explore our other blogs for a more step-by-step tutorial. Also, request you to write your feedback and follow us on Facebook and LinkedIn.