如何启动所有空手道功能,设置将哪个浏览器用作外部 maven 变量 [英] How to launch all Karate features setting up which browser to use as an external maven variable

查看:18
本文介绍了如何启动所有空手道功能,设置将哪个浏览器用作外部 maven 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

比如:

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

或使用 Selenium 网格:

or using a Selenium grid:

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

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

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. 将空手道配置文件定义为一个变量浏览器"
  2. 在我只设置空手道驱动程序的单个功能X"中使用变量浏览器"
  3. 从带有 callonce 的所有其他功能中重新调用功能X"以使用该驱动程序

但它没有用,老实说,在我看来这不是正确的方法.可能能够从功能内的 Javascript 函数设置空手道驱动程序是正确的方法,但我找不到解决方案.

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.

我在空手道中发现的另一个问题是区分使用本地或远程 webdriver 的行为,就像在它们以不同方式设置的功能文件中一样.

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?

推荐答案

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

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

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

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

在命令行上:

mvn test -Dkarate.env=chrome

建议你熟悉一下Karate的配置:https://github.com/intuit/karate#配置 - 它实际上比典型的 Java/Maven 项目更简单.

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.

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

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

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

牢记这些原则:

  • 由顶级"创建的任何驱动程序实例功能将可用于被调用"特点
  • 您甚至可以调用普通"功能,在那里创建驱动程序,它将在调用"中设置.功能
  • 创建的任何驱动程序将在顶级"结束时关闭.功能结束

您不需要任何其他模式.

You don't need any other patterns.

文档中有更多细节:https://github.com/intuit/karate/tree/develop/karate-core#code-reuse

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

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

这篇关于如何启动所有空手道功能,设置将哪个浏览器用作外部 maven 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