Taran
Taran

Reputation: 531

Setting global variables in karate from one feature to be used in another feature

The Problem

I have 3 different feature files. One is for EndToEndTest.feature (the integration test), one is for the create-service.feature (a basic api call), and another is for the create-tariff (an api call that requires the response from create-service.feature)

EndToEndTest.feature

Feature: End To End Standard

  Background:
  * def result = call read('../get-user-token.feature')
  * def service = call read('create-service.feature')
  * def location = service.responseHeaders['Location'][0]
  * def serviceId = location.substring(location.lastIndexOf('/') + 1)

  # I want serviceId to be set globally so I can access it into the create-tariff.feature

  * def tariff = call read('create-tariff.feature')

create-tariff.feature

Feature: Create Tariff

  Background:
  * def result = call read('../../get-user-token.feature')
  * def service = serviceId

Its obvious the second def here (service=serviceid) will fail as it will be unable to obtain the serviceId set from the first

My question is, what is the best way of passing the one variable (serviceId) from EndToEndTest.feature to assign to the other variable (service) in create-tariff.feature

Upvotes: 2

Views: 3713

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

Having features depend on each other is a very bad practice and I don't recommend it. You are not supposed to even have Scenario-s depend on other ones.

Please read this: https://github.com/intuit/karate#script-structure

Note that you have hooks - specifically karate.callSingle() but it is intended only for situations where you sign-in and get a token and then need to re-use it all over.

Upvotes: 3

Related Questions