英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论