Sbu
Sbu

Reputation: 940

Set url for all features

My question looks a lot like this one but the accepted answer does not correspond to my target usage :

I would like to set url once and for all in an initialize.feature file, and never set it again afterwards. In other words I don't want to clutter every single feature files with the same following statement :

* url baseUrl

My baseUrl value is set based on karate.env, e.g. https://localhost for local environment and http://prod.env.com for prod. It does not change.

path will change in our feature files because we test different endpoints.

I tried the following setup :

config.baseUrl = 'https://localhost';
// ... code changing config.baseUrl based on karate.env == 'prod' or not
var result = karate.callSingle('classpath:utility/initialize.feature', config);
@ignore
Feature:

  Scenario: Initialize
      * print baseUrl
      * url baseUrl

We can see that baseUrl is correctly printed when executing initialize.feature file.

But in any executed feature afterwards, I get the following error :

some-test.feature:24 - url not set, please refer to the keyword documentation for 'url'

Is it possible to set url only in my initialize.feature file, and never afterwards ?

Thanks.

Upvotes: 1

Views: 1143

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58088

No, you can't. You will have to do * url baseUrl at least once in every feature file. There are multiple reasons for this - readability and maintainability for one, and if you look at the "hello world example" - note how you could omit the url in the second call because you are following the REST-ful patterns.

Since you can do * url baseUrl in the Background: and have all other Scenario-s inherit - this is normally ok in practice, and in real-life API testing we see that you do need to switch URL-s within a test (e.g. for auth). If you feel very strongly about this - you could consider a pull-request. FWIW this is the first time in 2.5 years that someone has ever requested this.

Upvotes: 1

Related Questions