Prakruti Pathik
Prakruti Pathik

Reputation: 392

Can we pass method and path while calling feature from inside another feature

When you call a feature (with a few Scenarios) from inside another feature, I want to pass method and path as these are common scenarios - called from two different endpoints where base url remain same but path and also the method differ.

This situation arise because I am trying to put the common scenarios in a feature and call that feature from other feature files where the scenarios are all common and they differ only in path and method.

I have referred to this issue: 'https://github.com/intuit/karate/issues/239' and know that it is possible but in my case I need to pass the path and method as arguments while calling the feature file because that is the only thing differing with two modules calling the common scenarios.

So the question is can we pass path and method as parameters while calling a feature file. Currently I am getting error but do not understand why it should fail. I tried the following: booking.feature Background: * def pathVar = '/bookings'

 Scenario: Calling basic validation feature for create booking module
    * call read('classpath:feature/basic-validations.feature') {path1: '#(pathVar)', action: 'post'}

basic-validations.feature

Background: * url baseURL * header Accept = 'application/json' * def data = read(datafile) * header API-Version = 1 * path '#(path1)' * header Authorization = 'Bearer' + data.booking.token

   Scenario: Empty request validation
          Given request {}
          When method '#(action)'
          Then status 400

    Scenario: Request-Body element is empty or null.
          Given def req = ({ "request_body": null })
          And request req
          When method '#(action)'
          Then status 400

   Scenario: When parameter value for name in request body is incorrect.
          Given def req = ({ "request_body": [ { "name": "shipment_standard_booking", "action": 
          "create", "path": "/standardBooking", "body": data.booking.standardBooking.requestBody }] 
           })
          And def name = 'test'
          And set req.request_body[*].name = name
          And request req
          When method '#(action)'
          Then status 400
          And match $.debugMessage == 'Validations failed due to bad input payload request. WorkItem 
          name (' +name+ ') is invalid'

Upvotes: 1

Views: 803

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58068

The path step can take variables. The method step also can take a variable: https://github.com/intuit/karate#method

* def methodVar = 'post'

* url foo
* request bar
* method methodVar

But I totally don't recommend it - or the approach you are taking. Reasons are explained here, please take some time to read it: https://stackoverflow.com/a/54126724/143475

I think you also have mis-understood embedded expressions, so please read this: https://github.com/intuit/karate#rules-for-embedded-expressions

If you still decide to do this and get stuck, you can assume that either Karate does not support what you want to do - or that you need to contribute code.

Also read this for other ideas: https://stackoverflow.com/a/50350442/143475

Upvotes: 1

Related Questions