【问题标题】:maven selenium integration testmaven selenium 集成测试
【发布时间】:2011-10-17 10:00:13
【问题描述】:

我使用的是硒 2.8。我遇到了这样一个疯狂的错误:

testPersistence(com.***.***.selenium.test.PersistenceTest) 已用时间:0.032 秒 

我的测试类很简单。它有一个像这样的测试:

@Test
public void testPersistence() throws InterruptedException {
    DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://localhost:8080");
    selenium.start();
    selenium.waitForPageToLoad("30000");
    selenium.open("/***/register.seam");
    selenium.waitForPageToLoad("30000");
    selenium.type("registration:username", "jackman");
    Thread.sleep(5000);
    selenium.type("registration:name", "Jack Daniels");
    Thread.sleep(5000);
    selenium.type("registration:password", "123456789");
    Thread.sleep(5000);
    selenium.click("registration:register");
    selenium.waitForPageToLoad("30000");
    Thread.sleep(5000);
    assertTrue(selenium.isTextPresent("regexpi:Welcome"));
    selenium.stop();
}

谁能帮帮我?

提前致谢

【问题讨论】:

  • 从错误堆栈的顶部,它抱怨它无法启动 selenium。您的环境设置正确吗?您是否启动了 selenium 服务器?您是否添加了所需的依赖项?

标签: java maven selenium


【解决方案1】:

你的 pom.xml 丢失了,很难判断出了什么问题。

但是,在一个简单的测试项目中,我只需要在 pom.xml 的“依赖项”部分中添加以下内容(注意,我使用的是 Selenium 2.22.0 而不是 2.8):

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <scope>test</scope>
        <version>2.22.0</version>
    </dependency>

我使用 TestNG 调用我的测试,但它也应该适用于 JUnit。

我的测试用例如下所示。我删除了与代理设置相关的所有内容,因此该示例甚至可以简化:

    FirefoxProfile profile = new FirefoxProfile();
    FirefoxBinary firefoxBinary = new FirefoxBinary();
    WebDriver driver = new FirefoxDriver(firefoxBinary, profile);
    driver.get("http://www.google.com");
    Assert.assertEquals("Google", driver.getTitle().trim());

所以一个问题可能是您使用的是过时版本的 Selenium (2.8)。此外,您设置 DefaultSelenium 的方式对我来说也是错误的。此外,对于 Firefox,您不需要运行 Selenium 服务器。

我不明白的另一件事是在启动 selenium 后直接等待 30 秒。你还在等什么?

【讨论】:

  • @Frank 这可能是问题的一部分。看起来他复制了一些过时的示例代码。但是如果接近 Selenium 之类的东西,我会首先尝试使用最新的稳定版本(哦,我需要用 2.22.0 更新我的帖子)。
  • 没错-我还建议使用最新版本(谢谢告诉我他们更新到 2.22),但也许她/他有理由使用 2.8,所以您可能会指出您更改了版本。特别是因为(s)他根本没有绒球......;)
【解决方案2】:

The Maven Cookbook answer 使用 Maven 运行 Selenium 测试。

【讨论】:

    【解决方案3】:

    正如@Ozyman 所指出的,您似乎还没有启动 Selenium 服务器。如果您想使用 Selenium RC 创建测试,Selenium Server 必须在后台运行。

    您可以在执行集成测试之前使用Selenium Maven Plugin 启动 Selenium 服务器,方法是将以下插件配置添加到 ... 。

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>selenium-maven-plugin</artifactId>
      <version>2.3</version>
      <executions>
        <execution>
          <phase>pre-integration-test</phase>
          <goals>
            <goal>start-server</goal>
          </goals>
          <configuration>
            <background>true</background>
          </configuration>
        </execution>
      </executions>
      <dependencies>
        <!-- Get the plugin to use the latest version of Selenium drivers -->
        <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-server</artifactId>
          <version>2.26.0</version>
        </dependency>
      </dependencies>
    </plugin>
    

    您只需将单元测试从 PersistenceTest 重命名为 ITPersistenceTest 即可将其转换为集成测试。如果您将以下插件配置添加到 ... ,Maven Failsafe Plugin 将运行集成测试。

    <plugin>
      <artifactId>maven-failsafe-plugin</artifactId>
      <version>2.12.4</version>
      <executions>
        <execution>
         <id>run-integration-tests</id>
         <phase>integration-test</phase>
         <goals>
           <goal>integration-test</goal>
         </goals>
         <inherited>false</inherited>
       </execution>
     </executions>
    

    在 Selenium Maven 插件的网站上,guidance 介绍了如何在构建生命周期的集成测试阶段使用强制运行 Maven Surefire 插件。

    如果您要编写大量基于 Selenium 的单元测试,您可能会发现 Selenium JUnit 4 Class Runner 在减少您需要添加的样板数量方面很有用。

    【讨论】:

      猜你喜欢
      • 2013-03-20
      • 2016-07-26
      • 2012-12-16
      • 2016-07-25
      • 2019-08-31
      • 2016-12-04
      • 2016-01-23
      • 2016-01-21
      • 1970-01-01
      相关资源
      最近更新 更多