2017-04-02 193 views
2

我正在爲我的Selenium Java + TestNG項目嘗試Cucumber。 一旦我用空的stepdefinition運行.feature文件,我應該得到一個片段,我可以將粘貼複製到我的步驟定義類。但是我得到了一種奇怪的格式,不能用於開箱即用。來自Cucumber Java TestNG的片段看起來很奇怪

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>

回答

2

您正在使用黃瓜java8依賴,下面是用於stepDefintion文件格式(請確保您的Java編譯器版本應該是1.8)。

@Given註釋將在黃瓜的Java

定的註釋將在黃瓜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/

相關問題