How to generate more than one random UUID and use in scenario outline examples in karate

Viewed 258

I am new to Karate API, pardon me for the mistakes if any.

I want to generate multiple random UUID and then use them in scenario outline examples

Example: Background:

  • def UUID = function() {return java.util.UUID.randomUUID() + ''}

Scenario outline: to do post call Given url 'http://localhost:8080'

  • def UID = UUID()
  • print UID

And request {CID:"", name :""}

When method POST

Then status 201

Examples:

|CID| name|

|UID1| james|

|UID2| rahul|

Here in above 'Examples' I wanted to use randomly generated UUID in data table of examples so that I can run multiple scenarios for UUID with one POST API call. First question: How can I generate multiple random UUID ? Second question: once multiple UUID gets generated how can i call in scenario outline examples and use them? Can anyone suggest me on this?

1 Answers

Please try running the following simple example.

Feature:

Background:
* def uuid = function(){ return java.util.UUID.randomUUID() + '' }

Scenario Outline:
* url 'https://httpbin.org/anything'
* param foo = uuid()
* request { item: '#(item)' }
* method post

Examples:
| item   |
| first  |
| second |

It will make 2 requests, and each request will use a different param called "foo" and the URL will be like this:

https://httpbin.org/anything?foo=c1b6ab3d-5952-413b-827c-d9579a0a93b6

So it is simple. Think of the Examples: as like a "loop". Each time the Scenario Outline runs, we are calling the uuid() function again, which will return a different, random value.

Related