I am new to Karate framework and have written the following working code to run few sample API tests.
Feature: Sample API Tests
Background:
* def request_headers = {apikey: "#(apikey)", Content-Type: 'application/json'}
* configure headers = request_headers
Scenario: Create Customer
* def requestPayload = read('classpath:payload/customerdetails.json')
* def RandomDataGenerator = Java.type('utils.RandomDataGenerator')
Given url testurl+'/customers'
And request requestPayload
* requestPayload.firstName = RandomDataGenerator.generateRandomFirstName()
* requestPayload.lastName = RandomDataGenerator.generateRandomLastName()
* requestPayload.mobileNumber = RandomDataGenerator.generateRandomMobileNumber()
* requestPayload.emailAddress = RandomDataGenerator.generateRandomEmailAddress()
When method post
Then status 200
And def customerId = response.id
# Create An Account for the customer
* def createAccountPayload = read('classpath:payload/accountdetails.json')
Given url testurl+'/accounts/createCreditAccount'
And request createAccountPayload
* createAccountPayload.customerId = customerId
When method post
Then status 200
And def accountId = response.id
# Perform a Transaction
* def debitTransactionPayload = read('classpath:payload/transaction.json')
Given url testurl+'/accounts/createDebitTransaction'
And request debitTransactionPayload
* debitTransactionPayload.accountId = accountId
When method post
Then status 200
Now I want to run a test where I would:
How do I achieve this?
I tried using repeat and looping options, but I believe I am missing something basic. Can someone please help ?
Here is a Karate test that uses a JS function as a data-source to create 10 JSON payloads. You should be easily able to modify this to do what you want:
Feature:
@setup
Scenario:
* def generator =
"""
function(i){
if (i == 10) return null;
return { name: `cat${i}`, age: i };
}
"""
Scenario Outline:
* print __row
Examples:
| karate.setup().generator |
How this works is explained here: https://github.com/karatelabs/karate#json-function-data-source
Also refer: https://stackoverflow.com/a/75394445/143475