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

问题描述 投票:0回答:1

从Java类运行测试后遇到一些重复错误

我已经尝试更改/更新我的步骤和运行程序文件,但是很遗憾,这没有帮助解决我的问题。我对黄瓜和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 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 

功能文件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   

步骤:

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");
    }

}

转轮文件:

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

regex maven cucumber gherkin cucumber-java
1个回答
0
投票

[我找到了一些支持@Given的当前正则表达式语法的Cucumber示例,并且我还发现了使用替代语法的示例。代替这个:

@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");
}
© www.soinside.com 2019 - 2024. All rights reserved.