繁体   English   中英

黄瓜解析字符串

[英]Cucumber parsing strings

我正在学习如何在Java和Selenium Webdriver中使用黄瓜。 我拥有适当的体系结构,而我要解决一个非常具体的问题。

 @When("^I login with the parameters \"([^\"]*)\" and \"([^\"]*)\" and \"  ([^\"]*)\"")

    public void I_login_with_the_parameters(String arg1, String arg2, String arg3) throws Throwable{ 

        if (arg1.equals("PRO")) {
            arg1 = "PRO";
        }
        if (arg2.equals("testleader")) {
            arg2 = "testleader";
        }
        if (arg3.equals("Chrome")) {
            arg3 = "Chrome";
        }
        assertTrue (codebase.Beoordeling_Login.correctinloggen(arg1, arg2, arg3));

    }

我不明白为什么这段代码有效,当我忽略if语句时,会得到一个空指针异常,如下所示:

 @When("^I login with the parameters \"([^\"]*)\" and \"([^\"]*)\" and \"([^\"]*)\"")

public void I_login_with_the_parameters(String arg1, String arg2, String arg3) throws Throwable{ 

    assertTrue (codebase.Beoordeling_Login.correctinloggen(arg1, arg2, arg3));

}

让我:

 java.lang.NullPointerException
 at codebase.LoginPortal.inloggen(LoginPortal.java:14)
 at codebase.Beoordeling_Login.correctinloggen(Beoordeling_Login.java:12)
 at steps.InloggenSteps.I_login_with_the_parameters(InloggenSteps.java:32)
 at ✽.When I login with the parameters "PRO" and "testleader" and "Chrome"    (featurefiles/inloggen.feature:9)

当我放回if语句时,它会像我期望的那样运行,因此我知道arg1,arg2和arg3字符串实际上是字符串,可以使用。

为什么不让我在登录方法中使用它们,而不得不以丑陋的方式重新设置字符串?

编辑1:

public class LoginPortal { public WebDriver inloggen(String Omgeving, String Rol, String Browser) { 
String InlogUrl = null; WebDriver driver = anroepDriver.roepdriver(Browser); 
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

完整代码在github上: https : //github.com/Yourtestprofessionals/YourCucumber

edit2:到目前为止,为答复打气。 我的问题仍然没有解决。

编辑

这是我修改过的pom.xml。 我回到了硒Java版本2.53.1。 我还添加了surefire并指定了Java 1.8(Oracle)。 试试这个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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.czeczotka.bdd</groupId>
<artifactId>cucumber-jvm-maven</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>cucumber-jvm-maven</name>
<url>http://maven.apache.org</url>

<dependencies>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.5</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.53.1</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>2.53.1</version>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <additionalClasspathElements>
                        <!-- ${basedir} needed in classpath so that log4j can find the log4j.properties file --> 
                        <additionalClasspathElement>${basedir}</additionalClasspathElement>
                    </additionalClasspathElements>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>2.19.1</version>
                    </dependency>
                </dependencies>
            </plugin>

            <!-- Specifying the maven-compiler-plugin allows us to set default JDK -->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

我最后一点。 我正在使用ChromeDriver 2.25.426923。

编辑2

我修改了您的跑步者。 我用插件替换了格式。

package runner;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
    plugin = {"pretty", "html:target/cucumber"},
    glue = "steps",
    features = "classpath:featurefiles/nieuwe_bevinding.feature"
)
public class RunInloggerTest {
}

如果这确实可以解决您的问题,但尝试不会有任何伤害,我会感到惊讶。 您是否尝试过使用正则表达式“通配符”:“。*”?

像这样:

@When("^I login with the parameters \"(.*)\" and \"(.*)\" and \"(.*)\"")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM