karate

match is failing for substring validation when we pass the substring as a variable


My Payload has a tag data which I am setting the value to 'test'..

   * set test.input.data = 'test'
   * def listData = ["testcoding", "testcoding", "testcoding"]
   * match each listData contains '#(test.input.data)'

When I do the match then its failing with the following error -

   $ | match each failed at index 0 (LIST:STRING)
     ["testcoding", "testcoding", "testcoding"]
     '#(test.input.data)'
   $ [0] | not equal (STRING:STRING)
     'testcoding'
     'test'

but when I pass the value directly to the match statement it passed the validation -

    * set test.input.data = 'test'
    * def listData = ["testcoding", "testcoding", "testcoding"]
    * match each listData contains 'test'

Is this the expected behavior?? Why validation failed when we pass the substring as a variable in match condition? Is there anything wrong I am doing?

Appreciate any help. Please feel free to let me know if you need more details regarding the error.

PS - If the test.input.data is not a substring it return pass without any issues.

example :-

    * set test.input.data = 'testcoding'
    * def listData = ["testcoding", "testcoding", "testcoding"]
    * match each listData contains '#(test.input.data)'

Outcome - pass/success


Solution

  • The contains keyword works only for JSON arrays. When trying to do a "substring" - that kind of stuff enters the world of Javascript, and the API to do that is called includes().

    So try this:

    * def data = [ 'testcoding', 'testcoding', 'testcoding' ]
    * match each data == "#? _.includes('test')"
    * match each data == "#? _.startsWith('test')"
    

    Refer docs for what the #? means: https://github.com/karatelabs/karate#self-validation-expressions