来自Cucumber Java TestNG的片段看起来很奇怪

时间:2017-04-02 01:15:39

标签: java selenium cucumber testng bdd

我正在为我的Selenium Java + TestNG项目尝试Cucumber。 一旦我使用空stepdefinition运行.feature文件,我应该得到一个片段,我可以将粘贴复制到我的步骤定义类。但我得到了一种奇怪的格式,无法开箱即用。

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>

1 个答案:

答案 0 :(得分:2)

你正在使用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");
    });
   }
 }

我在我的机器上执行了它的工作,尝试并让我知道它是否适合你。

链接供参考: http://codoid.com/cucumber-lambda-expressions/

相关问题