0

我正在使用 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();
   }



}

提前致谢!:)

4

1 回答 1

1

首先是使用maven-failsafe-plugin而不是maven-surefire-plugin,因为 maven-surefire-plugin 用于单元测试,而 maven-failsafe-plugin 用于运行集成测试。此外,您需要将集成测试命名为

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

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

于 2013-04-15T20:03:37.890 回答