[英]Cucumber parsing strings
I'm learning how to use cucumber in java and selenium webdriver. 我正在学习如何在Java和Selenium Webdriver中使用黄瓜。 I have the architecture in place and I'm down to one very specific problem.
我拥有适当的体系结构,而我要解决一个非常具体的问题。
@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));
}
I don't understand why this code works, and when I leave out the if statements I get a null pointer exception, like this: 我不明白为什么这段代码有效,当我忽略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));
}
gets me: 让我:
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)
When I put back the if statements it runs like I would expect, so I know that arg1, arg2 and arg3 strings are actually strings and can be used. 当我放回if语句时,它会像我期望的那样运行,因此我知道arg1,arg2和arg3字符串实际上是字符串,可以使用。
Why won't it let me use them in my login method and I have to re-set the strings in an ugly fashion? 为什么不让我在登录方法中使用它们,而不得不以丑陋的方式重新设置字符串?
edit 1: 编辑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);
complete code on github: https://github.com/Yourtestprofessionals/YourCucumber 完整代码在github上: https : //github.com/Yourtestprofessionals/YourCucumber
edit2: cheers for the replies so far. edit2:到目前为止,为答复打气。 my problem is still not solved though.
我的问题仍然没有解决。
EDIT 编辑
Here is my modified pom.xml. 这是我修改过的pom.xml。 I went back to selenium-java version 2.53.1.
我回到了硒Java版本2.53.1。 I also added surefire and specified Java 1.8 (Oracle).
我还添加了surefire并指定了Java 1.8(Oracle)。 Try this pom and let me know.
试试这个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>
I last point. 我最后一点。 I am using ChromeDriver 2.25.426923.
我正在使用ChromeDriver 2.25.426923。
EDIT 2 编辑2
I modified your runner. 我修改了您的跑步者。 I replaced format with plugin.
我用插件替换了格式。
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 {
}
I would be surprised if this would actually solve your problem but trying won't hurt; 如果这确实可以解决您的问题,但尝试不会有任何伤害,我会感到惊讶。 have you tried using the regex 'wildcard': '.*'?
您是否尝试过使用正则表达式“通配符”:“。*”?
Like this: 像这样:
@When("^I login with the parameters \"(.*)\" and \"(.*)\" and \"(.*)\"")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.