Java Cucumber说0个场景

时间:2017-11-20 08:16:17

标签: java junit webdriver cucumber

我正在尝试对黄瓜进行一些测试(学习) 我有以下内容:

myapplication.feature with

Feature: test                                                                      
   Scenario: login
   Given open firefox and start app 
   When enter details    
   Then login happens

班长:

package runnerPackage;

import org.junit.runner.RunWith;
import org.testng.annotations.Test;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features="C:/Users/lucian.patrascu/workspace/tst/features/myapplication.feature",
glue={"stepdefinition"},dryRun = false)
public class Runner {
}

步骤定义:

public class test1 {

    WebDriver driver=new ChromeDriver();

    @Given("^open firefox and start app$")
    public void open_firefox_and_start_app(){
        System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
        System.out.println("worked");
    }

    @When("^enter details$")
    public void enter_details(){
        driver.navigate().to("hi5.com");
    }
    @Then("^login happens$")
    public void login(){
        driver.findElement(By.xpath("//input[@id='signInBtn'][@tabindex='3']"));
    }
}

我首先使用了TestNG和Junit库,然后我删除了TestNg和相同的结果。 我尝试了@CucumberOptions(features = ...)的各种路径,但仍然说:

0 Scenarios
0 Steps
0m0.000s

你能告诉我,因为我找不到办法解决问题

2 个答案:

答案 0 :(得分:0)

也许你可以从这个简单的项目开始,然后检查你的差异在哪里。它可能是路径中的拼写错误,或者根据您提供的输入我们无法知道的其他内容。

假设以下结构

/tmp/cuke/tst/features/myapplication.feature
/tmp/cuke/src/test/java/runnerPackage/Runner.java
/tmp/cuke/pom.xml

<强> myapplication.feature

Feature: test
    Scenario: login
        Given open firefox
        And start app
        When enter details
        Then login happens

<强> Runner.java

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

@RunWith(Cucumber.class)
@CucumberOptions(
    features="/tmp/cuke/tst/features/myapplication.feature",
    glue={"stepdefinition"},
    dryRun = false
)
public class Runner {
}

<强>的pom.xml

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.suboptimal</groupId>
    <artifactId>cuke</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>2.1.0</version>
        </dependency>
    </dependencies>
    <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>
    </properties>
</project>

运行Cucumber跑步者Runner

cd /tmp/cuke
mvn test -Dtest=Runner

结果

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running runnerPackage.Runner

1 Scenarios (1 undefined)
4 Steps (4 undefined)
0m0.021s
...

编辑基于提供的Eclipse项目。测试应该由TestNG执行。因此,黄瓜选手必须延长cucumber.api.testng.AbstractTestNGCucumberTests

假设以下结构

/tmp/cuke/src/test/java/runnerPackage/RunnerTest.java
/tmp/cuke/features/myapplication.feature
/tmp/cuke/pom.xml

<强> myapplication.feature

Feature: test
    Scenario: login
        Given open firefox
        And start app
        When enter details
        Then login happens

<强> RunnerTest.java

package runnerPackage;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;

@CucumberOptions(features = "features")
public class RunnerTest extends AbstractTestNGCucumberTests {
}

<强>的pom.xml

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.suboptimal</groupId>
    <artifactId>cuke</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>2.1.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>2.1.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <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>
        <surefire.version>2.18.1</surefire.version>
    </properties>
</project>

运行测试

cd /tmp/cuke
mvn test

结果

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running runnerPackage.RunnerTest
Configuring TestNG with:
org.apache.maven.surefire.testng.conf.TestNG652Configurator@6bf2d08e

1 Scenarios (1 undefined)
4 Steps (4 undefined)
0m0.018s
...

答案 1 :(得分:0)

请删除选项 dryRun = false

谢谢