简体   繁体   中英

maven selenium integration test

I'm using selenium 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.

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

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

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). Also the way you set up your DefaultSelenium looks wrong to me. Also, for Firefox you don't need the Selenium server running.

Another thing I don't understand is the wait for 30 seconds directly after starting selenium. What are you waiting for?

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

As @Ozyman has pointed out it looks like you have not started the Selenium Server. The Selenium Server must be running in the background if you want to use Selenium RC to create your tests.

You can use the Selenium Maven Plugin to launch the Selenium Server before executing integration tests by adding the the following plugin configuration to ... .

<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. The Maven Failsafe Plugin will run the integration tests if you add the following plugin configuration to ... .

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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