How Maven passes System attribute variables to testng

Source: Internet
Author: User
Tags null null testng

This article describes how to pass the System attribute parameters in Maven Pom.xml to testng, the article follows the author's consistent style-example driven.

Solve what problem

1. Used Webdriver know, when you start chrome or IE, you need to set the system properties, such as

1 system.setproperty ("Webdriver.ie.driver", "D:/temp/resources/chromedriver.exe"); 2        New chromedriver (); 3        Driver.get ("http://www.cnblogs.com");

Through this study, in the Pom.xml to specify the system property Variables by plug-ins, it is no longer necessary the first step.

And this value can be rewritten by Maven's commands, such as changing the path to/home/tmp/chromedriver.exe details how to use it below.

2. As with the development of building systems in different environments, each environment has its own configuration parameters, and it is obviously not smart to manually modify the parameters before each environment build.

Testers in the development of automated testing also have multiple environments, whether to test the dev environment or test the QA environment or production, we can use MAVEN profile to solve.

Different environment variables are passed through the MAVEN command, and the code initializes with different data depending on the value of the variable.

Detailed examples

Define MAVEN Surefire Plugin in Pom.xml

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.xml9</suiteXmlFile>Ten</suiteXmlFiles> One<systemPropertyVariables> A<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> the</systemPropertyVariables> -<testFailureIgnore>true</testFailureIgnore> -</configuration> -</plugin>

Define profile, which assigns a value to the <environment> property, and is activated by default as 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>Ten</profile> One<profile> A<id>DEV</id> -<properties> -<demo.automation.environment>DEV</demo.automation.environment> the</properties> -</profile> -</profiles>

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

@Test a very simple test method to read system property values

1  PackageCom.acxiom.insightlab.automation.util;2 3 Importorg.testng.annotations.Test;4 5 /**6 * @Description: For demo purpose7  * @authorWadexu8  *9 * @updateUserTen * @updateDate One  */ A  Public classDemotest { -  - @Test the      Public voidsimpletets () { -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); ASYSTEM.OUT.PRINTLN ("Test Environment:" +env); at     } -  -}

Run as TestNG directly against the Demotest class, and the value is NULL because these property values are not set at all

NULLNULL nullpassed:simpletets=============================== ================    Default test    1, failures:0, skips:0============================================ ===3 3 ms[testng] time taken by [Testlisteneradapter] Passed:0 failed:0 SKIPPED:0]: 0ms

The

should right-click Pom.xml. Run as, Maven test if you are using an IDE tool, or if you are running MVN test under the Direct Engineering Directory command line

 Chrome Driver path:d:/temp/resources/chromedriver.exeie Driver path:d: / Temp/resources/iedriverserver.exetest environment:qatests run:  1, failures:0, errors:0, skipped:0, time elapsed:0.754 sec- 1, F ailures:0, errors:0, Skipped:0[info] --------------------------------------- ---------------------------------[info] BUILD Success[info] ---------------- --------------------------------------------------------[info] Total time:  5 .138s[info] finished At:thu Oct  16:54:19 CST 2015 [info "Final memory:11m /217m[info] ---- --------------------------------------------------------------------

Note that the <demo.automation.environment> value QA in the default profile is used in the observation results.

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

mvn test-p Dev (this dev is the profile ID) can be run from the command line

Note that the <demo.automation.environment> value Dev in the other profile is used.

Run command from command line plus-d parameter to override variable value in POM

such as running mvn test-p dev-dwebdriver.chrome.driver=c:/temp/chromedriver.exe -dwebdriver.ie.driver=c:/temp/ IEDriverServer.exe

Note the results: Chrome and IE Driver path are overwritten.

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

The entire Pom.xml file is as follows

1<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"2xsi: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 Ten<properties> One<maven-compiler-version>3.1</maven-compiler-version> A<java-base-version>1.7</java-base-version> -<surefire-version>2.16</surefire-version> -</properties> the  -<dependencies> -<dependency> -<groupId>org.testng</groupId> +<artifactId>testng</artifactId> -<version>6.8.7</version> +</dependency> A          at<dependency> -<groupId>org.seleniumhq.selenium</groupId> -<artifactId>selenium-java</artifactId> -<version>2.46.0</version> -</dependency> -</dependencies> in  -<build> to<plugins> +<plugin> -<groupId>org.apache.maven.plugins</groupId> the<artifactId>maven-compiler-plugin</artifactId> *<version>${maven-compiler-version}</version> $<configuration>Panax Notoginseng<source>${java-base-version}</source> -<target>${java-base-version}</target> the</configuration> +</plugin> A<plugin> the<groupId>org.apache.maven.plugins</groupId> +<artifactId>maven-surefire-plugin</artifactId> -<version>2.16</version> $<configuration> $<suiteXmlFiles> -<suiteXmlFile> -${basedir}/src/test/resources/Testngcopy.xml the</suiteXmlFile> -</suiteXmlFiles>Wuyi<systemPropertyVariables> the<webdriver.chrome.driver>D:/temp/resources/chromedriver.exe</webdriver.chrome.driver> -<webdriver.ie.driver>D:/temp/resources/IEDriverServer.exe</webdriver.ie.driver> Wu<environment>${demo.automation.environment}</environment> -</systemPropertyVariables> About<testFailureIgnore>true</testFailureIgnore> $</configuration> -</plugin> -</plugins> -</build> A  +<profiles> the<profile> -<id>QA</id> $<activation> the<activeByDefault>true</activeByDefault> the</activation> the<properties> the<demo.automation.environment>QA</demo.automation.environment> -</properties> in</profile> the<profile> the<id>DEV</id> About<properties> the<demo.automation.environment>DEV</demo.automation.environment> the</properties> the</profile> +</profiles> -  the Bayi</project>
View Code

Other than that:

You can also write this

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

MAVEN will fetch the value of the environment variable driver_path_chrome.

Like write a bat file under Windows

SET Driver_path_chrome=xxx First

Then CD into the project directory

Last run mvn test

Other operating systems are the same.

Thank you for reading, if you think the content of this article is helpful to your study, you can click the Recommendation button at the bottom right, your encouragement is the motive force of my creation.

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

How Maven passes System attribute variables to testng

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.