本文介绍如何传递Maven pom.xml里的系统属性参数到TestNG,文章沿用笔者一贯的风格--例子驱动。

解决什么问题

1. 用过WebDriver的都知道,当你启动Chrome或IE的时候都需要设置系统属性, 比如

     System.setProperty("webdriver.ie.driver", "D:/temp/resources/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.cnblogs.com");

经过本文的学习,在pom.xml里通过插件指定system Property Variables, 就不再需要第一步了。

并且这个值可以通过Maven的命令来改写,比如把路径改成 /home/tmp/chromedriver.exe 详细如何使用见下文。

2. 就像开发在不同环境构建系统一样,每种环境都有各自的配置参数,每个环境build前手动修改参数,显然不智能。

测试人员在开发自动化测试时也有多个环境,到底是测试Dev环境呢还是测试QA环境呢还是Production呢, 我们可以用maven 的 profile 来解决。

通过Maven命令传递不同的环境变量,代码根据不同的变量值,取不同的数据来进行初始化。

例子详解

在pom.xml里定义Maven surefire plugin

         <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>
${basedir}/src/test/resources/testSuite.xml
</suiteXmlFile>
</suiteXmlFiles>
<systemPropertyVariables>
<webdriver.chrome.driver>D:/temp/resources/chromedriver.exe</webdriver.chrome.driver>
<webdriver.ie.driver>D:/temp/resources/IEDriverServer.exe</webdriver.ie.driver>
<environment>${demo.automation.environment}</environment>
</systemPropertyVariables>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>

定义 profile, 用来给<environment>属性赋值,默认激活的为QA profile

 <profiles>
<profile>
<id>QA</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<demo.automation.environment>QA</demo.automation.environment>
</properties>
</profile>
<profile>
<id>DEV</id>
<properties>
<demo.automation.environment>DEV</demo.automation.environment>
</properties>
</profile>
</profiles>

##转载注明出处:http://www.cnblogs.com/wade-xu/p/4857471.html

@Test 很简单的测试方法 读取系统属性值

 package com.acxiom.insightlab.automation.util;

 import org.testng.annotations.Test;

 /**
* @Description: For demo purpose
* @author wadexu
*
* @updateUser
* @updateDate
*/
public class DemoTest { @Test
public void simpleTets() {
String chromePath = System.getProperty("webdriver.chrome.driver");
String iePath = System.getProperty("webdriver.ie.driver");
String env = System.getProperty("environment"); System.out.println("Chrome Driver Path: "+ chromePath);
System.out.println("IE Driver Path: "+ iePath);
System.out.println("Test Environment: "+ env);
} }

直接针对这个DemoTest类来 Run as TestNG, 取值都是Null, 因为根本没设置这些属性值

Chrome Driver Path: null
IE Driver Path: null
Test Environment: null
PASSED: simpleTets ===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
=============================================== [TestNG] Time taken by org.testng.reporters.XMLReporter@3366184d: 41 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter@999c305: 3 ms
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@7ced5732: 3 ms
[TestNG] Time taken by [TestListenerAdapter] Passed:0 Failed:0 Skipped:0]: 0 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@2eb95569: 38 ms

应该右击pom.xml -> Run as -> Maven test 如果你用的IDE工具, 或者直接工程目录命令行下运行 mvn test

Chrome Driver Path: D:/temp/resources/chromedriver.exe
IE Driver Path: D:/temp/resources/IEDriverServer.exe
Test Environment: QA
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.754 sec - in TestSuite Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.138s
[INFO] Finished at: Thu Oct 08 16:54:19 CST 2015
[INFO] Final Memory: 11M/217M
[INFO] ------------------------------------------------------------------------

注意观察结果,默认的profile 里的 <demo.automation.environment> 值QA被用到了。

##转载注明出处:http://www.cnblogs.com/wade-xu/p/4857471.html

可以通过命令行运行 mvn test -P DEV   (这个DEV是Profile的id)

注意观察结果,另一个profile 里的 <demo.automation.environment> 值DEV被用到了。

通过命令行运行命令加 -D参数 可以覆盖pom里的变量值

比如运行 mvn test -P DEV -Dwebdriver.chrome.driver=C:/temp/chromedriver.exe -Dwebdriver.ie.driver=C:/temp/IEDriverServer.exe

注意观察结果: Chrome 和 IE Driver path 被覆盖了。

##转载注明出处:http://www.cnblogs.com/wade-xu/p/4857471.html

