How to get a value (ex: baseURL) in every Karate feature?
Asked Answered
M

1

4

I would like to get a common variable for all features. Ex: base URL and path (in some features). I don't understand well in Karate official tutos what to do exactly. I put in user.feature

`Background:
* url urlBase
* path users

In karate-config.js

function() {
  var env = karate.env;
  karate.log('karate.env system property was:', env);
  if (!env) {
    env = 'dev';
  }
  var config = {
    env: env,
    appId: 'my.app.id',
    appSecret: 'my.secret',
    baseURL: 'https://someurl/',
    users: 'users'
  };
  karate.configure('connectTimeout', 5000);
  karate.configure('readTimeout', 5000);
  return config;
}

and in my runner class:

@RunWith(Karate.class)
@CucumberOptions(features = "classpath:features/users/User.feature")
public class TestRunner{
    }
}

User.feature and karate-config.js are in the same folder and Runner class is in another. Is it right? or I need more things to specify?

Miosis answered 19/9, 2017 at 11:27 Comment(0)
M
3

No this is not right. I suggest you refer to the quickstart documentation of Karate where you can generate a simple project. Get that running first, and then try to set the URL from the karate-config.js. All the best.

Membrane answered 19/9, 2017 at 13:8 Comment(5)
thanks, Peter Thomas, for your quick reply. I couldn't find any issue. I tried many things, but may be not the right one. Can you give us some tips, especially for the feature side, please. PS: marked the first answer as "accepted"Miosis
try this: read the documentation. also create a quickstart. here is a useful tutorial: blog.testproject.io/2017/08/21/api-testing-karate-frameworkMembrane
I found an issue here: semaphoreci.com/community/tutorials/…. But it works for one parameter (ex: baseUrl). How to do for many parameters?Miosis
add more keys to the JSON returned by karate-config.js - it is that easy. if you still have trouble, get someone who knows basic JavaScript to look at your karate-config.js - that will solve your problem. all the very best :PMembrane
I'm using this tutorial, github.com/intuit/karate#configuration, to get environment dynamically from the command line but I keep getting an error stating that my url is not defined but when I use only one as mentioned above it works. @nirind were you able to get env dynamically (dev, staging, prod, etc). ThanksTractable

© 2022 - 2024 — McMap. All rights reserved.