【问题标题】:Feature not identified by the *Given* tag while running Cucumber BDD tests in Geb Framework (Groovy)在 Geb Framework (Groovy) 中运行 Cucumber BDD 测试时,*Given* 标记未识别的功能
【发布时间】:2015-08-26 14:55:29
【问题描述】:

我正在尝试使用 Geb Framework 和 Cucumber 执行 Web 应用程序的自动化测试。但是,在运行我的第一个测试时,一旦程序到达“Given”标签,我就偶然发现了 NullPointerException。我在互联网上搜索了线索,但没有找到答案。我还参考了 Geb 和 Cucumber 文档,并在使用 Cucumber 时发现了许多“良好实践”示例,但我尝试调整代码并运行的每个代码示例都以相同的空指针异常结束。

我的代码的重要摘录如下:

test.groovy

import geb.Browser
import org.openqa.selenium.Keys

import static cucumber.api.groovy.EN.*

browser = new Browser()
browser.go "/"


Given(~'Home page is opened') {
    browser.to HomePage
    assert browser.at(HomePage)
}

When(~'Add offer button is clicked') {
    HomePage.addOfferLink
}

[...]

GebConfig.groovy

import org.openqa.selenium.chrome.ChromeDriver

/**
 * Created by apoteralowicz on 2015-08-24.
 */

def newDriver
    driver =
    {
        newDriver = new ChromeDriver();
        newDriver.manage().window().maximize();
        return newDriver;
    }
System.setProperty('webdriver.chrome.driver', 'src/binary/chromedriver.exe')

HomePage.groovy

import geb.Page

/**
 * Created by apoteralowicz on 2015-08-24.
 */
class HomePage extends Page {
    static url = "/";
    static at = { waitFor { title.contains('Home') } };
    static content = {

    /*Menu tab links*/
    HomeLink(to: HomePage) {
        $('.menuBarNav').find('a[href="/"]')
    };
    IntercityLink(to: IntercityPage) {
        $('.menuBarNav').find('a[href="/LiftOffer/CityOffers/0?pageSize=5"]')
    };
    EntriesLink(to: EntriesPage) {
        $('.menuBarNav').find('a[href="/LiftOffer/MyEntriesOffers/1?pageSize=5"]')
    };

    CreateDropdownButton(to: '#') {
        $('#createButton')
    };
    addOfferLink(to: AddOfferPage) {
        $('.dropdown-menu').find('a[href="/LiftOffer/Create/1"]')
    };
    addRequestLink(to: AddRequestPage) {
        $('.dropdown-menu').find('a[href="/LiftOffer/Create/2"]')
    };

    SearchLink(to: SearchOffersPage) {
        $('.menuBarNav').find('a[href="/LiftOffer/Search?type=1&pageSize=5"]')
    };
    MessageboxLink(to: MailboxPage) {
        $('.menuBarNav').find('a[href="/LiftOffer/MessageBox"]')
    };

    /*Additional Buttons*/
    ShowOffersButton(to: SearchOffersPage) {
        $('a[href="/LiftOffer/Search/?type=1"]')
    };
    ShowRequestsButton(to: SearchRequestsPage) {
        $('a[href="/LiftOffer/Search/?type=2"]')
    };
    }
}

testScript.feature

Feature: new lift offer creation
  As an user
  I want to create a lift offer for people
  So that I could inform people that I am going somewhere and I have free seats available

  Scenario:
    Given Home page is opened
    When Add Offer button is clicked
    Then User can input some data into the Create New Lift Offer form
    And User can save the offer

精确错误

Starting ChromeDriver (v2.11.298604 (75ea2fdb5c87f133a8e1b8da16f6091fb7d5321e)) on port 40529
Only local connections are allowed.
Caught: java.lang.NullPointerException
java.lang.NullPointerException
    at cucumber.api.groovy.EN.Given(EN.java:27)
    at cucumber.api.groovy.EN$Given.callStatic(Unknown Source)
    at testScript1.run(testScript1.groovy:15)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Process finished with exit code 1

总而言之,我认为问题是在某种程度上没有正确识别 Given 功能,但我不知道为什么,因此我来这里问。

【问题讨论】:

    标签: groovy selenium-webdriver cucumber bdd geb


    【解决方案1】:

    我现在知道我应该添加一个单独的类来运行测试。

    import cucumber.api.CucumberOptions
    import cucumber.api.junit.Cucumber
    import org.junit.runner.RunWith
    
    @RunWith(Cucumber.class)
    @CucumberOptions(features = "src", format = ["pretty", "html:build/reports/tests/cucumber/html", "json:build/reports/tests/cucumber.json"])
    class RunTests {
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      相关资源
      最近更新 更多