1
votes

cucumber.runtime.CucumberException: java.util.regex.PatternSyntaxException: répétition illégale près de l'index 39

Face à des erreurs de répétition après avoir exécuté des tests à partir de la classe Java

J'ai essayé de modifier / mettre à jour mes étapes et mon fichier runner mais malheureusement cela n'a pas aidé à résoudre mon problème. Je suis assez nouveau dans le concombre et le maven, donc si les informations que j'ai fournies sont moins, faites-le moi savoir.

Fichier d'entités 1:

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

Fichier d'entités 2

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

Étapes:

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 {

}

Fichier Runner:

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

}


1 commentaires

Je pense qu'il essaie d'utiliser {int} comme quantificateur .


3 Réponses :


1
votes

Il est fort probable que Cucumber interprète {int} comme un quantificateur. Bien que j'ai trouvé des exemples de Cucumber qui prennent en charge votre syntaxe regex actuelle avec @Given , et j'ai également trouvé des exemples qui utilisent une syntaxe alternative. Au lieu de ceci:

@Given("^User enters a valid username(\\d+)$")
public void user_enters_a_valid_username(Integer int1) {
    System.out.println("user_enters_a_valid_username2");
}

utilisez ceci:

@Given("^User enters a valid username{int}$")
public void user_enters_a_valid_username(Integer int1) {
    System.out.println("user_enters_a_valid_username2");
}


1 commentaires

Merci beaucoup Tim. Cette solution a résolu le problème.



0
votes

Vous pouvez remplacer le nom de votre étape par une étape sans chiffres comme "L'utilisateur donné accède au deuxième site Web de stackoverflow".

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

Si vous voulez utiliser un veriable, vous pouvez utiliser comme ceci:

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


0 commentaires

0
votes

Les accolades sont les jetons d'ouverture et de fermeture du quantificateur de répétition {n, m}. C'est pourquoi l'exception Répétition illégale est lancée.

Il n'est pas nécessaire de créer 2 définitions d'étape différentes pour une seule et même étape dont le paramètre est différent.

Je suggère de combiner les définitions d'étape suivantes:

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 |

et

Given User navigates to stackoverflow "website"

INTO p>

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

L'étape de cornichon correspondante sera:

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

De cette façon, vous utiliserez la même définition d'étape dans les deux scénarios et uniquement le paramètre fourni sera différent. Cela évitera la répétition. Vous devez modifier toutes vos étapes en suivant ce modèle.

Dans votre cas, vous pouvez utiliser le plan du scénario:

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


0 commentaires