Radim Bukovský
Radim Bukovský

Reputation: 357

Karate - how to put variable value into driver configuration (configure in JS, read values in features - background)

I am not able to use variable in driver configuration (feature file Background).

1)variable is defined in JS configuration file (karate-config.js):

config.driverType = 'geckodriver';
config.driverExecutable = 'geckodriver';
config.driverStart = false;
config.driverPort = 4444;

2)in feature file (Background section) I need to modify driver according to the variable values:

configure driver = { type: driverType, executable: driverExecutable, start: driverStart, port: driverPort}

to have same result to this (this works):

configure driver = { type: 'geckodriver', executable: 'geckodriver', start: false, port: 4444} 

3) when I wrote the variable "print driverType" in scenario, value is printed correctly:

[print] geckodriver

but driver configuration fails:

WARN  com.intuit.karate - unknown driver type: driverType, defaulting to 'chrome'

ERROR com.intuit.karate - driver config / start failed: class java.lang.String cannot be cast to class java.lang.Boolean (java.lang.String and java.lang.Boolean are in module java.base of loader 'bootstrap'), options: {type=chrome, executable=driverExecutable, start=driverStart, port=driverPort, target=null}

Could you help me with solving this to be able to change driver settings in JS file (generally - how to insert variable into driver configuration)?

Thank you.

Upvotes: 1

Views: 1296

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58153

Just make this change:

* configure driver = { type: '#(driverType)', executable: '#(driverExecutable)', start: '#(driverStart)', port: '#(driverPort)' }

Or this should also work:

* configure driver = ({ type: driverType, executable: driverExecutable, start: driverStart, port: driverPort })

There is a subtle difference, explained here: https://github.com/intuit/karate#enclosed-javascript

By the way, you can do the config like this also in karate-config.js:

config.driverConfig = { type: 'geckodriver', executable: 'geckodriver' };

And this would work in the feature file:

* configure driver = driverConfig

And you can do the driverConfig completely in the karate-config.js if you want:

* karate.configure('driver', { type: 'geckodriver', executable: 'geckodriver' });

Upvotes: 2

Related Questions