Maven如何传递系统属性变量到TestNG

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

解决什么问题

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

1     System.setProperty("webdriver.ie.driver", "D:/temp/resources/chromedriver.exe");
2        WebDriver driver = new ChromeDriver();
3        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

 1         <plugin>
 2                 <groupId>org.apache.maven.plugins</groupId>
 3                 <artifactId>maven-surefire-plugin</artifactId>
 4                 <version>2.16</version>
 5                 <configuration>
 6                     <suiteXmlFiles>
 7                         <suiteXmlFile>
 8                             ${basedir}/src/test/resources/testSuite.xml
 9                         </suiteXmlFile>
10                     </suiteXmlFiles>
11                     <systemPropertyVariables>
12                         <webdriver.chrome.driver>D:/temp/resources/chromedriver.exe</webdriver.chrome.driver>
13                         <webdriver.ie.driver>D:/temp/resources/IEDriverServer.exe</webdriver.ie.driver>
14                         <environment>${demo.automation.environment}</environment>
15                     </systemPropertyVariables>
16                     <testFailureIgnore>true</testFailureIgnore>
17                 </configuration>
18             </plugin>

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

 1 <profiles>
 2         <profile>
 3             <id>QA</id>
 4             <activation>
 5                 <activeByDefault>true</activeByDefault>
 6             </activation>
 7             <properties>
 8                 <demo.automation.environment>QA</demo.automation.environment>
 9             </properties>
10         </profile>
11         <profile>
12             <id>DEV</id>
13             <properties>
14                 <demo.automation.environment>DEV</demo.automation.environment>
15             </properties>
16         </profile>
17     </profiles>

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

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

 1 package com.acxiom.insightlab.automation.util;
 2
 3 import org.testng.annotations.Test;
 4
 5 /**
 6  * @Description: For demo purpose
 7  * @author wadexu
 8  *
 9  * @updateUser
10  * @updateDate
11  */
12 public class DemoTest {
13
14     @Test
15     public void simpleTets() {
16         String chromePath = System.getProperty("webdriver.chrome.driver");
17         String iePath = System.getProperty("webdriver.ie.driver");
18         String env = System.getProperty("environment");
19
20         System.out.println("Chrome Driver Path: "+ chromePath);
21         System.out.println("IE Driver Path: "+ iePath);
22         System.out.println("Test Environment: "+ env);
23     }
24
25 }

直接针对这个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 [email protected]: 41 ms
[TestNG] Time taken by [email protected]: 3 ms
[TestNG] Time taken by [email protected]: 3 ms
[TestNG] Time taken by [TestListenerAdapter] Passed:0 Failed:0 Skipped:0]: 0 ms
[TestNG] Time taken by [email protected]: 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 文件如下

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3     <modelVersion>1.0.0</modelVersion>
 4     <groupId>com.wadeshop.demo.automation</groupId>
 5     <artifactId>demo-automation</artifactId>
 6     <packaging>jar</packaging>
 7     <version>1.0.0-SNAPSHOT</version>
 8     <name>demo-automation</name>
 9
10     <properties>
11         <maven-compiler-version>3.1</maven-compiler-version>
12         <java-base-version>1.7</java-base-version>
13         <surefire-version>2.16</surefire-version>
14     </properties>
15
16     <dependencies>
17         <dependency>
18             <groupId>org.testng</groupId>
19             <artifactId>testng</artifactId>
20             <version>6.8.7</version>
21         </dependency>
22
23         <dependency>
24             <groupId>org.seleniumhq.selenium</groupId>
25             <artifactId>selenium-java</artifactId>
26             <version>2.46.0</version>
27         </dependency>
28     </dependencies>
29
30     <build>
31         <plugins>
32             <plugin>
33                 <groupId>org.apache.maven.plugins</groupId>
34                 <artifactId>maven-compiler-plugin</artifactId>
35                 <version>${maven-compiler-version}</version>
36                 <configuration>
37                     <source>${java-base-version}</source>
38                     <target>${java-base-version}</target>
39                 </configuration>
40             </plugin>
41                       <plugin>
42                 <groupId>org.apache.maven.plugins</groupId>
43                 <artifactId>maven-surefire-plugin</artifactId>
44                 <version>2.16</version>
45                 <configuration>
46                     <suiteXmlFiles>
47                         <suiteXmlFile>
48                             ${basedir}/src/test/resources/testngCopy.xml
49                         </suiteXmlFile>
50                     </suiteXmlFiles>
51                     <systemPropertyVariables>
52                         <webdriver.chrome.driver>D:/temp/resources/chromedriver.exe</webdriver.chrome.driver>
53                         <webdriver.ie.driver>D:/temp/resources/IEDriverServer.exe</webdriver.ie.driver>
54                         <environment>${demo.automation.environment}</environment>
55                     </systemPropertyVariables>
56                     <testFailureIgnore>true</testFailureIgnore>
57                 </configuration>
58             </plugin>
59         </plugins>
60     </build>
61
62     <profiles>
63         <profile>
64             <id>QA</id>
65             <activation>
66                 <activeByDefault>true</activeByDefault>
67             </activation>
68             <properties>
69                 <demo.automation.environment>QA</demo.automation.environment>
70             </properties>
71         </profile>
72         <profile>
73             <id>DEV</id>
74             <properties>
75                 <demo.automation.environment>DEV</demo.automation.environment>
76             </properties>
77         </profile>
78     </profiles>
79
80
81 </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

