1

How can I define a variable as a random match?

item[]
0{}
1{
_id:item
}
2{}
3{}

I want to define a match not with item[1]._id but with item[random]._id.

Could you please help me with that?

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
Thanoon
  • 21
  • 2

1 Answers1

0

Just a warning that trying to add such dynamic behavior to your tests makes them less maintainable in the long term.

Here is an example that works:

* def size = response.items.length
* def index = Math.floor(Math.random() * size)
* def item = response.items[index]
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • `* match $.data.subjectById.itemsForSubjectCoordinator[0].docReferenceId == #present' `* def topItem0 = $.data.subjectById.itemsForSubjectCoordinator[0].docReferenceId` – Thanoon Dec 17 '19 at 13:50
  • @Thanoon your question is incomplete, may be it is `response.item` for you - so it is time for me to refer you to this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Dec 17 '19 at 13:51
  • this code is going just to select my topitemid in my test i have define my test as well but i need to select another topitemid with a really name that i want it works like random with if loop maybe! i want it to go into my list and look if its the save work in the list then take the itemid @peter – Thanoon Dec 17 '19 at 14:06
  • i do not want to use this match again for list 1 like -> `* match $.data.subjectById.itemsForSubjectCoordinator[1].docReferenceId == #present'` to get the match i want it like a random where ever it is it has to be selected – Thanoon Dec 17 '19 at 14:42
  • @Thanoon look at other questions on stack overflow and see if you can figure out what you can do better – Peter Thomas Dec 17 '19 at 15:06