zeus1234
zeus1234

Reputation: 473

Karate API Testing - Access variable value across scenario in a feature file

I am running into this situation (similar to this) when trying to do the following:

Scenario: Create User
Given path 'user'
    And request read('user.json');
    When method post
    Then status 200
    And def user_id = response.userId

Scenario: Test A
   Given path 'user/' + user_id <- Received javascript error here
   ...

Scenario: Test B
   Given path 'user/' + user_id <- Received javascript error here
   ...

Scenario: Test A
   Given path 'user/' + user_id <- Received javascript error here
   ...

Basically what i am trying to do is create a user in my database first, then run it through a series of test and use a last scenario to delete this user. Therefore, i need to share the user_id value across multiple scenarios. Background doesn't work for me because that means i have to create a new user for each scenario. I have seen in the demo that a simple way would be to put all test under 1 scenario but i don't feel that it is right to put all tests on 1 scenario

I have reviewed the karate demo but i did not come across any sample code that will help my situation. May i know the right way to do this in Karate? Thanks.

Upvotes: 3

Views: 7981

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58098

I think you are missing the callonce keyword. Understand it and then look at the demos, for example, this one: callonce.feature.

You will need to move the 'common' code into a separate feature, but that is the right thing to do, because you will typically want it re-usable by multiple feature files as well.

Upvotes: 2

Related Questions