Setup Selenium Java Maven Project with Cucumber And TestNG

In this tutorial, you will learn how to setup Selenium Cucumber Java project with TestNG from scratch.

Pre-requisites:

1. Java is installed on Windows (Follow Here)

2. Maven is installed on Windows (Follow Here)

3. IntellijIdea is installed on Windows (Follow Here)

Steps to Setup Selenium Cucumber Java Framework with TestNG:

1. Launch IntellijIdea and click on New Project.

2. Select Maven as Project type and select a java version in Project SDK.  Click on Next.

3. Enter a name for the project (SampleProject) and click on Finish.

4. Once the project setup is completed. Open the pom.xml file

5. Goto https://mvnrepository.com/ and search for the following dependencies.

– selenium-java

– cucumber-java

– cucumber-core

– cucumber-testng

– testng

6. Select the latest version of the dependency and copy the details within <dependency></dependency>

7. Go back to pom.xml and add all the following dependencies.

<dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.3.0</version>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>6.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>6.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-core</artifactId>
            <version>6.7.0</version>
        </dependency>
    </dependencies>

 

8. Go to src/test/java and right click on New -> Package. Enter a new package  “com.example”.

9. Right click on test folder and select New -> Directory. Enter a new directory  “resources”.

10. Right click on resources and select New -> Directory. Enter a new directory  “features”.

11. Right click on com.sample and select New -> Package. Enter a new package  “StepDefintions”.

12.  Right click on the package and click on New -> Java Class. Enter a new class  “TestRunner.java”.

13. Enter the following in the TestRunner.java class

@CucumberOptions(
        plugin = {"pretty",
                "html:target/cucumber-reports/cucumber.html",
                "json:target/cucumber-reports/cucumber.json"
        },
        features = {"src/test/resources/features"},
        glue = {"com.example.StepDefinitions"}
)

public class TestRunner extends AbstractTestNGCucumberTests {


}

 

14. Right click on the project. Select New -> File.

15. Enter file name as testng.xml in New File dialog box. Click on Enter.

16. Open testng.xml file and paste the below content.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Test Suite" verbose="1">
    <test name="Regression Tests">
        <classes>
            <class name="com.example.TestRunner">
            </class>
        </classes>
    </test>
</suite>

 

17. Add all your Cucumber Feature files under features folder.

18. Add all your Step Definition files under StepDefinitions folder.

19. Right click on testng.xml file and click on Run.

20. Cucumber reports are generated under target folder – cucumber.html & cucumber.json

Watch this video for detailed steps:  Click Here

Share

Bijan Patel

Founder & Creator of QAScript | 12+ years of IT Experience | Full Stack Automation Engineer | Blogger | Trainer

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!