在Geb框架(Groovy)中运行Cucumber BDD测试时,未被*Given*标签标识的特性



我正在尝试使用Geb框架与Cucumber执行web应用程序的自动化测试。然而,在运行我的第一个测试时,当程序到达"给定"标记时,我偶然发现了一个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

总而言之,我认为问题在于给定的特性没有被正确识别,但我不知道为什么,因此我来到这里询问。

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

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 {
}

最新更新