TestNG setup using Maven for Selenium

Reading Time: 3 minutes

Definitions

TestNG (Test Next Generationis) is an open source testing framework. TestNG is inspired from JUnit and NUnit. It is an improvised framework which makes it more powerful and easier. It provides the developer to write more powerful and flexible tests with the help of easy annotations, sequencing, grouping and parametrization. We can use TestNG for generating reports. The reports generated will be of two types pass and fail. If we wish we can run any of the report anytime.

Maven was created to have a standard way in which Projects can be built. One of its powerful features is dependency management. IntelliJ IDEA has in-built support for Maven.

Isn’t it great to use TestNG and Maven together Lets get started with TestNG setup using Maven for Selenium

Prerequisites

  • Java
  • Inellij

Don’t worry if the prerequisites are not installed. You can refer to my previous blog.

https://wordpress.com/post/blog.knoldus.com/93849

How to import Maven project in Intellij IDEA?

  • Go to File > Project
  • Select Maven and Project SDK to java package and click next.
  • Add GroupId, ArtifactId and click next. We should add a project name after clicking next. We are ready to implement.
  • Go to Pom.xml and add dependencies for TestNg, Maven, Selenium
  • We have to add selenium Java in Pom.xml from the below external link.
https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
  • For TestNg go to the below link.
https://mvnrepository.com/artifact/org.testng/testng
  • Copy the dependencies and paste in POM.xml. Now you will get option to import the dependencies. Do that.

How to use TestNg annotations ?

  • If you are not aware of the format used for TesntNg, stay calm and use the below format.
@DataProvider (name = “name_of_dataprovider”)
public Object[][] dpMethod() {
return new Object [][] { values}
}
  • Good going. Now you have to create a java project and implement the below example. Run the code as TestNGTest
import org.testng.annotations.Parameters; import org.testng.annotations.Test; import org.openqa.selenium.By;
public class MyFirstTestNg { @DataProvider (name = “data-provider”) public Object[][] dataMethod(){ return new Object[][] {{“FirstName”}, {“SecondName”}}; }
@Test (dataProvider = “data-provider”) public void test (String val) { System.out.println(“Passed Parameter Is : “+ val); } }
  • Run the above example as Run As -> TestNG Test.Run testing.xml and check the output.
<suite name = “Suite”> <test name=” Test”>
<classes> <class name = “Code.MyFirstTestNg”>
</class> </classes> </test> </suite>
  • You should run testing.xml and after running you check the output

Congratulations we have completed a data driven program with the help of TestNg hence TestNG setup using Maven for Selenium

References:-

https://www.toolsqa.com/testng/testng-data-provider-excel

https://blog.testproject.io/2017/04/03/testng-tutorial/

Knoldus-blog-footer-image

Written by 

I work with Knoldus and have experience of 8+ years in quality assurance and also 2 years of experience as an ETL consultant. I possess the attitude of conveying self-confidence and effective teamwork in Project Implementation. I am highly self-motivated with the ability to manage and co-ordinate events for the team with great success. I have good experience on SeleniumWebdriver(C#), Selenium IDE, Nightwatch,Appium, Jira, TFS, ZAP, Fiddler, Jmeter, Jenkins, Selenium Webdriver. I am also ITIL certified. I have worked on projects like innovation system, windows datawarehous and billing application, mobile application etc.