简体   繁体   中英

Karate API Testing - Passing variable from one feature file to another

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.

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  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM