2

I have set two variables, let's say cityA = 'New York' and cityB = 'Las Vegas' in one feature and "exported" them with * def expression. Now I want to use those variables in another feature

Scenario Outline: Successful transfer from <fromLocation> to <toLocation>
    * call read('re-usable.feature')    
    Given request read('request.json')
    When method post
    Then status 200

    Examples:
    | fromLocation | toLocation |
    | cityA | cityB |
    | cityB | cityA |

with request:

{
source: "#(fromLocation)"
destination: "#(toLocation)"
}

My scenarios are failing because in request there is set cityA/cityB, not values stored under these names, I tried to use variables from karate-config file and even

Scenario Outline: Successful transfer from <fromLocation> to <toLocation>
    * def res = call read('re-usable.feature')    
    Given request read('request.json')
    When method post
    Then status 200

    Examples:
    | fromLocation | toLocation |
    | res.cityA | res.cityB |
    | res.cityB | res.cityA |

and that is passing 'res.cityA', 'res.cityB' instead of New York/Las Vegas Moreover, I tried also following ways

Background:
    * call read('re-usable.feature')
Scenario Outline: Successful transfer from <fromLocation> to <toLocation>

    Given request read('request.json')
    When method post
    Then status 200

    Examples:
    | fromLocation | toLocation |
    | cityA | cityB |
    | cityB | cityA |

and

Background:
    * def res = call read('re-usable.feature')
Scenario Outline: Successful transfer from <fromLocation> to <toLocation>

    Given request read('request.json')
    When method post
    Then status 200

    Examples:
    | fromLocation | toLocation |
    | res.cityA | res.cityB |
    | res.cityB | res.cityA |

All presented cases return the same - varaible names not values. If in the request I set e.g. #(cityA) instead of #(toLocation) then everything is as expected but I cannot test other scenarios in this case

@PeterThomas you marked my last question as duplicate but link you have provided is not about my issue ;)

Sashore
  • 55
  • 3

1 Answers1

1

Variable "substitution" in Examples is not supported. Also you seem to be confused between fromLocation and cityA, I strongly recommend keeping tests simple, as I linked to from your previous question.

Anyway, especially when you use a call to generate data, you are better off using table and then using a loop:

* def data = [{ cityA: 'a', cityB: 'b' }, { cityA: 'c', cityB: 'd' }]
* call read('re-usable.feature') data

So please read: https://github.com/intuit/karate#data-driven-features, you still need 2 feature files, but the sequence of execution is "reversed".

Otherwise, I still don't understand your test. The only way to work around the lack of "dynamicism" in the cells of an Examples table is like this:

Scenario Outline:
* def res = { cityA: 'foo' }
* def value = <fromLocation>
* match value == 'foo'

Examples:
| fromLocation |
| res.cityA |

Refer: examples.feature - the last example.

But hopefully you will understand that you are using Examples the wrong way which will confuse all the people who need to read your test in the future. If you still have questions, ask a new one - and this time give some summary of the use-case you are trying to automate.

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