I'm learning to use Cucumber and have a Scenario as follows:
Given a standard party
And a flight from "UK" to "Europe"
When I search
And I receive search results
Then I can book my holiday
In Step Two's Step Definition, I query a database using JDBI and receive a dataset that I can iterate over and use as search data for Step 3 in the API I am sending requests to. It's is quite common not to receive any results on the first 2 or 3 rows queried. This is not useful to me because the scenario is not really testing for whether results are returned (although that is clearly a valid scenario).
What I really want is to get results back and then assert that I can book - within that Step Def are checked for correct price etc....
So, if
And I receive search results
equates to false, how can I return to
And a flight from.....
...so that I can try the next row of data.
My query then is in the context of Cucumber Step Definitions, what it is the best/ idiomatic way to do this, given that the for each loop is in the earlier step?
Without exposing too much of the Step Def code, is there a common convention for retrying in such cases? I can't simply call the underlying search method again because I have many search Step Def methods all glued to steps and the "And I receive..." in which I receive a response object has no concept of the preceding Step.
And a flight from "UK" to "Europe":
@And("^a flight from \"([^\"]*)\" to \"([^\"]*)\"$")
public void aFlightFromContinentOneToContinentTwo(String continentOne, String continentTwo) {
airportsAndDates = searchWorld.airportsAndDates();
supplierCode = supplierCredentials.getSupplierCode();
ReposMasterConnection rm = new ReposMasterConnection();
origins = getContinentOne(continentOne);
destinations = getContinentTwo(continentTwo);
rm.initialiseDatabaseConnection();
List<FlightDataBean> dataSet = rm.lastFlightBookedResultsByRegion(supplierCode, origins, destinations);
for (FlightDataBean i : dataSet) {
String startDate = i.getStartDate();
String endDate = i.getEndDate();
String origin = i.getOrigin();
String destination = i.getDestination();
String start = getStartDate(startDate);
String end = getEndDate(endDate);
if (startDate != null) {
DateRange dateRange = new DateRange()
.withStartDate(start)
.withEndDate(end);
airportsAndDates.
withDepartureDateRange(dateRange).
withDepartureAirports(departureAirportList(origin)).
withDestinationChoice(destinationChoice(destinationAirportList(destination)));
}
}
}
When I search...//another Stepdef which posts the request
Then I receive search results:
@Then("^I receive search results$")
public void iReceiveSearchResults() {
response = given().contentType(ContentType.XML).body(packageAvailRequest).when().post().then().extract().response();
String status = requestWorld.getAppRequest().getAvailabilityResponseObject().getSearchResponseStatus(response);
requestWorld.getAppRequest().getAvailabilityResponseObject().setResponse(response);
errorMessage = requestWorld.getAppRequest().getFirstBasketResponseObject().getError(response);
warningMessage = requestWorld.geAppRequest().getFirstBasketResponseObject().getWarning(response);
if (errorMessage != null && !errorMessage.isEmpty()) {
scenario.write("Error: " + errorMessage);
}
if (warningMessage != null && !warningMessage.isEmpty()) {
scenario.write("Warning: " + warningMessage);
}
assertEquals("true", status);
}
"uk"/"europe"
if there are no flights? If yes, where do you sore yourfrom ".." to ".."
data?