Cucumber BDD with Selenium WebDriver and Testng Framework
Cucumber is a Behavior Driven Development (BDD) testing framework that helps the non technical members of the team can easily understand the scenario’s automating by testers. In Cucumber, the feature files plays very important role that contains plain English text written using gherkin language which is easy to understand. Refer to the Cucumber Basics
Cucumber-BDD using Page Object Model and Testng Framework:
1. Create a Maven Project with name as ‘cucumbermvn’. Refer to the steps->
2. Add the following dependencies and plugins required for Cucumber,Selenium,Testng.
Refer to the dependencies for Cucumber – Java project
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<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>org.totalqa.cucumber</groupId> <artifactId>cucumbermvn</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>cucumbermvn</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <forkCount>0</forkCount> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <fork>true</fork> <executable>C:\Program Files\Java\jdk1.8.0_65\bin\javac.exe</executable> </configuration> </plugin> <plugin> <groupId>net.masterthought</groupId> <artifactId>maven-cucumber-reporting</artifactId> <version>3.15.0</version> <executions> <execution> <id>execute</id> <phase>verify</phase> <goals> <goal>generate</goal> </goals> <configuration> <outputDirectory>target/cucumber-reports/advanced-reports</outputDirectory> <cucumberOutput>target/cucumber-reports/CucumberTestReport.json</cucumberOutput> </configuration> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.11.0</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-testng</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-jvm-deps</artifactId> <version>1.0.5</version> <scope>provided</scope> </dependency> </dependencies> </project> |
3. Create a following Directory Structure for the project as mentioned below:
4. Refer to the Functional Tests OpenMRSTests.java as mentioned below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package org.cucumber.tests; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import cucumber.api.CucumberOptions; import cucumber.api.testng.CucumberFeatureWrapper; import cucumber.api.testng.TestNGCucumberRunner; @CucumberOptions( features="src/test/resources/features", glue={"org.cucumber.stepdefs"}, format= {"pretty", "html:target/cucumber-reports/cucumber-pretty", "json:target/cucumber-reports/CucumberTestReport.json", "rerun:target/cucumber-reports/re-run.txt"} ) public class OpenMRSTests { public static WebDriver driver; private TestNGCucumberRunner testRunner; @BeforeClass public void setUP() { System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); driver = new ChromeDriver(); testRunner = new TestNGCucumberRunner(OpenMRSTests.class); } @Test(description="login",dataProvider="features") public void login(CucumberFeatureWrapper cFeature) { testRunner.runCucumber(cFeature.getCucumberFeature()); } @DataProvider(name="features") public Object[][] getFeatures() { return testRunner.provideFeatures(); } @AfterClass public void tearDown() { testRunner.finish(); } } |
5. Refer to the Step Definitions file LoginPage.java as mentioned below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package org.cucumber.stepdefs; import org.cucumber.tests.OpenMRSTests; import org.openqa.selenium.By; import org.testng.Assert; import cucumber.api.java.en.Given; import cucumber.api.java.en.Then; import cucumber.api.java.en.When; public class LoginPage { @Given("^Open Application and Enter url$") public void open_Application_and_Enter_url() throws Throwable { OpenMRSTests.driver.get("https://demo.openmrs.org/openmrs/login.htm"); } @When("^enter username$") public void enter_username() throws Throwable { OpenMRSTests.driver.findElement(By.id("username")).sendKeys("Admin"); } @When("^enter password$") public void enter_password() throws Throwable { OpenMRSTests.driver.findElement(By.id("password")).sendKeys("Admin123"); OpenMRSTests.driver.findElement(By.id("Inpatient Ward")).click(); OpenMRSTests.driver.findElement(By.id("loginButton")).click(); } @Then("^verify Msg$") public void verify_Msg() throws Throwable { boolean result = OpenMRSTests.driver.findElement(By.tagName("h4")).getText().contains("Logged"); Assert.assertTrue(result); } } |
6. Feature file Login.feature contains the steps to be performed as part of the scenario:
1 2 3 4 5 6 7 8 9 10 |
Feature: feature name Scenario: Authenication Given Open Application and Enter url When enter username And enter password Then verify Msg |
7. Refer to the testng.xml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite" verbose="5"> <test name="Test"> <classes> <class name="org.cucumber.tests.OpenMRSTests"/> </classes> </test> <!-- Test --> </suite> <!-- Suite --> |
8. Execute the following maven command with Goals as verify to generate the Cucumber Reports.
mvn verify
its deprecated version of cucumber. New version is 3x
suppose we have multiple feature file in same folder and if we want to run each one feature file without hard code in @cucumberoption then how to configure each feature file in testng.xml file.
Hi ,
In the above example we have not hardcorded the feature file name.
So it will pick all the feature files available under the features folder.
features=”src/test/resources/features”
Regards,
total-qa.com
I am using below dependencies getting error
The method getCucumberFeature() is undefined for the type CucumberFeatureWrapper
The method provideFeatures() is undefined for the type TestNGCucumberRunner
Any specific reason why its not working
@Test(groups = “cucumber”,dataProvider=”features”)
public void test(CucumberFeatureWrapper wrapper) throws Exception {
testRunner.runCucumber(wrapper.getCucumberFeature());
}
@DataProvider(name=”features”)
public Object[][] getFeatures()
{
return testRunner.provideFeatures();
}
org.testng
testng
6.8
org.seleniumhq.selenium
selenium-java
2.53.1
io.cucumber
cucumber-java
4.2.6
io.cucumber
cucumber-testng
4.2.6
i have created a same project i am encountering step_definitions.cstLogin.userWillProvideInformationForTheNewUserid(cstLogin.java:12)