Vitaly Olegovitch
Vitaly Olegovitch

Reputation: 3547

Exception on starting Selenium with Firefox web driver

My code is the following:

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

The stacktrace that I get is:

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

I use selenium-java-2.40.0 and Firefox 26.

But the following code works:

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

So it is not a problem of Selenium version compatibility with Firefox.

Selenium tries to find some servlet at this URL:

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

But receives a 404 error.

Ca you help me?

Upvotes: 0

Views: 1018

Answers (2)

Vitaly Olegovitch
Vitaly Olegovitch

Reputation: 3547

Looking at the source code of the DefaultSelenium class, you can notice that the alloweb browser strings are:

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

So I wrote:

String browserString= "*firefox-wd";

And that fixed the problem.

Upvotes: 0

Spindizzy
Spindizzy

Reputation: 8924

Ensure that the Firefox is installed in the default location. Optional you can give the absolute path to the browser:

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

Check the source and documentation for more information.

However, I recommend to use the Driver (like in the working sample), if you don't want to provide a Selenium RC.

Upvotes: 1

Related Questions