2

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

Alex
  • 23
  • 7

1 Answers1

0

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 ?

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thanks. Having only variable in the table cell works perfectly. In the above case, there is a variable 'userid' inside parentheses after a comma like this (KHTML, like Gecko, userid) – Alex Aug 27 '21 at 13:01
  • @user43221 I don't understand what you mean by variable, so see my edit. if it doesn't help, please contribute code to "fix" karate or look for another tool, thanks – Peter Thomas Aug 27 '21 at 13:10
  • I meant variable (def userid = test.getuserid()) which is in the background – Alex Aug 27 '21 at 14:45
  • @user43221 didn't I already say that variables are not supported in the `Examples:` ? you just need to do some work in the body of the scenario. if that does not make sense please use some other framework – Peter Thomas Aug 27 '21 at 15:08