Contents
cucumber
When Cucumber executes a Gherkin step in a scenario, it will look for a matching step definition to execute.
Example project
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns="http://maven.apache.org/POM/4.0.0"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>hellocucumber</groupId>
8 <artifactId>hellocucumber</artifactId>
9 <version>1.0.0-SNAPSHOT</version>
10 <packaging>jar</packaging>
11
12 <properties>
13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14 </properties>
15
16 <dependencyManagement>
17 <dependencies>
18 <dependency>
19 <groupId>io.cucumber</groupId>
20 <artifactId>cucumber-bom</artifactId>
21 <version>7.14.1</version>
22 <type>pom</type>
23 <scope>import</scope>
24 </dependency>
25 <dependency>
26 <groupId>org.junit</groupId>
27 <artifactId>junit-bom</artifactId>
28 <version>5.10.1</version>
29 <type>pom</type>
30 <scope>import</scope>
31 </dependency>
32 </dependencies>
33 </dependencyManagement>
34
35 <dependencies>
36 <dependency>
37 <groupId>io.cucumber</groupId>
38 <artifactId>cucumber-java</artifactId>
39 <scope>test</scope>
40 </dependency>
41
42 <dependency>
43 <groupId>io.cucumber</groupId>
44 <artifactId>cucumber-junit-platform-engine</artifactId>
45 <scope>test</scope>
46 </dependency>
47
48 <dependency>
49 <groupId>org.junit.platform</groupId>
50 <artifactId>junit-platform-suite</artifactId>
51 <scope>test</scope>
52 </dependency>
53
54 <dependency>
55 <groupId>org.junit.jupiter</groupId>
56 <artifactId>junit-jupiter</artifactId>
57 <scope>test</scope>
58 </dependency>
59
60 <dependency>
61 <groupId>org.seleniumhq.selenium</groupId>
62 <artifactId>selenium-java</artifactId>
63 <version>4.16.1</version>
64 </dependency>
65 </dependencies>
66
67 <build>
68 <plugins>
69 <plugin>
70 <groupId>org.apache.maven.plugins</groupId>
71 <artifactId>maven-compiler-plugin</artifactId>
72 <version>3.11.0</version>
73 <configuration>
74 <encoding>UTF-8</encoding>
75 <source>17</source>
76 <target>17</target>
77 </configuration>
78 </plugin>
79 <plugin>
80 <groupId>org.apache.maven.plugins</groupId>
81 <artifactId>maven-surefire-plugin</artifactId>
82 <version>3.2.2</version>
83 </plugin>
84 </plugins>
85 </build>
86 </project>
AnExample.java
1 // src/test/java/hellocucumber/AnExample.java
2 package hellocucumber;
3
4 import io.cucumber.java.en.*;
5
6 import static org.junit.jupiter.api.Assertions.assertNotNull;
7
8 import org.junit.jupiter.api.Assertions.*;
9 import org.openqa.selenium.By;
10 import org.openqa.selenium.WebDriver;
11 import org.openqa.selenium.WebElement;
12 import org.openqa.selenium.firefox.FirefoxDriver;
13
14 public class AnExample {
15 WebDriver driver = null;
16
17 private void waitMillis(long ms) {
18 try {
19 Thread.sleep(ms);
20 } catch (InterruptedException e) {
21 e.printStackTrace();
22 }
23 }
24
25 @Given("we are at site {string}")
26 public void weAreAtRpiSite(String site) {
27 driver = new FirefoxDriver();
28 driver.navigate().to(site);
29 }
30
31 @When("we try to login with credentials")
32 public void tryToLoginWithCredentials() {
33 // mvn clean test -Duser=xxxx -Dpassword=xxxx
34 String user = System.getProperty("user");
35 String password = System.getProperty("password");
36 driver.findElement(By.name("user")).sendKeys(user);
37 driver.findElement(By.name("password")).sendKeys(password);
38 driver.findElement(By.className("btn-primary")).click();
39 }
40
41 @Then("we should be logged in and see the logout link")
42 public void shouldBeLoggedInAndSeeLogoutLink() {
43 waitMillis(10000);
44 WebElement logout = driver.findElement(By.id("logout"));
45 assertNotNull(logout);
46 logout.click();
47 waitMillis(10000);
48 driver.close();
49 }
50 }
Example.java
1 // src/test/java/hellocucumber/Example.java
2 package hellocucumber;
3
4 import io.cucumber.java.en.*;
5
6 public class Example {
7
8 @Given("an example scenario")
9 public void an_example_scenario() {
10 }
11
12 @When("all step definitions are implemented")
13 public void all_step_definitions_are_implemented() {
14 }
15
16 @Then("the scenario passes")
17 public void the_scenario_passes() {
18 }
19 }
RunCucumberTest.java
1 // src/test/java/hellocucumber/RunCucumberTest.java
2 package hellocucumber;
3
4 import org.junit.platform.suite.api.ConfigurationParameter;
5 import org.junit.platform.suite.api.IncludeEngines;
6 import org.junit.platform.suite.api.SelectClasspathResource;
7 import org.junit.platform.suite.api.Suite;
8 import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME;
9
10 @Suite
11 @IncludeEngines("cucumber")
12 @SelectClasspathResource("hellocucumber")
13 @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty")
14 public class RunCucumberTest {
15 }
anexample.feature
- src/test/resources/hellocucumber/anexample.feature
Feature: AnExample
Scenario: An example scenario
Given we are at site "<url>"
When we try to login with credentials
Then we should be logged in and see the logout link
Examples:
| url |
| https://rpi.bitarus.mooo.com/ |
example.feature
- src/test/resources/hellocucumber/example.feature
Feature: example
Scenario: example scenario
Given an example scenario
When all step definitions are implemented
Then the scenario passes