Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

selenium - How to launch all Karate features setting up which browser to use as an external maven variable

I was trying to find a way to launch all features in Karate testing through maven using an external variable to set up the browser (with a local webdriver or using a Selenium grid).

So something like:

mvn test -Dbrowser=chrome (or firefox, safari, etc)

or using a Selenium grid:

mvn test -Dbrowser=chrome (or firefox, safari, etc) -Dgrid="grid url"

With Cucumber and Java this was quite simple using a singleton for setting up a global webdriver that was then used in all tests. In this way I could run the tests with different local or remote webdrivers.

In Karate I tried different solution, the last was to:

  1. define the Karate config file a variable "browser"
  2. use the variable "browser" in a single feature "X" in which I set up only the Karate driver
  3. from all the other features with callonce to re-call the feature "X" for using that driver

but it didn't work and to be honest it doesn't seem to me to be the right approach. Probably being able to set the Karate driver from a Javascript function inside the features is the right way but I was not able to find a solution of that.

Another problem I found with karate is differentiating the behavior using a local or a remote webdriver as in the features files they're set in different ways.

So does anyone had my same needs and how can I solve it?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

With the suggestions of Peter Thomas I used this karate-config.js

function fn() {

  // browser settings, if not set it takes chrome
  var browser = karate.properties['browser'] || 'chrome';
  karate.log('the browser set is: ' + browser + ', default: "chrome"');

  // grid flag, if not set it takes false. The grid url is in this format http://localhost:4444/wd/hub
  var grid_url = karate.properties['grid_url'] || false;
  karate.log('the grid url set is: ' + grid_url + ', default: false');

  // configurations.
  var config = {
    host: 'http://httpstat.us/'
  };

  if (browser == 'chrome') {
      if (!grid_url) {
         karate.configure('driver', { type: 'chromedriver', executable: 'chromedriver' });
         karate.log("Selected Chrome");
      } else {
         karate.configure('driver', { type: 'chromedriver', start: false, webDriverUrl: grid_url });
         karate.log("Selected Chrome in grid");
      }
   } else if (browser == 'firefox') {
      if (!grid_url) {
          karate.configure('driver', { type: 'geckodriver', executable: 'geckodriver' });
          karate.log("Selected Firefox");
      } else {
          karate.configure('driver', { type: 'geckodriver', start: false, webDriverUrl: grid_url });
          karate.log("Selected Firefox in grid");
      }
   } 

  return config;
}

In this way I was able to call the the test suite specifying the browser to use directly from the command line (to be used in a Jenkins pipeline):

mvn clean test -Dbrowser=firefox -Dgrid_url=http://localhost:4444/wd/hub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...