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

。_饼干妹妹 提交于 2021-02-10 23:17:36

问题


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?


回答1:


Here are a couple of principles. Karate is responsible for starting the driver (the equivalent of the Selenium WebDriver). All you need to do is set up the configure driver as described here: https://github.com/intuit/karate/tree/master/karate-core#configure-driver

Finally, depending on your environment, just switch the driver config. This can easily be done in karate-config.js actually (globally) instead of in each feature file:

function fn() {
    var config = {
        baseUrl: 'https://qa.mycompany.com'
    };
    if (karate.env == 'chrome') {
        karate.configure('driver', { type: 'chromedriver', start: false, webDriverUrl: 'http://somehost:9515/wd/hub' });
    }
    return config;
}

And on the command-line:

mvn test -Dkarate.env=chrome

I suggest you get familiar with Karate's configuration: https://github.com/intuit/karate#configuration - it actually ends up being simpler than typical Java / Maven projects.

Another way is to set variables in the karate-config.js and then use them in feature files.

* configure driver = { type: '#(myVariableFromConfig)' }

Keep these principles in mind:

  • Any driver instances created by a "top level" feature will be available to "called" features
  • You can even call a "common" feature, create the driver there, and it will be set in the "calling" feature
  • Any driver created will be closed when the "top level" feature ends

You don't need any other patterns.

EDIT: there's some more details in the documentation: https://github.com/intuit/karate/tree/develop/karate-core#code-reuse

And for parallel execution or trying to re-use a single browser for all tests, refer: https://stackoverflow.com/a/62325328/143475




回答2:


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


来源:https://stackoverflow.com/questions/63459745/callsingle-for-login-in-karate-config-js-does-not-work-as-expected-for-karate-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!