【问题标题】:Cucumber - Java8 - using @Before with lambda expressionsCucumber - Java8 - 使用 @Before 和 lambda 表达式
【发布时间】:2019-06-25 21:27:57
【问题描述】:

我有一个使用 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}
}

【问题讨论】:

    标签: java selenium cucumber


    【解决方案1】:

    这工作正常 - 胶水代码等都设置正确。但是,随着更新,Cucumber 现在生成 lambda 表达式,而不是以前使用的格式。

    您已将 cucumber-java8 而不是 cucumber-java 添加到您的依赖项中。

    如果我将它们放入一个方法中,如此处所示,当我运行功能文件时,它会运行 @Before 语句,但找不到步骤定义。

    这只适用于一些扩展 Cucumber 的框架。然而,eclipse 插件支持这两种表示法。

    如果我在构造函数中实现它们(如此处所讨论的),那么 BaseDefinitions 文件的 @Before 语句仅在我运行该功能时在测试之后执行 - 这是没有用的。

    您将黄瓜步骤的执行与步骤定义的创建混淆了。步骤定义的创建发生在对其调用任何步骤之前。 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。您可以使用StartableDisposable。他们可以帮助您设置在 Cucumber 开始场景之前需要设置的东西。

    【讨论】:

    • 非常感谢。将驱动程序调用添加到步骤中已解决此问题。奇怪的是(见上面的编辑)我的 build.gradle 没有指定 cucumber-java8 (据我所知,无论如何),我不确定它为什么使用它。也感谢您指出 PicoContainers 阅读,这可能是一个更好的方法。
    • 更新:cucumber-java8 通过未知路径潜入我的项目依赖项。我已将其删除,并且正在获取旧注释 Cucumber sn-ps。但是,现在这给了我两种实现相同目标的方法,这很棒。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    相关资源
    最近更新 更多