Karate: Is there a way to pass variable as string in scenario outline and examples table

Viewed 856

I am using latest karate v1.1.0. My feature looks like this:

Feature: Scenario outline with examples table

Background:
  * url 'http://localhost:8080'
  * def dummy = Java.type(karatetests.attributes)
  * def test = new attributes()
  * def userid = test.getuserid()

Scenario Outline: pass userid with string as parameter
  Given path '<path>'
  And Header Host = 'hostname'
  And Header User-Agent = '<Ua-string>' 
  When method POST
  Then status 200

  Examples:
    | path | Ua-string |
    | api  |  AppleWebKit/537.36 (KHTML, like Gecko, userid)|

In cucumber: I was able to realise the variable value of 'userid' in Ua-string table with AppleWebKit/537.36 (KHTML, like Gecko, ${userid})

In karate: I tried with 'userid', "userid", "#(userid)", and '#(userid)' unfortunately was not succesfull.

  Examples:
    | path | Ua-string |
    | api  |  AppleWebKit/537.36 (KHTML, like Gecko, userid)| => Result: userid string is passed not its value
    | api  |  AppleWebKit/537.36 (KHTML, like Gecko, 'userid')| => Result: syntax error
    | api  |  AppleWebKit/537.36 (KHTML, like Gecko, "userid")| => Result: "userid" string is passed not its value
    | api  |  AppleWebKit/537.36 (KHTML, like Gecko, "#(userid)")| => Result: "#(userid)" string is passed not its value
    | api  |  AppleWebKit/537.36 (KHTML, like Gecko, '#(userid)')| => Result: '#(userid)' syntax error

How can I replace the userid with its value, while passing it to Ua-string header? Thanks

1 Answers

Works perfectly for me. Try this example that you can cut and paste into any feature and see that it works for yourself:

Scenario Outline:
* url 'https://httpbin.org/anything'
* header User-Agent = userAgent
* method get

Examples:
| userAgent |
| foo       |
| bar       |

But I think you are expecting functions and variables to work within Examples: - but sorry, that is not supported: https://stackoverflow.com/a/60358535/143475

EDIT I still don't understand what you mean by "variable" in your comment, but if you are seeing some problems with commas, try this, and refer the docs: https://github.com/intuit/karate#scenario-outline-enhancements

Scenario Outline:
* url 'https://httpbin.org/anything'
* header User-Agent = userAgent
* method get

Examples:
| userAgent! |
| 'foo, bar' |
| 'baz, ban' |

And finally, if you really want to do some "magic" using variables, you can always do that in the scenario body as shown here: https://stackoverflow.com/a/60358535/143475

Or just use string concatenation. Is that so hard ?

Related