Karate DSL variable is not resolved in array initialization

Viewed 14

In my karate feature I want to initialize an array with a variable in it.

To my understanding, that should be possible with a single line like this: * def array = [Id]

However, that sets the variable array to ["Id"]. But using set works.

See example:

  Scenario: WTF
    * def Id = 123
    * print "Id", Id
    * print "[Id]", [Id]
    * def array = [Id]
    * print "array", array
    * set array[0] = Id
    * print "array", array

Produces the output

Id 123 
[Id] [123]
array ["Id"]
array [123]

How can * def array = [Id] be changed to produce the array with the variable in it?

1 Answers

Here you go:

Scenario: FTW
* def id = 123

# karate recommended
* def array = [ '#(id)' ]
* match array == [123]

# js developers like this
* def array = ([id])
* match array == [123]

And please read this for a full explanation: https://github.com/karatelabs/karate#enclosed-javascript

Related