1

I couldn't find any solution for my problem so I hope someone will be able to help me.

Basically I'm trying to test micro service architecture which depends on the main system.

I created test data located in helper-create-test-data.feature

Feature: Functionality of creating test data. The main goal of this helper is to create a certain test data
  and use it for testing a proper features and microservices.

  Background:
    * configure report = { showLog: true, showAllSteps: false }

  @create-multiple-adverts
  Scenario Outline: Advert with type - <type> - successfully paid
    * def advert_request = read('classpath:utilities/creation-request.json')
    * def authentication = callonce read('classpath:utilities/helper-authenticate-user.feature')
    * def access_token = authentication.access_token
    * def timestamp = result.timestamp
    * set advert_request.params.type = '<type>'
    * set advert_request.category_id = <category_id>


    Given url baseUrl
    And header Authorization = 'Bearer ' + access_token
    And path 'acc/ad'
    And request advert_request
    When method post
    Then status 201
    And print "Advert successfully created with " + __row
    And def advert_id = $.id
    Given path 'acc/ad/' + advert_id + '/activate'
    And header Authorization = 'Bearer ' + access_token
    And request ''
    When method post
    Then status 200
    And print "Advert successfully activated"

    Examples:
      | type          | category_id |
      | test1         | 163         |
      | test2         | 165         |
      | test3         | 167         |
      | test3         | 173         |

Then I execute a proper test located in pricing-e2e.feature:

  Background:
    * def result = call read('classpath:utilities/helper-random-data-js.feature@timestamp')
    * def timestamp = result.timestamp
    * print "Current timestamp " + timestamp
    * configure afterScenario =
      """
      function(){
        var info = karate.info;
        karate.log('after', info.scenarioType + ':', info.scenarioName);
        karate.call('classpath:utilities/helper-create-test-data.feature@deactivate-advert', { caller: info.featureFileName });
      }
      """

 @pricing
  Scenario: Payment events successfully received 
    * def adverts = call read('classpath:utilities/helper-create-test-data.feature@create-multiple-adverts')

    Given url payment_url
    And path 'bil/history/' + user_id
    And param startDate = timestamp
    When method get
    Then status 200
    And match response.data == '#[4]'
    Given url payment_url
    And path 'bil/site/' + user_id
    And param startDate = timestamp
    When method get
    Then status 200

And clean-up afterScenario located in helper-create-test-data.feature

  @deactivate-advert
  Scenario: Deactivate an advert
    * def authentication = callonce read('classpath:utilities/helper-authenticate-user.feature')
    * def access_token = authentication.access_token

    Given url baseUrl
    And header Authorization = 'Bearer ' + access_token
    And path 'acc/ad/'+ advert_id + '/deactivate'
    And request { "reason": {"id": "100", "description": "Some Reason" } }
    When method post
    Then status 200

My questions are: how I can loop @deactivate-advert in afterScenario and how can I pass adverts_id into afterScenario? I should run this request 4 times because I created 4 items before.

rado
  • 45
  • 4
  • Wouldn't you just add a `for` loop to your `afterScenario` function? Also, recently read a good article about why after hooks are an anti-pattern, food for thought: https://docs.cypress.io/guides/references/best-practices.html#Using-after-or-afterEach-hooks – anutter Jan 11 '21 at 16:51
  • @rado not sure if this still can help you. Background: * configure afterScenario = function(){karate.call('helper-create-test-data.feature@@deactivate-advert',advert_id);} – Don Feb 05 '22 at 18:54

0 Answers0