来自 Cucumber Java 的代码片段看起来很奇怪
Snippet from Cucumber Java TestNG looks strange
我正在为我的 Selenium Java + TestNG 项目尝试 Cucumber。
一旦我 运行 具有空步骤定义的 .feature 文件,我应该得到一个片段,我可以将其复制粘贴到我的步骤定义 class。但是我得到了一种无法开箱即用的奇怪格式。
Given
应该是 @Given
吧?
没有 public void()
.
可能是什么原因?谢谢。
2 Scenarios (2 undefined)
5 Steps (5 undefined)
0m0.000s
You can implement missing steps with the snippets below:
Given("^User is on Home Page$", () -> {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
});
When("^User enters UserName and Password$", () -> {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
});
Then("^He can visit the practice page$", () -> {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
});
When("^User LogOut from the Application$", () -> {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
});
Then("^he cannot visit practice page$", () -> {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
});
这是我的glue file
package glue;
import java.io.IOException;
import org.testng.annotations.Test;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import cucumber.api.testng.TestNGCucumberRunner;
@Test(groups = "cucumber")
@CucumberOptions(features = "C:\Users\workspace\cucumber\src\test\java\feature\logintest.feature",
glue = "C:\Users\workspace\cucumber\src\test\java\glue\Glue.java",
plugin = {"html:C:\Users\workspace\cucumber\logs"})
public class Glue extends AbstractTestNGCucumberTests {
@Test(groups = "cucumber", description = "Runs Cucumber Features")
public void runCukes() throws IOException {
}
}
这是我的 .feature
文件
Feature: Login
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User enters UserName and Password
Then He can visit the practice page
Scenario: Successful LogOut
When User LogOut from the Application
Then he cannot visit practice page
这是我的依赖项
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-testng -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/gherkin -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.12.2</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java8</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
您正在使用 cucumber-java8 依赖项,因为以下是 stepDefintion 文件的格式(请确保您的 java 编译器版本应为 1.8)。
@Given Annotation会在cucumber-java
给定的注释将在 cucumber-java8
import cucumber.api.java8.En;
public class stepDefinitions implements En{
public stepDefinitions(){
Given("^User is on Home Page$", () -> {
// Write code here that turns the phrase above into concrete actions
System.out.println("Test1");
});
When("^User enters UserName and Password$", () -> {
// Write code here that turns the phrase above into concrete actions
System.out.println("Test2");
});
Then("^He can visit the practice page$", () -> {
// Write code here that turns the phrase above into concrete actions
System.out.println("Test3");
});
When("^User LogOut from the Application$", () -> {
// Write code here that turns the phrase above into concrete actions
System.out.println("Test4");
});
Then("^he cannot visit practice page$", () -> {
// Write code here that turns the phrase above into concrete actions
System.out.println("Test5");
});
}
}
我在我的机器上执行了它,它工作正常,试试看,如果它适合你,请告诉我。
我正在为我的 Selenium Java + TestNG 项目尝试 Cucumber。 一旦我 运行 具有空步骤定义的 .feature 文件,我应该得到一个片段,我可以将其复制粘贴到我的步骤定义 class。但是我得到了一种无法开箱即用的奇怪格式。
Given
应该是 @Given
吧?
没有 public void()
.
可能是什么原因?谢谢。
2 Scenarios (2 undefined)
5 Steps (5 undefined)
0m0.000s
You can implement missing steps with the snippets below:
Given("^User is on Home Page$", () -> {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
});
When("^User enters UserName and Password$", () -> {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
});
Then("^He can visit the practice page$", () -> {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
});
When("^User LogOut from the Application$", () -> {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
});
Then("^he cannot visit practice page$", () -> {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
});
这是我的glue file
package glue;
import java.io.IOException;
import org.testng.annotations.Test;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import cucumber.api.testng.TestNGCucumberRunner;
@Test(groups = "cucumber")
@CucumberOptions(features = "C:\Users\workspace\cucumber\src\test\java\feature\logintest.feature",
glue = "C:\Users\workspace\cucumber\src\test\java\glue\Glue.java",
plugin = {"html:C:\Users\workspace\cucumber\logs"})
public class Glue extends AbstractTestNGCucumberTests {
@Test(groups = "cucumber", description = "Runs Cucumber Features")
public void runCukes() throws IOException {
}
}
这是我的 .feature
文件
Feature: Login
Scenario: Successful Login with Valid Credentials
Given User is on Home Page
When User enters UserName and Password
Then He can visit the practice page
Scenario: Successful LogOut
When User LogOut from the Application
Then he cannot visit practice page
这是我的依赖项
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-testng -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>1.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/gherkin -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.12.2</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java8</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
您正在使用 cucumber-java8 依赖项,因为以下是 stepDefintion 文件的格式(请确保您的 java 编译器版本应为 1.8)。
@Given Annotation会在cucumber-java
给定的注释将在 cucumber-java8
import cucumber.api.java8.En;
public class stepDefinitions implements En{
public stepDefinitions(){
Given("^User is on Home Page$", () -> {
// Write code here that turns the phrase above into concrete actions
System.out.println("Test1");
});
When("^User enters UserName and Password$", () -> {
// Write code here that turns the phrase above into concrete actions
System.out.println("Test2");
});
Then("^He can visit the practice page$", () -> {
// Write code here that turns the phrase above into concrete actions
System.out.println("Test3");
});
When("^User LogOut from the Application$", () -> {
// Write code here that turns the phrase above into concrete actions
System.out.println("Test4");
});
Then("^he cannot visit practice page$", () -> {
// Write code here that turns the phrase above into concrete actions
System.out.println("Test5");
});
}
}
我在我的机器上执行了它,它工作正常,试试看,如果它适合你,请告诉我。