本文介紹如何傳遞Maven pom.xml裏的系統屬性參數到TestNG,文章沿用筆者一向的風格--例子驅動。html
1. 用過WebDriver的都知道,當你啓動Chrome或IE的時候都須要設置系統屬性, 好比java
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, 就再也不須要第一步了。web
而且這個值能夠經過Maven的命令來改寫,好比把路徑改爲 /home/tmp/chromedriver.exe 詳細如何使用見下文。chrome
2. 就像開發在不一樣環境構建系統同樣,每種環境都有各自的配置參數,每一個環境build前手動修改參數,顯然不智能。apache
測試人員在開發自動化測試時也有多個環境,究竟是測試Dev環境呢仍是測試QA環境呢仍是Production呢, 咱們能夠用maven 的 profile 來解決。maven
經過Maven命令傳遞不一樣的環境變量,代碼根據不一樣的變量值,取不一樣的數據來進行初始化。ide
在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 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 文件以下
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
其它操做系統同理。
感謝閱讀,若是您以爲本文的內容對您的學習有所幫助,您能夠點擊右下方的推薦按鈕,您的鼓勵是我創做的動力。