1

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 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.