1

My scenario is very common: I create certain objects using the POST method. At the end of the feature I want to delete all the objects that I have retrieved from a GET operation

Feature:

Scenario: create cat
Given url demoBaseUrl
And path 'cats'
And request { name: '#(name)' } ## using table created many cats
When method post
Then status 200

Scenario: get all the cats
Given url demoBaseUrl
When method get
Then status 200
* def createdcats = $.cats[*].id

## this gives lets say 4 values Cat1,Cat2,Cat3,Cat4

##Now I want to use the DELETE method to delete the said cats, the DELETE operation takes a path param

Given url demoBaseUrl
And path 'cats'
When method delete
Then status 204

what is the parameter to be passed for looping over an array of createdcats

I had an idea of having a JS function, but then it will mean calling a feature from with js. Is it the right way to do it?

1 Answer 1

1

You can create a re-usable delete feature and iterate it with multiple data set as you wish

create a new feature as below,

deleteCats.feature

Feature: delete cat feature
 Scenario: delete cat
  Given url demoBaseUrl 
  And path catId
  When method delete 
  Then status 204

Modify your main feature to call the above feature and run

Main.feature

Feature: cat feature
 Scenario: create cat 
  Given url demoBaseUrl 
  And path 'cats' 
  And request { name: '#(name)' } ## using table created many cats 
  When method post 
  Then status 200 
 Scenario: get all the cats 
  Given url demoBaseUrl 
  When method get 
  Then status 200 
  * def createdcats = $.cats[*].id
  * def createdcats = karate.mapWithKey(createdcats, 'catId')
  * def delCats = call read('deleteCats.feature') createdcats

Now the last 2 lines in the above code will create data for iteration and iterate the given feature for n number of times.

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.