简体   繁体   English

maven selenium整合测试

[英]maven selenium integration test

I'm using selenium 2.8. 我正在使用硒2.8。 I am getting a crazy error like this: 我得到一个像这样的疯狂错误:

testPersistence(com.***.***.selenium.test.PersistenceTest)  Time elapsed: 0.032 sec  <<< ERROR!
java.lang.RuntimeException: Could not start Selenium session: ^@
        at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:107)
        at com.***.***.selenium.test.PersistenceTest.testPersistence(PersistenceTest.java:37)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:120)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:103)
        at org.apache.maven.surefire.Surefire.run(Surefire.java:169)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
        at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)
Caused by: com.thoughtworks.selenium.SeleniumException: ^@
        at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:112)
        at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:106)
        at com.thoughtworks.selenium.HttpCommandProcessor.getString(HttpCommandProcessor.java:275)
        at com.thoughtworks.selenium.HttpCommandProcessor.start(HttpCommandProcessor.java:237)
        at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:98)
        ... 28 more

my test class is very simple. 我的测试课很简单。 its got a test like thislike this: 它有这样的测试:

@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();
}

Can anyone help me please? 有人可以帮我吗?

thanks in advance 提前致谢

Your pom.xml is missing, so hard to judge what's going wrong. 你的pom.xml丢失了,很难判断出了什么问题。

However, in a simple test project, I only need in my pom.xml the following in the "dependencies" section (note, I'm using Selenium 2.22.0 instead of 2.8): 但是,在一个简单的测试项目中,我只需要在我的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>

I invoke my tests using TestNG, but it should also work with JUnit. 我使用TestNG调用我的测试,但它也应该与JUnit一起使用。

My test case looks as follows. 我的测试用例如下所示。 I stripped out everything related to proxy settings, so the example could probably even be simplified: 我删除了与代理设置相关的所有内容,因此甚至可以简化示例:

    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());

So one problem could be that you are using an outdated version of Selenium (2.8). 所以一个问题可能是你使用的是过时版本的Selenium(2.8)。 Also the way you set up your DefaultSelenium looks wrong to me. 你设置DefaultSelenium的方式对我来说也是错误的。 Also, for Firefox you don't need the Selenium server running. 此外,对于Firefox,您不需要运行Selenium服务器。

Another thing I don't understand is the wait for 30 seconds directly after starting selenium. 我不明白的另一件事是在开始硒后直接等待30秒。 What are you waiting for? 你在等什么?

Maven Cookbook回答了使用Maven运行Selenium测试。

As @Ozyman has pointed out it looks like you have not started the Selenium Server. 正如@Ozyman指出的那样,你似乎没有启动Selenium Server。 The Selenium Server must be running in the background if you want to use Selenium RC to create your tests. 如果要使用Selenium RC创建测试,Selenium Server必须在后台运行。

You can use the Selenium Maven Plugin to launch the Selenium Server before executing integration tests by adding the the following plugin configuration to ... . 您可以使用Selenium Maven插件在执行集成测试之前启动Selenium Server,方法是将以下插件配置添加到....

<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>

You can turn your unit test into an integration test simply by renaming it from PersistenceTest to ITPersistenceTest. 只需将其从PersistenceTest重命名为ITPersistenceTest,即可将单元测试转换为集成测试。 The Maven Failsafe Plugin will run the integration tests if you add the following plugin configuration to ... . 如果您将以下插件配置添加到..., Maven Failsafe插件将运行集成测试。

<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>

On the site for the Selenium Maven Plugin there is guidance of how to use the force the Maven Surefire Plugin to run as part of the integration-test phase of the build life cycle. 在Selenium Maven插件的网站上,有关如何使用Maven Surefire插件作为构建生命周期的集成测试阶段的一部分运行的指导

If you are going to be writing a lot of Selenium based unit tests you might find the S elenium JUnit 4 Class Runner useful in reducing the amount of boiler plate you need to add. 如果您要编写大量基于Selenium的单元测试,您可能会发现S elenium JUnit 4 Class Runner可用于减少需要添加的锅炉板数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM