1

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!

LinaK
  • 41
  • 3

1 Answers1

1

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.

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • Thank you Peter! To run them in parallel I need to add the scenario outline to every test them, setting the 3 types of users in headers for each run? – LinaK Aug 02 '22 at 10:34
  • @LinaK for the tests for all 3 users, just call a second feature 3 times but passing the user+token as an argument, won't that work ? if you just have 3 "iterations" you may not even need a Scenario Outline. also see this answer: https://stackoverflow.com/a/60387907/143475 – Peter Thomas Aug 02 '22 at 11:14