sn1693
sn1693

Reputation: 271

Karate - Having multiple 'When's in single scenario

I have a scenario where there are multiple steps/ REST operations needs to be run to complete one process.

Each REST operation needs to have authorization username and password. I have provided this in Background session. This is how my present feature looks like.

Feature: Adding to cart

  Background: 
    * url 'https://soa-mp-rmsk-someurl.com'
    * header agent_uid = 'AUTO_TST'
    * configure ssl = true
    * header Authorization = call read('classpath:Project/JSFiles/auth.js') { username: 'ABC', password: '123' }
    * configure logPrettyResponse = true
    * configure logPrettyRequest = true

  Scenario: Find available mobiles
    Given path '/v1/available-mobiles'
    When method get
    Then status 200
    * def mobile = response.iPhoneXSMax

  # Add a mobile to cart
    Given path '/v1/mobiles/'+mobile+'/add
    And request {name: 'iPhoneXSMax'}
    When method put
    Then status 200

Now this throws error saying that "faultstring": "Authentication challenge issued".

I can group them into different scenarios so that they call header authorization every time which results in successful run; I have tried this as well. Works for me. But i don't think its a good practice to group these steps in different scenarios as they literally makes a single scenario. How can i overcome this error? Or should i go and distribute them in different scenarios?

https://automationpanda.com/2018/02/03/are-gherkin-scenarios-with-multiple-when-then-pairs-okay/

EDIT-1 This is how i tried to add configure headers for Authorization details; I was not able to fully understand this, could you please help?

headers.js

function fn() {
      var username = karate.get('username');
      var password = karate.get('password');
      if (username && password) {
        return { 
            Authorization: username + password
        };
      } else {
        return {};
      }
    }

And i called it in feature background like this; but didn't worked.

* configure headers = read('classpath:headers.js')
* def username = 'ABC'
* def password = '123'

Upvotes: 1

Views: 3128

Answers (2)

sn1693
sn1693

Reputation: 271

Just want to say that for the EDIT-1 i managed to get the solution. This is how i have written my headers.js file which has BASE64 format conversion.

Please let me know guys, if i can enhance it in any better way.

function fn() {
      var username = karate.get('username');
      var password = karate.get('password');

      if (username && password) {
      var temp = username + ':' + password;
      var Base64 = Java.type('java.util.Base64');
      var encoded = Base64.getEncoder().encodeToString(temp.bytes)
        return { 
            Authorization: 'Basic ' + encoded
        };
      } else {
        return {};
      }
    }

Upvotes: 1

Peter Thomas
Peter Thomas

Reputation: 58058

I think you have completely missed that for header based auth - Karate has a global kind of a hook - which is what most teams use. Refer the docs for configure headers.

Upvotes: 2

Related Questions