整个pom.xml 文件如下

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>1.0.0</modelVersion>
<groupId>com.wadeshop.demo.automation</groupId>
<artifactId>demo-automation</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>demo-automation</name> <properties>
<maven-compiler-version>3.1</maven-compiler-version>
<java-base-version>1.7</java-base-version>
<surefire-version>2.16</surefire-version>
</properties> <dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.7</version>
</dependency> <dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.46.0</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-version}</version>
<configuration>
<source>${java-base-version}</source>
<target>${java-base-version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>
${basedir}/src/test/resources/testngCopy.xml
</suiteXmlFile>
</suiteXmlFiles>
<systemPropertyVariables>
<webdriver.chrome.driver>D:/temp/resources/chromedriver.exe</webdriver.chrome.driver>
<webdriver.ie.driver>D:/temp/resources/IEDriverServer.exe</webdriver.ie.driver>
<environment>${demo.automation.environment}</environment>
</systemPropertyVariables>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build> <profiles>
<profile>
<id>QA</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<demo.automation.environment>QA</demo.automation.environment>
</properties>
</profile>
<profile>
<id>DEV</id>
<properties>
<demo.automation.environment>DEV</demo.automation.environment>
</properties>
</profile>
</profiles> </project>

另外:

还可以这样写

<webdriver.chrome.driver>${DRIVER_PATH_CHROME}</webdriver.chrome.driver>

Maven会去取环境变量DRIVER_PATH_CHROME的值。

比如在Windows下写个bat 文件

先 SET DRIVER_PATH_CHROME=xxx

然后 cd 到 工程目录下

最后 运行 mvn test

其它操作系统同理。

 

感谢阅读,如果您觉得本文的内容对您的学习有所帮助,您可以点击右下方的推荐按钮,您的鼓励是我创作的动力。

##转载注明出处:http://www.cnblogs.com/wade-xu/p/4857471.html

Maven如何传递系统属性变量到TestNG的更多相关文章

  1. Ant 脚本打印系统属性变量、ant内置属性

    Ant 脚本打印系统属性变量.ant内置属性 作用 编写ant脚本的时候,经常会引用到系统属性,本脚本用于打印系统常用属性(System.getProperties)与环境变量(Environment ...

  2. java中获取系统属性以及环境变量

    java中获取系统属性以及环境变量 System.getEnv()和System.getProperties()的差别 从概念上讲,系统属性 和环境变量 都是名称与值之间的映射.两种机制都能用来将用户 ...

  3. Spring中如何向 Bean注入系统属性或环境变量

    [转自] http://unmi.cc/spring-injection-system-properties-env/ 在 Spring 中为 javabean 注入属性文件中的属性值一般人都知道的, ...

  4. Java获取系统属性及环境变量

    当程序中需要使用与操作系统相关的变量(例如:文件分隔符.换行符)时,Java提供了System类的静态方法getenv()和getProperty()用于返回系统相关的变量与属性,getenv方法返回 ...

  5. Java获取系统环境变量(System Environment Variable)和系统属性(System Properties)以及启动参数的方法

    系统环境变量(System Environment Variable): 在Linux下使用export $ENV=123指定的值.获取的方式如下: Map<String,String> ...

  6. Java获取环境变量和系统属性

    Java获取服务器环境变量和JVM系统变量    当程序中需要使用与操作系统相关的变量(例如:文件分隔符.换行符)时,Java提供了System类的静态方法getenv()和getProperty() ...

  7. Java取得环境变量和系统属性

    取得所有的环境变量 public class GetEnvAndProp { public static void main(String[] args) { Map<String, Strin ...

  8. springboot读取系统级环境变量,和读写系统属性以及unittest来获取环境变量的方法

    环境变量的读取以及系统属性的设置 环境变量只能读取,不能修改,系统属性可以修改 系统变量的读取方式: System.getEnv() 系统属性有多重读取和修改方式: 其修改方式为: 读取系统属性: @ ...

  9. JVM系统属性 OS环境变量 JVM启动参数

    JVM系统属性(System Properties) 1.不支持通过文件查看和设置系统属性 2.可以通过JDK自带的工具jvisulavm.exe查看 3.可以在Java程序中使用API来查看系统属性 ...

随机推荐

  1. (实用篇)php支付宝接口用法分析

    本文实例讲述了php支付宝接口用法.分享给大家供大家参考.具体分析如下: 现在流行的网站支持平台,支付宝当仁不让的老大了,现在我们就来告诉你如何使用支付宝api来做第三方支付,把支付宝放到自己网站来, ...

  2. 解析C语言结构体对齐(内存对齐问题)

    C语言结构体对齐也是老生常谈的话题了.基本上是面试题的必考题.内容虽然很基础,但一不小心就会弄错.写出一个struct,然后sizeof,你会不会经常对结果感到奇怪?sizeof的结果往往都比你声明的 ...

  3. IL命令

    部分CIL 操作码 操作码                                           作用 add, sub, mul, div, rem                   ...

  4. C++ STL泛型编程——在ACM中的运用

    学习过C++的朋友们应该对STL和泛型编程这两个名词不会陌生.两者之间的关系不言而喻,泛型编程的思想促使了STL的诞生,而STL则很好地体现了泛型编程这种思想.这次想简单说一下STL在ACM中的一些应 ...

  5. UVa 673 平衡的括号

    题意:给出包含"()"和"[]"的括号序列,判断是否合法. 用栈来完成,注意空串就行. #include<iostream> #include< ...

  6. 自执行函数与setTimeout结合计算

    var v1=0,v2=0,v3=0;        for(var i=1;i<=3;i++){            var i2=i;            (function(){   ...

  7. CE取系统时间值

    取时间值: void GetTime() { //取时间值 SYSTEMTIME st = {}; // 时间结构体 GetLocalTime(&st); // 把获取的系统时间信息存储到SY ...

  8. Oracle、MySql、SQLServer数据分页查询

    看过此博文后Oracle.MySql.SQLServer 数据分页查询,在根据公司的RegionRes表格做出了 SQLserver的分页查询语句: 别名.字段 FROM( SELECT row_nu ...

  9. 【Unity3D基础教程】给初学者看的Unity教程(四):通过制作Flappy Bird了解Native 2D中的RigidBody2D和Collider2D

    作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 引子 在第一篇文章[Unity3D基础教程] ...

  10. linux开机启动mongodb

    方式一(不推荐) ubuntu编辑/etc/rc.local /home/wyt/bin/mongodb-linux-x86_64-ubuntu1404-3.2.8/bin/mongod --dbpa ...