使用Firefox Web驱动程序启动Selenium的例外情况

时间:2014-03-19 12:01:23

标签: java firefox selenium

我的代码如下:

startServer();
Integer port = 4545;
String browserString= "*firefox";
selenium = new DefaultSelenium("localhost",port,browserString, url) {
public void open(String url) { commandProcessor.doCommand("open", new String[] {url,"true"});};
};
logger.info("Start Selenium ");
selenium.start(); // <------ SeleniumException here
logger.info("Selenium started");

我得到的堆栈跟踪是:

java.lang.RuntimeException: Could not start Selenium session: Failed to start new browser session: Error while launching browser
    at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:109)
    at com.example.test.infrastruttura.util.SeleniumHolder.initAll(SeleniumHolder.java:55)
    at com.example.test.infrastruttura.util.SeleniumHolder.<init>(SeleniumHolder.java:23)
    at com.example.test.infrastruttura.util.SeleniumHolder.createInstance(SeleniumHolder.java:43)
    at com.example.test.infrastruttura.util.SeleniumHolder.getInstance(SeleniumHolder.java:28)
    at com.example.test.infrastruttura.util.SgateSeleneseTestCase.setUp(SgateSeleneseTestCase.java:38)
    at com.example.sgate.test.selenium.GenericLogin.executeNavigation(GenericLogin.java:18)
    at com.example.test.infrastruttura.checks.CheckObject.executeSelenium(CheckObject.java:424)
    at com.example.test.infrastruttura.MainTestCase.executeNode(MainTestCase.java:312)
    at com.example.test.infrastruttura.MainTestCase.execute(MainTestCase.java:244)
    at com.example.test.infrastruttura.MainTestCase.execute(MainTestCase.java:157)
    at com.example.sgate.test.functional.verificaEsitiAnagrafici.VerificaEsitiAnagrafici019AltroUtenteStessoComune.testComune(VerificaEsitiAnagrafici019AltroUtenteStessoComune.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at junit.framework.TestCase.runTest(TestCase.java:154)
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:118)
    at junit.framework.TestSuite.runTest(TestSuite.java:208)
    at junit.framework.TestSuite.run(TestSuite.java:203)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:96)
    at org.apache.maven.surefire.junit.JUnit3Provider.executeTestSet(JUnit3Provider.java:117)
    at org.apache.maven.surefire.junit.JUnit3Provider.invoke(JUnit3Provider.java:94)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: com.thoughtworks.selenium.SeleniumException: Failed to start new browser session: Error while launching browser
    at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:109)
    at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:103)
    at com.thoughtworks.selenium.HttpCommandProcessor.getString(HttpCommandProcessor.java:272)
    at com.thoughtworks.selenium.HttpCommandProcessor.start(HttpCommandProcessor.java:234)
    at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:100)
    ... 39 more

我使用selenium-java-2.40.0和Firefox 26。

但以下代码有效:

WebDriver driver = new FirefoxDriver();
driver.get("http://myhostname:myport/myapplicationname");
WebElement userId = driver.findElement(By.name("userId"));
userId.sendKeys("coolusername");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("ultrasecretpassword");
password.submit();
System.out.println("Page title is: " + driver.getTitle());
driver.quit();

所以这不是Selenium版本与Firefox兼容的问题。

Selenium尝试在此URL找到一些servlet:

http://localhost:4545/selenium-server/driver/

但收到404错误。

你帮我吗?

2 个答案:

答案 0 :(得分:1)

确保Firefox安装在默认位置。可选,您可以提供浏览器的绝对路径:

String browserString = "c:\\program files\\mozilla firefox\\firefox.exe";

Check the source and documentation了解更多信息。

但是,如果您不想提供Selenium RC,我建议使用驱动程序(如工作示例中所示)。

答案 1 :(得分:0)

查看source code of the DefaultSelenium class,您会注意到alloweb浏览器字符串是:

  • *webdriver
  • *firefox-wd
  • *iexplore-wd

所以我写道:

String browserString= "*firefox-wd";

这解决了这个问题。