1

We use karate 0.9.5. We would like to make a dynamic Scenario Outline using conditional logic. We would like to retrieve towns for a list of regions, and this for a list of countries. Conditional logic is used only in towns retrieving. What we've done:

Main.feature:

Scenario Outline:
* def countries = call read('GetRegionsWithTowns.feature') { countryId: '#(<id>)' }
Examples:
  |countriesList|

GetRegionsWithTowns.feature:

Scenario:
* def fun =
  """
  function(regions) {
    for (i = 0; i < regions.length; i++) {
      var region = regions[i];
      if(region['name'] && region['name'].contains('alabama')){
        karate.log('***smth');
      }
      else{
        var regionId = region['region_id'];
        karate.call('GetTowns.feature', [regionId]);
      }
    }
  }
  """
* def regions = call read('GetRegions.feature') { countryId: '#(countryId)' }
* def result = call fun(regions)

GetRegions.feature

Scenario:
 Given url 'someurl'
 And path countryId, 'regions' 
 When method get
 Then status 200

GetTowns.feature

Scenario:
 Given url 'someurl'
 And path regionId, 'towns'
 When method get
 Then status 200

Error is

 GetTowns.feature:16 (the line with "And path regionId, 'towns'") - javascript evaluation failed: regionId, ReferenceError: "regionId" is not defined in <eval> at line number 1

We can't figure out what we are doing wrong? Any help is welcome, thanks

cygne
  • 527
  • 1
  • 10
  • 28

1 Answers1

1

I think this is the change needed:

karate.call('GetTowns.feature', { regionId: regionId });

I do think you are grossly overcomplicating your tests.

  • I would try to avoid call as far as possible.
  • Any variables defined in parent features will be visible to "called" features, so you don't need to pass all data as arguments
  • Avoid JS code especially loops as far as possible, refer: https://github.com/intuit/karate#loops
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thank u so much @Peter Thomas, but how we can simplify this test? We need to iterate in amount of countries, regions, towns, which is dynamic and we need a condition in towns retrieving. – cygne Mar 31 '20 at 07:49
  • @cygne just my opinion and I don't understand your scenario because it is so complex. sometimes you should just hard-code your input and output data for simpler tests, best explained here: https://stackoverflow.com/a/54126724/143475 – Peter Thomas Mar 31 '20 at 07:51