如何启动所有空手道功能,设置哪个浏览器用作外部 Maven 变量

发布于 2025-01-10 04:43:33 字数 713 浏览 0 评论 0原文

我试图找到一种方法,通过 maven 使用外部变量来设置浏览器(使用本地网络驱动程序或使用 Selenium 网格)来启动空手道测试中的所有功能。

所以类似:

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

或使用 Selenium 网格:

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

使用 Cucumber 和 Java,使用单例来设置然后在所有测试中使用的全局 Web 驱动程序非常简单。通过这种方式,我可以使用不同的本地或远程网络驱动程序运行测试。

在空手道中,我尝试了不同的解决方案,最后一个是:

  1. 定义空手道配置文件变量“浏览器”
  2. 在单个功能“X”中使用变量“浏览器”,其中我仅设置
  3. 所有其他功能中的 空手道驱动程序曾经调用过使用该驱动程序的功能“X”

,但它不起作用,说实话,在我看来这不是正确的方法。 也许能够从功能内的 Javascript 函数设置空手道驱动程序是正确的方法,但我无法找到解决方案。

我发现空手道的另一个问题是使用本地或远程网络驱动程序来区分行为,因为它们在以不同方式设置的功能文件中。

那么有人有我同样的需求吗?我该如何解决呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心如狂蝶 2025-01-17 04:43:33

根据 Peter Thomas 的建议,我使用了这个 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;
}

通过这种方式,我能够调用测试套件,指定直接从命令行使用的浏览器(在 Jenkins 管道中使用):

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

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
变身佩奇 2025-01-17 04:43:33

这里有几个原则。 Karate 负责启动 driver(相当于 Selenium WebDriver)。您需要做的就是按照此处所述设置配置驱动程序https://github.com/intuit/karate/tree/master/karate-core#configure-driver

最后,根据您的环境,只需切换驱动程序配置即可。这实际上可以在 karate-config.js 中(全局)轻松完成,而不是在每个功能文件中:

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;
}

在命令行上:

mvn test -Dkarate.env=chrome

我建议您熟悉空手道的配置:https://github.com/intuit/karate#configuration - 它实际上比典型的 Java / Maven 更简单项目。

另一种方法是在 karate-config.js 中设置变量,然后在功能文件中使用它们。

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

请记住这些原则:

  • 由“顶级”功能创建的任何驱动程序实例都可用于“被调用”功能
  • 您甚至可以调用“通用”功能,在那里创建驱动程序,并将其设置在“调用”功能中 您
  • 当“顶级”功能结束时,创建的任何驱动程序都将关闭

不需要任何其他模式。

编辑:文档中有更多详细信息: https:// github.com/intuit/karate/tree/develop/karate-core#code-reuse

对于并行执行或尝试在所有测试中重复使用单个浏览器,请参阅:https://stackoverflow.com/a/60387907/143475

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/60387907/143475

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文