0

I am trying to use "Examples" under "Scenario Outline" Section of a feature file.

In Feature File, i tried representing it in the below format. Will this work as expected.

Feature: save data to db
Background: no definition
Scenario Outline: to validate file is getting read by example section

Given url 'https://www.googleapis.com/geolocation/v1/geolocate'
And param key = 'AIzaSyB2jt4BQ9McqBXAe8dYcp1CwKf0oGFlWuc'
And def dogs = read('classpath:external_table/json.feature')
And request { "homeMobileCountryCode": '<homeMobileCountryCode>' 
,"homeMobileNetworkCode": '<homeMobileNetworkCode>' } dogs
When method post
And print response
Then status 200

Examples: {'datafile':'src/test/java/external_table/testdata.xlsx'}
Preethi Ravi
  • 91
  • 1
  • 2
  • 10

3 Answers3

1

First of all the syntax is wrong. The examples table should start in the next line rather than on the same one as the word 'Examples'.

Second there is no header in the examples table, so to use this as a placeholder in the scenario steps.

Third there is a placeholder '', which seems like one, defined in the scenario steps which is not there in the examples table.

I am not familiar to karate to be able to modify the scenariooutline properly. Refer to this link to get an understanding of this -- http://www.baeldung.com/cucumber-scenario-outline

Grasshopper
  • 8,908
  • 2
  • 17
  • 32
1

If you really have to deal with driving a scenario with examples from a CSV or Excel here is the way to do it.

  1. Name your examples, ideally with a name the business understands.

  2. Write a single scenario that uses that name - do not use a Scenario Outline

  3. Get a step to do all the work to get the Excel and examples, using the name to guide the step to get the correct examples.

Lets go through this with a simple list of the football teams in the English Premier League, and assume that you have to get these dynamically from an Excel spreadsheet

Scenario: Commpile statistics for premier league teams
  Given I am working with the premier league
  When I compile teams statistics
  Then I should see team statistics
  And there should be no errors

To make this work we have to push all the details down from the scenarios to the step definitions and ideally helper methods for those step definitions. So we end up with something like

Given "I am working with the premier league" do
  @teams = load_premier_league_teams
end

and then a helper method

def load_premier_league_teams
  get_the_teams_from_the_spreadsheet
end

Now all the stuff about dealing with the spreadsheet is dealt with outside of Cucumber and in your programming language.

Then in the When step you can do your operation for each of the teams as follows

When "I compile the teams statistics" do
  @results = compile_statistics(@teams)
end

and write another helper method

def compile_statistics(teams)
  results = []
  teams.each do |t|
    results << compile_team_stats(t)
  ...
end

Again we push all the details of how we do things DOWN out of Cucumber.

You can do the same thing for the 'There should be no errors' step, iterating through the results using the @results variable and collecting any errors.

Cucumber is just not a tool to be doing programming. With this technique you can still use it to run anything you want, whilst keeping your scenario simple and very speedy.

diabolist
  • 3,990
  • 1
  • 11
  • 15
0

No it won't work as expected. As someone else has already answered, the syntax is wrong.

I don't understand why you have to ask this question again when it is answered here: https://stackoverflow.com/a/49727819/143475

Maybe your leaders are insisting that Excel needs to be used. From experience let me say this, it is far simpler to use Cucumber native examples or Karate's data-driven capabilities where you can loop over a JSON array, refer this: https://github.com/intuit/karate#the-karate-way

If you still insist that you want to use Excel and you are not willing to write a small Java utility to convert Excel (or CSV) to JSON, then please give up on using Karate, it is not the right tool for you. All the best.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248