mvn集成测试不会运行我的测试

时间:2013-04-15 19:55:33

标签: maven selenium testng

我正在使用Selenium,TestNG和Maven进行自动化测试。当我在cmd中执行:mvn integration-test时,maven不会运行我的测试。我是Maven的新手,我读了一些例子,但没有找到解决问题的方法。

这是我的pom.xml:

<dependencies>
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <classifier>jdk15</classifier>
      <version>5.11</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>2.31.0</version>
    </dependency>
  </dependencies>

    <build>
        <plugins>
            <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>selenium-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start-server</goal>
                        </goals>
                        <configuration>
                            <background>true</background>
                        </configuration>
                    </execution>
                    <execution>
                          <id>stop-selenium</id>
                          <phase>post-integration-test</phase>
                          <goals>
                              <goal>stop-server</goal>
                          </goals>
                      </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <include>**/MainPage*.java </include>                       
                </configuration>

                <executions>
                    <execution>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>



</project>

这是我的MainPage类:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.testng.Assert.*;

public class MainPage 
{
   static WebDriver driver;

   @BeforeClass
   public void setUp() {
   driver = new FirefoxDriver();
   }

   @Test
   public void OpenRegistrationPage(){
       //GIVEN
       openPage();
       //WHEN
       clickOnRegistration();
       //THEN
       checkIfIsDirected();

   }

   private void checkIfIsDirected() {

       String elementTitle = driver.findElement(By.xpath("//td[@id='center-  col']/div/div/table/tbody/tr/td/div")).getText();
       assertEquals(elementTitle, "Rejestracja");   

        }

   private void clickOnRegistration() {

       WebElement registrationLink = driver.findElement(By.cssSelector("span.icon-rejestracja"));
       registrationLink.click();

        }

   private void openPage() {
       driver.get("https://www.x-kom.pl");

        }

   @AfterClass
   public static void tearDown() {
    driver.close();
   }



}

提前致谢! :)

1 个答案:

答案 0 :(得分:1)

第一件事是使用maven-failsafe-plugin而不是maven-surefire-plugin,因为maven-surefire-plugin用于单元测试,而maven-failsafe-plugin用于运行集成测试。 此外,您需要为集成测试命名,如

 **/IT*.java
 **/*IT.java
 **/*ITCase.java

但在您的情况下,您应该有一个单独的模块,其中包含要在您正在开发的Web应用程序上运行的集成测试。