问题
Facing some repetition errors after running tests from Java class
I have tried to change/update my steps and runner file but unfortunately this did not help to solve my problem. I'm quite new in cucumber and maven so if the information that I've provided is less please let me know.
Feature file 1:
Feature: Login into account
Existing user should be able to login account using correct credentials
Scenario: Login into account with correct credentials
Given User navigates to stackoverflow website
And User clicks on the login button on homepage
And User enters a valid username
And User enters a valid password
When User clicks on the login button
Then User should be taken to the succesful login page
Feature file 2
Feature: Login into account
Existing user should be able to login account using correct credentials
Scenario: Login into account with correct credentials
Given User navigates to stackoverflow website2
And User clicks on the login button on homepage2
And User enters a valid username2
And User enters a valid password2
When User clicks on the login button2
Then User should be taken to the succesful login page2
Steps:
package CucumberFramework.steps;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;
import cucumber.api.java.en.Then;
public class LoginSteps {
@Given("^User navigates to stackoverflow website$")
public void user_navigates_to_stackoverflow_website() {
System.out.println("user_navigates_to_stackoverflow_website");
}
@Given("^User clicks on the login button on homepage$")
public void user_clicks_on_the_login_button_on_homepage() {
System.out.println("user_clicks_on_the_login_button_on_homepage");
}
@Given("^User enters a valid username$")
public void user_enters_a_valid_username() {
System.out.println("user_enters_a_valid_username");
}
@Given("^User enters a valid password$")
public void user_enters_a_valid_password() {
System.out.println("user_enters_a_valid_password");
}
@When("^User clicks on the login button$")
public void user_clicks_on_the_login_button() {
System.out.println("user_clicks_on_the_login_button");
}
@Then("^User should be taken to the succesful login page$")
public void user_should_be_taken_to_the_succesful_login_page() {
System.out.println("user_should_be_taken_to_the_succesful_login_page");
}
@Given("^User navigates to stackoverflow website{int}$")
public void user_navigates_to_stackoverflow_website(Integer int1) {
System.out.println("user_navigates_to_stackoverflow_website2");
}
@Given("^User clicks on the login button on homepage{int}$")
public void user_clicks_on_the_login_button_on_homepage(Integer int1) {
System.out.println("user_clicks_on_the_login_button_on_homepage2");
}
@Given("^User enters a valid username{int}$")
public void user_enters_a_valid_username(Integer int1) {
System.out.println("user_enters_a_valid_username2");
}
@Given("^User enters a valid password{int}$")
public void user_enters_a_valid_password(Integer int1) {
System.out.println("user_enters_a_valid_password2");
}
@When("^User clicks on the login button{int}$")
public void user_clicks_on_the_login_button(Integer int1) {
System.out.println("user_clicks_on_the_login_button2");
}
@Then("^User should be taken to the succesful login page{int}$")
public void user_should_be_taken_to_the_succesful_login_page(Integer int1) {
System.out.println("user_should_be_taken_to_the_succesful_login_page2");
}
}
Runner file:
package CucumberFramework.runner;
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
import cucumber.api.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions (
features = {"src/test/java/CucumberFramework/features/"},
glue = {"CucumberFramework.steps"},
monochrome = true,
tags = {},
plugin = {"pretty", "html:target/cucumber", "json:target/cucumber.json", "com.cucumber.listener.ExtentCucumberFormatter: target/report.html"}
)
public class MainRunner {
}
POM file:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.webdriveruniversity</groupId>
<artifactId>CucumberFramework</artifactId>
<version>0.0.21-SNAPSHOT</version>
<packaging>jar</packaging>
<name>CucumberFramework</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
<!--<executable>C:\Program Files\Java\jdk1.8.0_121\bin\javac.exe</executable> -->
<executable>${env.JAVA_HOME}\bin\javac.exe</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<testErrorIgnore>false</testErrorIgnore>
<testFailureIgnore>false</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
</reporting>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>1.2.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-core -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-html -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-html</artifactId>
<version>0.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-jvm-deps -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm-deps</artifactId>
<version>1.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/gherkin -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.12.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-picocontainer -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-testng -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>1.2.5</version>
</dependency>
<!-- Extent Reports -->
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>3.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.26-incubating</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.masterthought/cucumber-reporting -->
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>com.vimalselvam</groupId>
<artifactId>cucumber-extentsreport</artifactId>
<version>2.0.4</version>
</dependency>
</dependencies>
</project>
I get following outcome in my console:
cucumber.runtime.CucumberException: java.util.regex.PatternSyntaxException: Illegal repetition near index 39
^User navigates to stackoverflow website{int}$
^
at cucumber.runtime.java.JavaBackend.addStepDefinition(JavaBackend.java:156)
at cucumber.runtime.java.MethodScanner.scan(MethodScanner.java:68)
at cucumber.runtime.java.MethodScanner.scan(MethodScanner.java:41)
at cucumber.runtime.java.JavaBackend.loadGlue(JavaBackend.java:86)
at cucumber.runtime.Runtime.<init>(Runtime.java:92)
at cucumber.runtime.Runtime.<init>(Runtime.java:70)
at cucumber.runtime.Runtime.<init>(Runtime.java:66)
at cucumber.api.junit.Cucumber.createRuntime(Cucumber.java:80)
at cucumber.api.junit.Cucumber.<init>(Cucumber.java:59)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:90)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:49)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: java.util.regex.PatternSyntaxException: Illegal repetition near index 39
^User navigates to stackoverflow website{int}$
^
at java.base/java.util.regex.Pattern.error(Pattern.java:2015)
at java.base/java.util.regex.Pattern.closure(Pattern.java:3308)
at java.base/java.util.regex.Pattern.sequence(Pattern.java:2201)
at java.base/java.util.regex.Pattern.expr(Pattern.java:2056)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1778)
at java.base/java.util.regex.Pattern.<init>(Pattern.java:1427)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1068)
at cucumber.runtime.java.JavaBackend.pattern(JavaBackend.java:203)
at cucumber.runtime.java.JavaBackend.addStepDefinition(JavaBackend.java:151)
... 25 more
回答1:
Most likely Cucumber is interpreting {int}
as a quantifier. While I found some Cucumber examples which support your current regex syntax with @Given
, and I also found examples which use an alternative syntax. Instead of this:
@Given("^User enters a valid username{int}$")
public void user_enters_a_valid_username(Integer int1) {
System.out.println("user_enters_a_valid_username2");
}
use this:
@Given("^User enters a valid username(\\d+)$")
public void user_enters_a_valid_username(Integer int1) {
System.out.println("user_enters_a_valid_username2");
}
回答2:
You can replace your step name with a step without numbers like " Given User navigates to stackoverflow second website".
@Given("^User navigates to stackoverflow second website$")
public void userNavigatesToStackoverflowSecondWebsite() {
}
If you want to use a veriable, u can use like this:
@Given("^User navigates to stackoverflow website(\\d+)$")
public void userNavigatesToStackoverflowWebsite(int arg0) {
}
回答3:
The curly brackets are the opening and closing tokens for the repetition quantifier {n, m}. That's why the exception Illegal repetition is thrown.
There is no need to create 2 different step definitions for one and the same step which parameter is different.
I suggest to combine the following step definitions:
@Given("^User navigates to stackoverflow website$")
public void user_navigates_to_stackoverflow_website() {
System.out.println("user_navigates_to_stackoverflow_website");
}
and
@Given("^User navigates to stackoverflow website{int}$")
public void user_navigates_to_stackoverflow_website(Integer int1) {
System.out.println("user_navigates_to_stackoverflow_website2");
}
INTO
@Given("^User navigates to stackoverflow \"([^\"]*)\"$")
public void userNavigatesToStackoverflow(String arg0) {
// Write code here that turns the phrase above into concrete actions
}
The corresponding gherkin step will be:
Given User navigates to stackoverflow "website"
This way you will use the same step definition into both scenario and only the provided parameter will different. It will avoid the repetition. You need to modify all your steps following this pattern..
In your case you could use Scenario Outline:
Feature: Login into account
Existing user should be able to login account using correct credentials
Scenario Outline: Login into account with correct credentials
Given User navigates to stackoverflow "<website>"
And User clicks on the login button on "<homepage>"
And User enters a valid "<username>"
And User enters a valid "<password>"
When User clicks on the login "<button>"
Then User should be taken to the succesful login "<page>"
Examples:
| website | homepage | username | password | button | page |
| website1 | homepage1 | username1 | password1 | button1 | page1 |
| website2 | homepage2 | username2 | password2 | button2 | page2 |
来源:https://stackoverflow.com/questions/58482340/cucumber-runtime-cucumberexception-java-util-regex-patternsyntaxexception-ille