时间: 2024-12-15 01:42:14

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

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

java中获取系统属性以及环境变量 System.getEnv()和System.getProperties()的差别 从概念上讲,系统属性 和环境变量 都是名称与值之间的映射.两种机制都能用来将用户定义的信息传递给 Java 进程.环境变量产生很多其它的全局效应,由于它们不仅对Java 子进程可见,并且对于定义它们的进程的全部子进程都是可见的.在不同的操作系统上,它们的语义有细微的区别,比方,不区分大写和小写.由于这些原因,环境变量更可能有意料不到的副作用.最好在可能的地方使用系统属性.环境变

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

当程序中需要使用与操作系统相关的变量(例如:文件分隔符.换行符)时,Java提供了System类的静态方法getenv()和getProperty()用于返回系统相关的变量与属性,getenv方法返回的变量大多于系统相关,getProperty方法返回的变量大多与java程序有关. 系统属性和环境变量都是名称与值之间的映射.两种机制都能用来将用户定义的信息传递给 Java进程.环境变量产生更多的全局效应,因为它们不仅对Java子进程可见,而且对于定义它们的进程的所有子进程都是可见的.在不同的操作

Java获取系统环境变量(System Environment Variable)和系统属性(System Properties)以及启动http://m.jb51.net/article/83454.htm参数的方法

系统环境变量(System Environment Variable): 在Linux下使用export $ENV=123指定的值.获取的方式如下: Map<String,String> map = System.getenv(); Set<Map.Entry<String,String>> entries = map.entrySet(); for (Map.Entry<String, String> entry : entries) { System.o

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

环境变量的读取以及系统属性的设置 环境变量只能读取,不能修改,系统属性可以修改 系统变量的读取方式: System.getEnv() 系统属性有多重读取和修改方式: 其修改方式为: 读取系统属性: @Autowired AbstractEnvironment environment; System.setProperty("today","tuesday"); environment.getProperty("test"); 增加新的系统属性:

Android SystemProperties系统属性详解

Systemproperties类在android.os下,但这个类是隐藏的,上层程序开发无法直接使用,用Java的反射机制就可以了.Java代码中创建与修改android属性用Systemproperties.set(name, value),获取android属性用Systemproperties.get(name),Native代码中通过property_get(const char *key, char *value, const char *default_value)/propert

Android 系统属性SystemProperty分析

http://www.cnblogs.com/bastard/archive/2012/10/11/2720314.html Android System Property 一 System Property 代码中大量存在:SystemProperties.set()/SystemProperties.get():通过这两个接口可以对系统的属性进行读取/设置, 顾名思义系统属性,肯定对整个系统全局共享.通常程序的执行以进程为单位各自相互独立,如何实现全局共享呢? System Properti

Mac系统搭建java+selenium+testng环境

Mac系统搭建java+selenium+testng环境: 1.   配置java环境,安装eclipse 2.  离线安装testng插件 3.  配置maven环境 4.  安装谷歌浏览器,下载对应浏览器版本的chromedriver 陆陆续续遇到的一些小问题记录: 1.  testng是使用离线方式安装的,离线包下载地址:http://dl.bintray.com/testng-team/testng-eclipse-release/ 下载完成后将其放到eclipse的dropins文件

Maven的内置属性

Maven共有6类属性: ①内置属性(Maven预定义属性,用户可以直接使用) ${basedir}表示项目的根路径,即包含pom.xml文件的目录 ${version}表示项目版本 ${project.basedir}同${basedir} ${project.baseUri}表示项目文件地址 ${maven.build.timestamp}表示项目构建开始时间 ${maven.build.timestamp.format}表示${maven.build.timestamp}的展示格式,默认值

iOS 成员变量,实例变量,属性变量的区别,联系

在ios第一版中: 我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个新的机制,并且要求你必须声明与之对应的实例变量,例如: 注意:(这个是以前的用法) @interface MyViewController :UIViewController { UIButton *myButton; } @property (nonatomic, retain) UIButton *myButton; @end 在现在iOS版本中: 苹果将默认编译器从GCC转换为LLVM(low leve