2017-04-16 214 views
1

以下testng.xml文件有三個參數。我不希望參數customerCode被硬編碼。我想通過改變/ diffent customerCode以下testng.xml文件,但我也想有彼得作爲默認customerCode如果萬一我不通過mvn命令行提供一個如下:如何通過mvn命令行傳遞testng.xml參數值

mvn test -DcustomerCode=customer1 -Ptestng.xml 

以下是testng.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > 
<suite name="TestSuite" parallel="false"> 

    <test name="someTest"> 
     <parameter name="browserType" value="chrome_win"></parameter> 
     <parameter name="Url" value="http://regression.mytest.com/?customerCode=" /> 
     <parameter name="customerCode" value="Peter"/> 

     <groups> 
      <run> 
       <exclude name="navigateToHomePage" /> 
      </run> 
     </groups> 
     <classes> 
      <class name="com.automation.customermanagement.createTest.myIntegrationTest" > 
      <methods> 
       <exclude name="deleteCustomer" /> 
      </methods> 
      </class> 
     </classes> 
    </test> 
</suite> 

我該如何做到這一點?有沒有辦法做到這一點?

以下是我的POM文件。我在哪裏可以將customerCode屬性放在配置文件中?

<profile> 
    <id>AddMgmtTest</id> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.5.1</version> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.19.1</version> 
       <configuration> 
        <suiteXmlFiles> 
         <suiteXmlFile>admgmttestng.xml</suiteXmlFile> 
        </suiteXmlFiles> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</profile> 

回答

0

這是我實施的解決方案,它工作的很好!

1)添加下列屬性來POM.xml文件

    <systemProperties> 
         <property> 
          <name>customerCode</name> 
          <value>Subaru</value> 
         </property> 
        </systemProperties> 

2)添加這一行來測試方法

String customerCode= System.getProperty("customerCode"); 

3)上面的行拉動在以下MVN命令行提供customerCode=customer1

mvn test -DcustomerCode = customer1 -Ptestng.xml

如果您不在命令行中提供customerCode,則需要在上述屬性中提供的默認值SubaruPOM.xml文件