cucumber.runtime.CucumberException:java.util.regex.PatternSyntaxException:索引 39 附近的非法重复

cucumber.runtime.CucumberException: java.util.regex.PatternSyntaxException: Illegal repetition near index 39

在 Java 运行 测试后面临一些重复错误 class

我已经尝试 change/update 我的步骤和跑步者文件,但不幸的是这并没有帮助解决我的问题。我对 Cucumber 和 Maven 很陌生,所以如果我提供的信息较少,请告诉我。

特征文件 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 Whosebug 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 

专题文件 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 Whosebug 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   

步骤:

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 Whosebug website$")
    public void user_navigates_to_Whosebug_website() {
        System.out.println("user_navigates_to_Whosebug_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 Whosebug website{int}$")
    public void user_navigates_to_Whosebug_website(Integer int1) {
        System.out.println("user_navigates_to_Whosebug_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");
    }

}

跑步者文件:

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 文件:

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

我在控制台中得到以下结果:

cucumber.runtime.CucumberException: java.util.regex.PatternSyntaxException: Illegal repetition near index 39
^User navigates to Whosebug 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 Whosebug 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

Cucumber 很可能将 {int} 解释为量词。虽然我发现了一些 Cucumber 示例支持您当前使用 @Given 的正则表达式语法,但我还发现了使用替代语法的示例。而不是这个:

@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 username(\d+)$")
public void user_enters_a_valid_username(Integer int1) {
    System.out.println("user_enters_a_valid_username2");
}

您可以用没有数字的步骤替换您的步骤名称,例如“给定用户导航到 Whosebug 第二个网站”。

@Given("^User navigates to Whosebug second website$")
public void userNavigatesToWhosebugSecondWebsite() {
}

如果你想使用可验证的,你可以这样使用:

@Given("^User navigates to Whosebug website(\d+)$")
public void userNavigatesToWhosebugWebsite(int arg0) {
}

大括号是重复量词 {n, m} 的开始和结束标记。这就是抛出异常 Illegal repetition 的原因。

无需为参数不同的同一步骤创建 2 个不同的步骤定义。

我建议合并以下步骤定义:

 @Given("^User navigates to Whosebug website$")
public void user_navigates_to_Whosebug_website() {
    System.out.println("user_navigates_to_Whosebug_website");
}

@Given("^User navigates to Whosebug website{int}$")
public void user_navigates_to_Whosebug_website(Integer int1) {
    System.out.println("user_navigates_to_Whosebug_website2");
}

进入

@Given("^User navigates to Whosebug \"([^\"]*)\"$")
public void userNavigatesToWhosebug(String arg0) {
    // Write code here that turns the phrase above into concrete actions
}

相应的小黄瓜步骤将是:

Given User navigates to Whosebug "website"

这样您将在两种场景中使用相同的步骤定义,只是提供的参数不同。它将避免重复。 您需要按照此模式修改所有步骤..

在您的情况下,您可以使用场景大纲:

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 Whosebug "<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 |