Reputation: 338
I'm trying to perform the automation tests of a web application using Geb Framework with Cucumber. However, while running my first test I have stumbled upon a NullPointerException as soon as the program reaches the 'Given' tag. I have searched the Internet for clues but found no answers. I have also referred to both Geb and Cucumber documentations and found many examples of 'good practice' while making use of Cucumber, but every code example that I tried to adjust my code to and ran concluded in the same Null Pointer Exception.
A significant excerpt from my code is included below:
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
Exact error
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
To sum up, I assume the problem is that somehow the Given feature is not identified correctly, but I have no idea why and thus I came to ask here.
Upvotes: 1
Views: 422
Reputation: 338
I know now that I should have added a separate class to run the tests.
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 {
}
Upvotes: 1