2

I am looking to pass Authorization Header as a variable to another feature file. Here is an example I am trying to do:

    Feature: Resource Creation
      Background:
        * url baseUrl

        Scenario: Create Resource
          Given def basictoken = 'Basic Zn*****'          
          And def token = call read('classpath:endpoints/UserLogin.feature')
          Given path 'lobs'
          And header X-XSRF-TOKEN = token.xsrftoken
          And header Cookie = 'SESSION='+token.scookie+'; '+'XSRF-TOKEN='+token.xsrftoken
          And request [{"name":"Boston"}]
          When method post
          Then status 200

Here is the file it is referring to:

Feature: Common User Login
Background:
  * url baseUrl

Scenario:
  Given path 'security/user'
  And header Authorization = '#(basictoken)'
  When method get
  Then status 200
  Given path 'rname/name'
  When method get
  Then status 200
  And def xsrftoken = responseCookies["XSRF-TOKEN"].value
  And def scookie = responseCookies["SESSION"].value

I am getting error when at And header Authorization = '#(basictoken)' Is there a way I can pass it ? When I hardcode it to its value, I don't see any issue. Could you help us on how to pass variable from one feature file to another. Thanks in advance.

Saurabh
  • 930
  • 2
  • 17
  • 39

1 Answers1

4

Please make this change:

Given def token = call read('classpath:endpoints/UserLogin.feature') { basictoken: 'Basic Zn*****' }

Also note that for simple variables that exist in scope (that are also inherited from "calling" features), you don't need the #(foo) convention:

And header Authorization = basictoken  
Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • I can't see why the original code isn't working in the first place. There is a outer def of "basictoken" so shouldn't that be available in the called file? – davidfrancis Oct 04 '18 at 08:53
  • @davidfrancis from the documents, I think we need to pass the basic token to the called feature i.e UserLogin.feature, which will be used. Unless we pass the token to the UserLogin will not know the feature file. – Bharathan Kumaran Mar 08 '20 at 05:14
  • If you post a full example I'm sure Peter will look at it for you. We no longer use Karate. – davidfrancis Mar 09 '20 at 11:44