Store all tokens for different users and run tests in parallel

Viewed 20

Bothering again but really need a piece of advice from the community

Imagine I have 3 user types:

  • read
  • write
  • admin

with different permissions and I need to request a token for all 3. Then store each token and then be able to run some tests for read user, other tests for admin and some tests for all 3 profiles. Is it possible to handle with Karate? I know there is ScenarioOutline but that would require me to add this ScenarioOutline to every single scenario. Also thought about having a json.file and run my CreateToken.feature with every single user that is in this users.json but then I don't know how to tell my tests which token to use. My Create Token feature file for the basic profile is:

Feature: Create Token

Background: URL definition
    * url authUrl

Scenario: Create Token for Read user
    When path '/v2/u/login'
    And request
    """
    {
        "username": "#(readUser)",
        "password": "#(password)"
    }
    """
    And method Post
    Then status 200
    * def readAccessToken = response.token

in karate-config.js I have defined the 3 users:

 var config = {
    readUser: 'readuser@example.com',
    writeUser: 'writeuser@example.com',
    adminUser: 'adminuser@example.com',
    password: karate.properties['password'],
  }
  const readAccessToken = karate.callSingle('classpath:helpers/CreateToken.feature', config).readAuthToken
  karate.configure('headers', {Authorization: 'Bearer ' + readAccessToken})

In the Feature files:

Scenario: Get explore page
#This should run for all 3 types of users
    When path '/a/v2/explore'
    And method Get
    Then status 200
    And match response contains exploreResponse

Scenario: Get user list
#This should run only for admin user
    When path '/users'
    And method Get
    Then status 200

Sorry but I can't the best way to setup this Thanks!

1 Answers

I would just create the 3 tokens using call-single, keep them as global variables in config and then use them the way you see fit.

Related