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

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

问题描述

我试图找到一种方法,使用外部变量来设置浏览器(使用本地Web驱动程序或使用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)

或使用硒网格:

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

使用Cucumber和Java,使用单例设置全局Web驱动程序非常简单,然后将其用于所有测试中.这样,我可以使用不同的本地或远程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函数设置Karate驱动程序是正确的方法,但我找不到解决方案.

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?

推荐答案

以下是一些原则.空手道负责启动driver(与硒WebDriver等效).您需要做的就是按照以下说明设置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

我建议您熟悉空手道的配置: 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.

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

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