黄瓜 - Java8 - 将@Before 与 lambda 表达式结合使用
Cucumber - Java8 - using @Before with lambda expressions
我有一个使用黄瓜设置的 Java selenium 项目。步骤定义位于一组重用 BaseDefinitions 文件中的代码的文件中,如下所示:
(示例缩减为第一个@Given 语句):
public class GoogleDefinitions {
private BaseDefinitions baseDefinitions;
private WebDriver driver;
public GoogleDefinitions (BaseDefinitions baseDefinitions) {
this.baseDefinitions = baseDefinitions;
this.driver = baseDefinitions.getDriver();
}
@Given("^I visit the Google search page$")
public void iVisitTheGoogleSearchPage() {
GoogleSearchHomepage googleSearchHomepage = new GoogleSearchHomepage(driver);
googleSearchHomepage.visit();
}
}
BaseDefinitions 文件(简短版)如下所示:
public class BaseDefinitions {
private WebDriver driver;
@Before
public void setUp(Scenario scenario) throws Exception {
String path = getClass()
.getClassLoader()
.getResource("chromedriver.exe")
.getPath();
System.setProperty("webdriver.chrome.driver", path);
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@After
public void teardown() {
driver.close();
try {
driver.quit();
}
catch(Exception e) {
System.out.println("Caught driver error on quit.");
}
}
public WebDriver getDriver() {
return driver;
}
}
这工作正常 - 粘合代码等都已正确设置。但是,通过更新,Cucumber 现在生成 lambda 表达式而不是以前使用的格式。我对如何实现这些感到困惑。
- 如果我在构造函数中实现它们(如讨论的 here),则 BaseDefinitions 文件的 @Before 语句仅在 测试后执行 当我 运行 功能 - 这是没有用的。
- 如果我将它们放在一个方法中,如图 here 所示,当我 运行 特征文件时它 运行 是 @Before 语句但没有找到步骤定义。
下面是在方法中实现 lambda 的示例。当我 运行 这给了我 You can implement missing steps with the snippets below:
:
public class lambdaTestDefinitions implements En {
private BaseDefinitions baseDefinitions;
private WebDriver driver;
public lambdaTestDefinitions(BaseDefinitions baseDefinitions) {
this.baseDefinitions = baseDefinitions;
this.driver = baseDefinitions.getDriver();
}
private void testLambdaSteps () {
Given("I try to navigate to www.google.co.uk", () -> {
driver.get("http://www.google.co.uk");
});
Then("I should be on the Google home page", () -> {
// Write code here that turns the phrase above into concrete actions
Assert.assertEquals(driver.getCurrentUrl(), "http://www.google.co.uk");
});
}
}
显然我遗漏了一些东西 - 我该如何让它工作?
编辑:这是我的build.gradle。据我所知,我没有使用 Cucumber 的 Java8 版本,我也不太了解它:
buildscript {
repositories {
mavenCentral()
}
}
plugins {
id 'java'
//id "com.github.spacialcircumstances.gradle-cucumber-reporting" version "0.1.2"
}
group 'org.myorg'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
project.ext {
cucumberVersion = '4.0.0'
}
dependencies {
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.5.1'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile 'io.cucumber:cucumber-java:' + cucumberVersion
testCompile 'io.cucumber:cucumber-junit:' + cucumberVersion
testCompile group: 'com.browserstack', name: 'browserstack-local-java', version:'1.0.1'
testCompile group: 'io.cucumber', name: 'cucumber-picocontainer', version: '2.1.0'
testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
testCompile group: 'org.testng', name: 'testng', version: '6.9.10'
testCompile 'io.github.prashant-ramcharan:courgette-jvm:3.1.0'
}
test {
systemProperties System.getProperties()
ignoreFailures = true
systemProperty "localBrowser", System.getProperty("localBrowser")
systemProperty "browserstackLocal", System.getProperty("browserstackLocal")
systemProperty "browser", System.getProperty("browser")
testLogging.showStandardStreams = true
test.outputs.upToDateWhen {false}
}
This works fine - glue code etc is all set up correctly. However, with updates, Cucumber is now generating lambda expressions rather than the format used before.
您已将 cucumber-java8
而不是 cucumber-java
添加到依赖项中。
If I put them in a method, as shown here, when I run the feature file it runs the @Before statement but does not find the step definitions.
这仅适用于一些扩展 Cucumber 的框架。然而,eclipse 插件支持这两种表示法。
If I implement them in the constructor (as discussed here), then the @Before statement of the BaseDefinitions file is only executed after the test when I run the feature - this is of no use.
您混淆了黄瓜步骤的执行和步骤定义的创建。步骤定义的创建发生在对其调用任何步骤之前。 this.driver = baseDefinitions.getDriver();
在创建 GoogleDefinitions
时调用,因此总是在创建驱动程序的方法之前调用。
相反,您应该延迟调用直到调用该步骤。
public class lambdaTestDefinitions implements En {
public lambdaTestDefinitions(BaseDefinitions baseDefinitions) {
Given("I try to navigate to www.google.co.uk", () -> {
baseDefinitions.getDriver().get("http://www.google.co.uk");
});
Then("I should be on the Google home page", () -> {
// Write code here that turns the phrase above into concrete actions
assertEquals(baseDefinitions.getDriver().getCurrentUrl(), "http://www.google.co.uk");
});
}
}
您可能也有兴趣调查 PicoContainers life cycle. You can use Startable and Disposable。他们可以帮助您设置在 Cucumber 开始场景之前需要设置的东西。
我有一个使用黄瓜设置的 Java selenium 项目。步骤定义位于一组重用 BaseDefinitions 文件中的代码的文件中,如下所示:
(示例缩减为第一个@Given 语句):
public class GoogleDefinitions {
private BaseDefinitions baseDefinitions;
private WebDriver driver;
public GoogleDefinitions (BaseDefinitions baseDefinitions) {
this.baseDefinitions = baseDefinitions;
this.driver = baseDefinitions.getDriver();
}
@Given("^I visit the Google search page$")
public void iVisitTheGoogleSearchPage() {
GoogleSearchHomepage googleSearchHomepage = new GoogleSearchHomepage(driver);
googleSearchHomepage.visit();
}
}
BaseDefinitions 文件(简短版)如下所示:
public class BaseDefinitions {
private WebDriver driver;
@Before
public void setUp(Scenario scenario) throws Exception {
String path = getClass()
.getClassLoader()
.getResource("chromedriver.exe")
.getPath();
System.setProperty("webdriver.chrome.driver", path);
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@After
public void teardown() {
driver.close();
try {
driver.quit();
}
catch(Exception e) {
System.out.println("Caught driver error on quit.");
}
}
public WebDriver getDriver() {
return driver;
}
}
这工作正常 - 粘合代码等都已正确设置。但是,通过更新,Cucumber 现在生成 lambda 表达式而不是以前使用的格式。我对如何实现这些感到困惑。
- 如果我在构造函数中实现它们(如讨论的 here),则 BaseDefinitions 文件的 @Before 语句仅在 测试后执行 当我 运行 功能 - 这是没有用的。
- 如果我将它们放在一个方法中,如图 here 所示,当我 运行 特征文件时它 运行 是 @Before 语句但没有找到步骤定义。
下面是在方法中实现 lambda 的示例。当我 运行 这给了我 You can implement missing steps with the snippets below:
:
public class lambdaTestDefinitions implements En {
private BaseDefinitions baseDefinitions;
private WebDriver driver;
public lambdaTestDefinitions(BaseDefinitions baseDefinitions) {
this.baseDefinitions = baseDefinitions;
this.driver = baseDefinitions.getDriver();
}
private void testLambdaSteps () {
Given("I try to navigate to www.google.co.uk", () -> {
driver.get("http://www.google.co.uk");
});
Then("I should be on the Google home page", () -> {
// Write code here that turns the phrase above into concrete actions
Assert.assertEquals(driver.getCurrentUrl(), "http://www.google.co.uk");
});
}
}
显然我遗漏了一些东西 - 我该如何让它工作?
编辑:这是我的build.gradle。据我所知,我没有使用 Cucumber 的 Java8 版本,我也不太了解它:
buildscript {
repositories {
mavenCentral()
}
}
plugins {
id 'java'
//id "com.github.spacialcircumstances.gradle-cucumber-reporting" version "0.1.2"
}
group 'org.myorg'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
project.ext {
cucumberVersion = '4.0.0'
}
dependencies {
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.5.1'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile 'io.cucumber:cucumber-java:' + cucumberVersion
testCompile 'io.cucumber:cucumber-junit:' + cucumberVersion
testCompile group: 'com.browserstack', name: 'browserstack-local-java', version:'1.0.1'
testCompile group: 'io.cucumber', name: 'cucumber-picocontainer', version: '2.1.0'
testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
testCompile group: 'org.testng', name: 'testng', version: '6.9.10'
testCompile 'io.github.prashant-ramcharan:courgette-jvm:3.1.0'
}
test {
systemProperties System.getProperties()
ignoreFailures = true
systemProperty "localBrowser", System.getProperty("localBrowser")
systemProperty "browserstackLocal", System.getProperty("browserstackLocal")
systemProperty "browser", System.getProperty("browser")
testLogging.showStandardStreams = true
test.outputs.upToDateWhen {false}
}
This works fine - glue code etc is all set up correctly. However, with updates, Cucumber is now generating lambda expressions rather than the format used before.
您已将 cucumber-java8
而不是 cucumber-java
添加到依赖项中。
If I put them in a method, as shown here, when I run the feature file it runs the @Before statement but does not find the step definitions.
这仅适用于一些扩展 Cucumber 的框架。然而,eclipse 插件支持这两种表示法。
If I implement them in the constructor (as discussed here), then the @Before statement of the BaseDefinitions file is only executed after the test when I run the feature - this is of no use.
您混淆了黄瓜步骤的执行和步骤定义的创建。步骤定义的创建发生在对其调用任何步骤之前。 this.driver = baseDefinitions.getDriver();
在创建 GoogleDefinitions
时调用,因此总是在创建驱动程序的方法之前调用。
相反,您应该延迟调用直到调用该步骤。
public class lambdaTestDefinitions implements En {
public lambdaTestDefinitions(BaseDefinitions baseDefinitions) {
Given("I try to navigate to www.google.co.uk", () -> {
baseDefinitions.getDriver().get("http://www.google.co.uk");
});
Then("I should be on the Google home page", () -> {
// Write code here that turns the phrase above into concrete actions
assertEquals(baseDefinitions.getDriver().getCurrentUrl(), "http://www.google.co.uk");
});
}
}
您可能也有兴趣调查 PicoContainers life cycle. You can use Startable and Disposable。他们可以帮助您设置在 Cucumber 开始场景之前需要设置的东西。