1

I need to create 2 items, use get method to check if everything is ok and after that I should delete these items. I have 1 tc - getItem, which uses 2 helpers (postItem and deleteItem). For getItem I need to have itemId, which I get from postItem, where this variable is defined. After that I use the same itemId for deleteItem as afterhook. What I do:

Feature:get item

Background:Pre-conditions
    * url apiUrl
    * call read('classpath:/helpers/features/postItem.feature')
    * configure afterScenario = function(){karate.call('classpath:/helpers/features/deleteItem.feature')}

    Scenario: Get items
        * path '/items/'
        And param id = itemId
        When method Get
        Then status 200

It works but I create only 1 item and delete it correctly because itemId is predefined in postItem and I`m able to re-use it. I saw how to use karate.repeat from HERE but when I do the next

* def item = function(i){ return karate.call ('classpath:/helpers/features/postItem.feature')}

I`m not able to get itemId and as a result not able to delete it. Have tried to use

* print item.response

but it is "null"

So I have 2 questions:

  1. How to get variable from postItem
  2. How to delete each of these created items using afterHook?
Holgar
  • 91
  • 5

2 Answers2

1

Have found solution how can I do this using DRY pattern + afterhooks.

Feature:get items

Background:Pre-conditions
    * url apiUrl
    * def item = function(i){ return karate.call ('classpath:/helpers/features/postItem.feature')}
    * def createdItem = karate.repeat(2, item )
    * table createdItems
    |itemId                         |
    |createdItem[0].response.data.id|
    |createdItem[1].response.data.id|
    * configure afterScenario = function(){karate.call('classpath:/helpers/features/deleteItem.feature', createdItems )}

    Scenario: Get all items
        * path '/items'
        When method Get
        Then status 200

It works, but maybe it also can be updated. Im new in this)

So, basically, what I do:

  1. I create 2 items, for get method using karate.repeat with calling postItem feature
  2. I create table with itemId references
  3. Create afterHook with calling deleteItem.feature, which should have argument itemId and I provide created table for this.
  4. And I have scenario, which checks created items
  5. And after that these created items are deleted by afterhooks.

As a result, I have clear scenario, which contains

  1. Pre-conditions --> creating items (preparing data)
  2. Scenario body --> GET method
  3. Post-conditions --> deleting created items and returning to default state.

All of this I do because dont have DB read permission) In an ideal world, preparing data should be done via SQL and deleted as well) Hope this will help someone)) Also, if you find better solution, feel free to write this here) Tnx

Holgar
  • 91
  • 5
  • 1
    anyway, I'll comment here that I absolutely don't recommend this approach because of the reasons I gave in my other answer. we can leave it to readers to choose what works best for them. peace – Peter Thomas Feb 13 '23 at 15:15
0

May I offer some advice. I would NOT try to create helpers and re-use them like this. Please take some time to read this, and then you may understand: https://stackoverflow.com/a/54126724/143475

I would definitely not use a hook also. Please think of the people who need to maintain your test in the future, they will cry.

Here is how I would write your test. And let me repeat, it is OK to repeat some code when your are doing test automation. For a real, working example, see here.

Background:
* url apiUrl + '/items'

Scenario:
* request {}
* method post
* status 201

* path response.id
* method get
* status 200

* request {}
* method post
* status 201

* path response.id
* method delete

# and so on

Otherwise, the only thing I will say is please refer to the documents on how you can call features and get data back in a loop without using karate.repeat() which should be used only for creating JSON arrays. You can see this answer which has an example and links to the documentation: https://stackoverflow.com/a/75394445/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Tnx for advice and explanation. But what about patern DRY? Also, in your example, in Scenario body you make postItem twice, which re-tests postItem as a separate feature postItem.feature. As a result, this scenario, from your example, will be a huge and checks everything: postItem, getItem and deleteItem. Is this ok? From my point of view, if I want to test specific method, for example GET, I need to have: 1. Background, where I prepare everything what I need for GET method 2. Scenario body, where I check GET 3. Cleaning. Delete what was in Background. And for this, I use helper – Holgar Feb 10 '23 at 14:08
  • @Holgar yes in my opinion, DRY is over-rated. I am just giving you my opinion, you are welcome to take it or leave it. I have nothing else to say, please read the links. some things can be helpers, I recommend only "common setup". please don't put every API call into helpers, that is a mistake. again, just my opinion. – Peter Thomas Feb 10 '23 at 14:20