13

Selenium WebDriver、Eclipse、TestNG、Surefire プラグインを使用しています。pom.xml から testng.xml ファイルを実行できません。mvn testを使用して pom.xml を実行している間、 src/test/javaにあるファイルを直接実行します。

私の pom.xml コードは

<groupId>angel</groupId>
<artifactId>Angel</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Angel</name>  
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>   </properties>

<dependencies>
          <!-- Java API to access the Client Driver Protocols -->
 <dependency>
     <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.21.0</version>
 </dependency>
  <!-- JExcel API is a java library which provides the ability to read, write, and                 modify Microsoft Excel spreadsheets.-->
  <dependency>
     <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.10</version>
    </dependency>
   <dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.14</version>
</dependency>
 <!-- Java API for manipulate the Microsoft Excel Sheets.  -->  
 <dependency>  
<groupId>org.apache.poi</groupId>  
<artifactId>poi-contrib</artifactId> 
 <version>3.5-beta5</version>   
 </dependency>
 <!-- Java Mail API used to send Mails. -->   <dependency>             <groupId>javax.mail</groupId> 
 <artifactId>mail</artifactId>   <version>1.4.3</version>
</dependency>
<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.3.1</version>
  <scope>test</scope>
</dependency>
 </dependencies>  
  <build>  
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugin</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12</version>
          <configuration>
           <suiteXmlFiles>
               <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
          </suiteXmlFiles>
                 </configuration> 
    </plugin>
 <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
             <source>1.6</source>
            <target>1.6</target>
         </configuration>        </plugin> 

</plugins> 
   </project>

私を助けてください。

私のプロジェクト構造は

 project
--src/main/java
--src/main/resources
--src/test/java
    |
     --my testng class file with @Test method.
--src/test/resources
     |
      --testng.xml
--maven dependencies
--jre system library
--src
   |
    --main
    --test
--target
pom.xml

-- =>フォルダ名 |-- =>サブフォルダ名

私のtestng.xmlファイルは...

  <?xml version="1.0" encoding="UTF-8"?>
  <suite name="Suite1" verbose="1"  >

 <test name="samplePage Test" >
   <classes>
   <class name="TestScripts.SamplePage" >
        <methods>
            <include name = "SamplePageTest_Execution"/>
        </methods>
    </class>
</classes>
 </test>
   <test name="newSamplePage Test" >
    <classes>
   <class name="TestScripts.NewSamplePage" >
        <methods>
            <include name = "NewSamplePage_Execution02"/>
        </methods>
        </class>
    </classes>
   </test> 
 </suite>

私は、pom.xml から testng.xml ファイルを介して SamplePage_Execution メソッドを呼び出したかっただけです。

私の SamplePage_Execution メソッドは

  public class sample{
  @Test
  public static void SamplePageTest_Execution() throws Exception{

String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
boolean testStatus = true;
       driver = new FirefoxDriver();
      driver.get("http://www.google.com"); 

   WebElement searchField = driver.findElement(By.name("q")); 

    searchField.sendKeys(new String [] {"selenium2.0"});

    searchField.submit();

    driver.close();
}}
4

6 に答える 6

17

pom.xml の次のコードはうまく機能しています。

<profiles>
   <profile>
      <id>selenium-tests</id>
      <build>
         <plugins>
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-surefire-plugin</artifactId>
               <version>2.12.4</version>
               <configuration>
                  <suiteXmlFiles>
                     <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                  </suiteXmlFiles>
               </configuration>
            </plugin>     
         </plugins>
      </build>
   </profile>
</profiles>

次のコマンドを使用してテストを実行します。

mvn clean test -U -Pselenium-tests

また、

mvn clean test
于 2012-12-05T06:52:55.730 に答える
3

コマンドラインから直接testng.xmlを呼び出すmavenテストを実行できます

mvn test -Dsurefire.suiteXmlFiles=src/test/resources/testng.xml
于 2016-06-27T10:01:33.577 に答える
2

たとえば、src/test/resources にある testng.xml ファイルの代わりに、suiteXmlFile をクラスからファイルに変更する必要があります。さらに、surefire-plugin (現在のバージョン 2.12) を更新します。もう 1 つのことは、セレン テストは通常​​、単体テストではなく統合テストであるということです。

更新 junit 依存関係の代わりに testng 依存関係を追加する必要があります。したがって、 junit の依存関係を削除し、 maven-surefire-pluginのドキュメントに従って testng の依存関係を追加します。

于 2012-05-10T12:34:54.343 に答える
0

ここに基本的な間違いがあると思います.. pom.xml = Mavenによって維持されるパッケージ定義

また、testing.xml (ユーザーが定義できる任意の xml) が TestNG テスト定義に使用されます。xml 構成を使用して、Test NG を実行するためのカスタム実行設定から独自の構成を作成できます。また、xml ファイルで提供された構成 (テスト名、リスナー クラス名、スイート名、またはクラス名) は、テスト クラスと一致する必要があります。class name = TestScripts.SamplePage を宣言したが、テスト クラス = sample を宣言したように、この xml はテスト クラスを実行しません。小さな例: <class name="com.automation.test.TestA" />それは私のテスト xml にあり、私のテスト クラスは TestA になります。

于 2014-04-25T05:47:54.843 に答える
0
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.adiltutorials.facebook</groupId>
  <artifactId>WebdriverTests</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</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>
          <forkCount>0</forkCount>
          <suiteXmlFiles>
            <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.52.0</version>
    </dependency>
    <dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.8</version>
</dependency>  
</dependencies>

</project>
于 2016-03-14T11:35:49.010 に答える
0

src/test/resources/testng.xml の代わりに直接 testng.xml を与えることができます

私のPOM.xmlは正常に動作しています。これを POM.xml に置き換えます。Maven を更新して、もう一度実行してください。頑張ってください:)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Automation</groupId>
  <artifactId>Automation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</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.18.1</version>
        <configuration>
          <forkCount>0</forkCount>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.46.0</version>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.5.0-b01</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.9.6</version>
    </dependency>
  </dependencies>
</project>
于 2015-08-20T04:59:03.390 に答える