如何在从命令行触发测试时设置所需的浏览器

huangapple go评论79阅读模式
英文:

How to set desired browser when triggering tests from CLI

问题

我有一个 maven-testng-selenium 框架,我的默认浏览器是 Chrome,但已配置为支持 Firefox 和 IE。

浏览器类型是从代码中选择的,但是当我从 Maven 命令或 testng.xml 文件触发测试时,我需要一个解决方法来进行更改。

有谁知道我如何通过在命令行中传递浏览器名称作为变量来更改浏览器?

英文:

I have a maven-testng-selenium framework where my default browser to run the tests is Chrome, but is configured to support Firefox and IE as well.

Browser type is selected from code, but I need a workaround to change it when I trigger the tests from
maven command or testng.xml file.

Does anyone know how I can change the browser by passing it's name as a variable in CLI?

答案1

得分: 1

只需使用系统属性传递参数,这样您就能够像从命令行一样使用-Dproperty.name来参数化您的浏览器,就像从sure-fire设置中一样。就像这样:

<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version>
        <configuration>
          <systemPropertyVariables>
            <propertyName>firefox</propertyName>
          </systemPropertyVariables>
        </configuration>
      </plugin>
    [...]
</plugins>

您还可以在这里找到更多示例链接

在您的测试代码中,您将读取属性值并设置您的运行时以使用适当的浏览器。

英文:

Just pass the parameter using system property so that you will be able to parameterize your browser as from command line like -Dproperty.name as from sure-fire settings. Like:

<plugins>
    [...]
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version>
        <configuration>
          <systemPropertyVariables>
            <propertyName>firefox</propertyName>
          </systemPropertyVariables>
        </configuration>
      </plugin>
    [...]
</plugins>

You can also find more examples here.

In your test code you will read property value and set up your runtime to use the appropriate browser.

huangapple
  • 本文由 发表于 2020年10月20日 16:41:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64441530.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定