gpt4 book ai didi

java - Maven Selenium 集成测试

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:36:00 41 4
gpt4 key购买 nike

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

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

谁能帮帮我?

提前致谢

最佳答案

你的 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秒。你还在等什么?

关于java - Maven Selenium 集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7792143/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com