cucumber

Example project

   1 mvn archetype:generate                      \
   2    "-DarchetypeGroupId=io.cucumber"           \
   3    "-DarchetypeArtifactId=cucumber-archetype" \
   4    "-DarchetypeVersion=7.14.1"               \
   5    "-DgroupId=hellocucumber"                  \
   6    "-DartifactId=hellocucumber"               \
   7    "-Dpackage=hellocucumber"                  \
   8    "-Dversion=1.0.0-SNAPSHOT"                 \
   9    "-DinteractiveMode=false"

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>

src/test/java/hellocucumber/AnExample.java

   1 package hellocucumber;
   2 
   3 import io.cucumber.java.en.*;
   4 
   5 import static org.junit.jupiter.api.Assertions.assertNotNull;
   6 
   7 import org.junit.jupiter.api.Assertions.*;
   8 import org.openqa.selenium.By;
   9 import org.openqa.selenium.WebDriver;
  10 import org.openqa.selenium.WebElement;
  11 import org.openqa.selenium.firefox.FirefoxDriver;
  12 
  13 public class AnExample {
  14     WebDriver driver = null;
  15 
  16     private void waitMillis(long ms) {
  17         try {
  18             Thread.sleep(ms);
  19         } catch (InterruptedException e) {
  20             e.printStackTrace();
  21         }
  22     }
  23 
  24     @Given("we are at site {string}")
  25     public void weAreAtRpiSite(String site) {
  26         driver = new FirefoxDriver();
  27         driver.navigate().to(site);
  28     }
  29 
  30     @When("we try to login with credentials")
  31     public void tryToLoginWithCredentials() {
  32         //  mvn clean test -Duser=xxxx -Dpassword=xxxx
  33         String user =  System.getProperty("user");
  34         String password =  System.getProperty("password");
  35         driver.findElement(By.name("user")).sendKeys(user);
  36         driver.findElement(By.name("password")).sendKeys(password);
  37         driver.findElement(By.className("btn-primary")).click();
  38     }
  39 
  40     @Then("we should be logged in and see the logout link")
  41     public void shouldBeLoggedInAndSeeLogoutLink() {
  42         waitMillis(10000);
  43         WebElement logout = driver.findElement(By.id("logout"));
  44         assertNotNull(logout);
  45         logout.click();
  46         waitMillis(10000);
  47         driver.close();
  48     }
  49 }

src/test/java/hellocucumber/Example.java

   1 package hellocucumber;
   2 
   3 import io.cucumber.java.en.*;
   4 
   5 public class Example {
   6 
   7     @Given("an example scenario")
   8     public void an_example_scenario() {
   9     }
  10 
  11     @When("all step definitions are implemented")
  12     public void all_step_definitions_are_implemented() {
  13     }
  14 
  15     @Then("the scenario passes")
  16     public void the_scenario_passes() {
  17     }
  18 }

src/test/java/hellocucumber/RunCucumberTest.java

   1 package hellocucumber;
   2 
   3 import org.junit.platform.suite.api.ConfigurationParameter;
   4 import org.junit.platform.suite.api.IncludeEngines;
   5 import org.junit.platform.suite.api.SelectClasspathResource;
   6 import org.junit.platform.suite.api.Suite;
   7 import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME;
   8 
   9 @Suite
  10 @IncludeEngines("cucumber")
  11 @SelectClasspathResource("hellocucumber")
  12 @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty")
  13 public class RunCucumberTest {
  14 }

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/ |

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

cucumber (last edited 2023-12-10 13:18:27 by vitor)