cucumber.runtime.CucumberException: java.util.regex.PatternS



从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 {
}

聚甲醛文件:

<?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 FilesJavajdk1.8.0_121binjavac.exe</executable> -->
<executable>${env.JAVA_HOME}binjavac.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

最有可能的是,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");
}

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

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

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

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

@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 navigates to stackoverflow "([^"]*)"$")
public void userNavigatesToStackoverflow(String arg0) {
// Write code here that turns the phrase above into concrete actions
}

相应的小黄瓜步骤将是:

Given User navigates to stackoverflow "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 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 |

您可以将步骤名称替换为不带数字的步骤,例如" 给定用户导航到堆栈溢出第二个网站"。

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

如果要使用可验证的,可以像这样使用:

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

最新更新