0

When I run the following code, below error is showing : org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.

The chrome browser is not getting launched.

//baseClass.java:

public class BaseClass {

//ThreadLocal will keep local copy of driver
public static ThreadLocal<RemoteWebDriver> dr = new ThreadLocal<RemoteWebDriver>();

@BeforeTest
//Parameter will get browser from testng.xml on which browser test to run
@Parameters("myBrowser")
public void beforeClass(String myBrowser) throws MalformedURLException{
    try {
        RemoteWebDriver driver = null;

        if(myBrowser.equals("chrome")){
            DesiredCapabilities capability = new DesiredCapabilities().chrome();
            capability.setBrowserName("chrome");
            capability.setPlatform(Platform.WINDOWS);
            driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
        }
        else if(myBrowser.equals("firefox")){
            DesiredCapabilities capability = new DesiredCapabilities().firefox();
            capability.setBrowserName("firefox");
            capability.setPlatform(Platform.WINDOWS);
            driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
        }

        //setting webdriver
        setWebDriver(driver);

        getDriver().manage().window().maximize();
        getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }catch (Exception ex){
        System.out.println(ex.toString());
    }
}

public WebDriver getDriver() {
    return dr.get();
}

public void setWebDriver(RemoteWebDriver driver) {
    dr.set(driver);
}

@AfterClass
public void afterClass(){
    getDriver().quit();
    dr.set(null);

}

}

juherr
  • 5,640
  • 1
  • 21
  • 63
Cinra
  • 33
  • 8
  • Possible duplicate of [Selenium UnreachableBrowserException - "Could not start a new session" in SoapUI Groovy TestStep](http://stackoverflow.com/questions/30397043/selenium-unreachablebrowserexception-could-not-start-a-new-session-in-soapui) – JeffC Apr 19 '17 at 13:05

1 Answers1

1

You have to set the system property for chrome/gecko driver before initializing the RemoteWebDriver. Something like,

 if(myBrowser.equals("chrome")){
        DesiredCapabilities capability = new DesiredCapabilities().chrome();
        capability.setBrowserName("chrome");
        capability.setPlatform(Platform.WINDOWS);
        System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe"); 
        driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
    }
    else if(myBrowser.equals("firefox")){
        DesiredCapabilities capability = new DesiredCapabilities().firefox();
        capability.setBrowserName("firefox");
        capability.setPlatform(Platform.WINDOWS);
        System.setProperty("webdriver.gecko.driver", "C:\\geckodriver\\geckodriver.exe"); 
        driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
    }