1. Create a Maven Project
2. Add Selenium, TestNG, Cucumber dependencies in pom.xml
3. Add maven compiler plugin in pom.xml
4. Create feature file
5. Create BasePage class
6. Create Hooks
7. Create CucumberRunner class
8. Create Step Definition class
9. Run the project with TestNG framework.
10. Run the tests using TestNG.xml
11. Reporting : Cucumber Reporting
12. Reporting : TestNG Reporting.
Download the project : Click Me
1. Create a Maven Project
While Creating the Maven Project the Structure would be as shown in below image
3. Add maven compiler plugin in pom.xml
<projectxmlns="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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CucumberSeleniumTestNG</groupId>
<artifactId>CucumberSeleniumTestNG</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>7.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>7.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
@tag
Feature: Login to HRM Application
I want to use this template for my feature file
@ValidCredentials
Scenario: Login with valid credentials
Given User is on home page
When User enters username as "Admin"
And User enters password as "admin123"
Then User should be able to login successfully
package com.sadakar.common;
importorg.openqa.selenium.WebDriver;
publicclassBasePage{
publicstatic WebDriver driver;
}
package com.sadakar.common;
importorg.openqa.selenium.chrome.ChromeDriver;
importio.cucumber.java.After;
importio.cucumber.java.Before;
publicclassHooksextends BasePage {
@Before
publicvoidsetupDriver()throws InterruptedException {
System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
driver =new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
}
@After
publicvoidquit()throws Exception {
driver.quit();
}
}
package com.sadakar.testng.runner;
importio.cucumber.testng.AbstractTestNGCucumberTests;
importio.cucumber.testng.CucumberOptions;
@CucumberOptions(tags =" @ValidCredentials",
features ="classpath:cucumberfeatures", glue ={"com.sadakar.common","com.sadakar.stepdefinitions",
"com.sadakar.testng.runner","com.inovalon.cucumber.common"},
plugin ={"pretty","json:target/cucumber-reports/cucumber.json","html:target/cucumber-reports/cucumberreport.html"}, monochrome =true)
publicclassCucumberRunnerextends AbstractTestNGCucumberTests {
}
package com.sadakar.stepdefinitions;
importorg.openqa.selenium.By;
importcom.sadakar.common.BasePage;
importio.cucumber.java.en.Given;
importio.cucumber.java.en.Then;
importio.cucumber.java.en.When;
publicclassHRMLoginPageextends BasePage {
@Given("User is on home page")
publicvoiduser_is_on_home_page(){
driver.get("https://opensource-demo.orangehrmlive.com/");
}
@When("User enters username as {string}")
publicvoiduser_enters_username_as(String userName){
System.out.println("Username entered");
driver.findElement(By.name("txtUsername")).sendKeys(userName);
}
@When("User enters password as {string}")
publicvoiduser_enters_password_as(String password){
System.out.println("Password entered");
driver.findElement(By.name("txtPassword")).sendKeys(password);
driver.findElement(By.id("btnLogin")).submit();
}
@Then("User should be able to login successfully")
publicvoiduser_should_be_able_to_login_successfully(){
String newPageText = driver.findElement(By.id("welcome")).getText();
System.out.println("newPageText ="+ newPageText);
}
}
<?xml version = "1.0"encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suitename ="Suite1">
<testname ="HRMTests">
<classes>
<classname ="com.sadakar.testng.runner.CucumberRunner"/>
</classes>
</test>
</suite>
plugin ={"pretty","json:target/cucumber-reports/cucumber.json","html:target/cucumber-reports/cucumberreport.html"}