我正在创建一个简单的Selenium Cucumber项目,并使用“ Lambda Expressions Constructor”方法为功能文件定义了步骤,但是当我运行CucumberTest类时,却遇到了失败异常
有未定义的步骤!
我的StepDefinition低于1
CucumberRunner类如下:
所以请建议我,如果我使用Lambda表达式,可以使用任何其他方法来调用“步骤定义文件”吗?
答案 0 :(得分:1)
正如我在评论中已经提到的那样。选项glue
需要一个软件包名称列表,而不是目录。更改为
glue = {"src/test/java/my.project.automation.wolfram_alpha" }
到
glue = {"my.project.automation.wolfram_alpha" }
将解决问题。
在下面找到工作片段。假设以下结构
src/test/java/my/project/automation/wolfram_alpha/StepDef.java
src/test/java/my/project/automation/wolfram_alpha/cucumberTest.java
src/test/resources/wolfram.feature
pom.xml
pom.xml (依赖关系部分)
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<version.cucumber>3.0.2</version.cucumber>
</properties>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java8</artifactId>
<version>${version.cucumber}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${version.cucumber}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>${version.cucumber}</version>
<scope>test</scope>
</dependency>
</dependencies>
cucumberTest.java
package my.project.automation.wolfram_alpha;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
@CucumberOptions(
features = { "src/test/resources/wolfram.feature" },
glue = {"my.project.automation.wolfram_alpha" }
)
public class cucumberTest extends AbstractTestNGCucumberTests {
}
StepDef.java
package my.project.automation.wolfram_alpha;
import cucumber.api.java8.En;
public class StepDef implements En {
public StepDef() {
Given("URL of WolframAlpha" , () -> {
System.out.println("Given URL of WolframAlpha");
});
When("user logged in as {string} with {string}" , (String user, String password) -> {
System.out.printf("When user logged in as {%s} with {%s}%n", user, password);
});
And("login is successful" , () -> {
System.out.println("And login is successful");
});
And("user search for a {string}" , (String topic) -> {
System.out.printf("And user search for a {%s}%n", topic);
});
Then("results are displayed in a creative way" , () -> {
System.out.println("Then results are displayed in a creative way");
});
}
}
问题中的wolfram.feature
使用mvn test
运行测试会产生以下输出。
Running my.project.automation.wolfram_alpha.cucumberTest
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@726f3b58
Given URL of WolframAlpha
When user logged in as {user} with {password}
And login is successful
And user search for a {IDOL}
Then results are displayed in a creative way
1 Scenarios (1 passed)
5 Steps (5 passed)