2017-01-24 7 views
0

コンソールからメソッドにパラメータを送信するにはどうすればよいですか?Maven/testngを使用してメソッドにパラメータを送信するにはどうすればよいですか?

@Test 
public void test() throws InterruptedException { 
    botClass.Desktop(driver, "same url");  
} 

私はコンソールを介してString URLパラメータを送信する:

私はこのようになります方法を持っています。

たとえば、コンソールにmvn clean test, www.google.comと入力すると、プログラムがGoogleに接続してテストを実行する必要があります。 これは可能ですか?はいの場合は、これを達成するためのアドバイスをお願いします。

答えて

1

次のように行います

1)あなたのSCR /テスト/ resoucesでがtestConfig.propertiesを置く:

url = ${url} 

2)は、テストを書く:

@Test 
    public void test() throws Exception{ 

     String urlParam = null; 

     try { 

      Properties prop = new Properties(); 
      InputStream input = null; 
      input = getClass().getClassLoader().getResourceAsStream("testConfig.properties"); 

      prop.load(input); 

      urlParam = prop.getProperty("url"); 

     }catch (Exception e){} 

     // use urkParam 
    } 

3)をpom.xmlに追加する:

<build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-resources-plugin</artifactId> 
       <version>3.0.2</version> 
      </plugin> 

       <plugin> 
        <groupId>org.apache.maven.plugins</groupId> 
        <artifactId>maven-surefire-plugin</artifactId> 
        <version>2.12.4</version> 
       </plugin> 
     </plugins> 
     <testResources> 
      <testResource> 
       <directory>src/test/resources</directory> 
       <filtering>true</filtering> 
      </testResource> 
     </testResources> 
    </build> 

4)実行達人、次のように:

mvn clean test resources:testResources -Durl=www.google.com 

は今、あなたはMavenのPARAMに基づいてパラメータ化されたURLを取得します。

+0

私はテストを実行しようとするとエラーが発生します [link](http://www.interload.co.il/upload/5985310.png) –

+0

okあなたのテストメソッドとmvnコマンド..とあなたのプロパティファイルの内容 –

+0

テストで不運?これは、おそらくあなたがおそらくいくつかの細部を見逃している可能性があります –

0

ポームファイルに何も変更することなく簡単に解決できる方法が見つかりました。私はクラスString valueFromMaven=System.getProperty("URL");に書きました。コンソールでは、私はmvn clean test -DURL=www.google.comでテストを開始します。

関連する問題