在 Geb Framework 中进行 运行 Cucumber BDD 测试时 *Given* 标记未识别的功能 (Groovy)
Feature not identified by the *Given* tag while running Cucumber BDD tests in Geb Framework (Groovy)
我正在尝试使用 Geb Framework 和 Cucumber 执行 Web 应用程序的自动化测试。但是,在 运行 我的第一个测试中,当程序到达 'Given' 标记时,我偶然发现了 NullPointerException。我在 Internet 上搜索了线索,但没有找到答案。我还参考了 Geb 和 Cucumber 文档,并在使用 Cucumber 时发现了许多 'good practice' 的示例,但是我尝试将我的代码调整到的每个代码示例和 运行 都在同一个空指针中结束异常。
我的代码的重要摘录如下:
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 特征没有被正确识别,但我不知道为什么,所以我来这里问。
我现在知道我应该在 运行 测试中添加一个单独的 class。
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 {
}
我正在尝试使用 Geb Framework 和 Cucumber 执行 Web 应用程序的自动化测试。但是,在 运行 我的第一个测试中,当程序到达 'Given' 标记时,我偶然发现了 NullPointerException。我在 Internet 上搜索了线索,但没有找到答案。我还参考了 Geb 和 Cucumber 文档,并在使用 Cucumber 时发现了许多 'good practice' 的示例,但是我尝试将我的代码调整到的每个代码示例和 运行 都在同一个空指针中结束异常。
我的代码的重要摘录如下:
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 特征没有被正确识别,但我不知道为什么,所以我来这里问。
我现在知道我应该在 运行 测试中添加一个单独的 class。
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 {
}