2017-05-26 57 views
0

我想從sh文件發送一個動態參數給我的Java。但我無法得到這個參數。TestNG:從Shell腳本發送動態參數到Java文件

我的代碼象下面這樣:我試圖從scripts.sh發送 「文件路徑」 參數如下圖所示

$./scripts.sh "/opt/test.apk" 

源scripts.sh象下面這樣:

java -cp libs/*:bin org.testng.TestNG testng.xml -filePath $1 

我的testng.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
    <suite name="Suite"> 
    <test name="Test"> 
    <classes>   
     <parameter name="filePath" value="${filePath}"></parameter> 
     <class name="example.FullTestAndroidApp"/> 
    </classes> 
    </test> <!-- Test --> 
</suite> <!-- Suite --> 

和Java類中,我試圖讓文件路徑PARAM:

public class FullTestAndroidApp { 
@BeforeMethod 
@Parameters("filePath") 
public void initContext(@Optional String filePath) throws MalformedURLException { 
     System.out.println("Parameterized value is : " + filePath); 

} 

輸出:參數值是:空

所以我不能從SH文件中獲取文件路徑動態PARAMS。

請幫幫我。我錯了什麼?

回答

1

這裏是你如何做到這一點

首先改變你的shell腳本類似下面:

java -Dfilepath=$1 -cp libs/*:bin org.testng.TestNG testng.xml

你修改testng.xml看起來像下面

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="Suite"> 
    <test name="Test"> 
     <classes> 
      <!-- [/opt/test.apk] would be the default value of filePath--> 
      <parameter name="filePath" value="/opt/test.apk"/> 
      <class name="example.FullTestAndroidApp"/> 
     </classes> 
    </test> 
</suite> 

現在改變你的方法如下

@BeforeMethod 
@Parameters("filePath") 
public void initContext(@Optional String filePath) throws MalformedURLException { 
    //We query the JVM property "filepath" and if its not defined then we fall back to the 
    //parameter that was sent to us via the suite xml 
    System.out.println("Parameterized value is : " + System.getProperty("filepath", filePath)); 
}