2017-05-26 10 views
0

shファイルからJavaに動的パラメータを送信します。しかし、私はこのパラムを得ることができません。TestNG:シェルスクリプトからJavaファイルに動的パラメータを送信

以下のように私のコード:

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

私のtestng.xmlファイル:私は以下のような

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

ソースscripts.sh以下のようにscripts.shから "filePathに" パラメータを送信しようとしました。

<?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クラスで、私はfilePathにするのparamを取得しようとしました:

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

} 

出力:パラメータ化された値は次のとおりです。ヌル

だから私はSHファイルからfilePathにダイナミックのparamsを取得することはできません。

私を助けてください。私は何が間違っていますか?ここで

答えて

1

は、あなたがこの

まず以下のようなものにあなたのシェルスクリプトを変更するにはどうすればよいのです。

java -Dfilepath=$1 -cp libs/*:bin org.testng.TestNG 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> 

今すぐあなたの方法を変更するには、以下のようになりますtestng.xmlを修正次のとおりです。

@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)); 
} 
関連する問題