ISTQB Certified Software Test Engineer

Tuesday, November 1, 2011

Selenium WebDriver,Running Scripts in a Remote Machine

Selenium is famous for its Cross Browser Testing.With the new changes in Selenium2(Re-implementation of Web driver),running test scripts becomes easier compared to Selenium RC.Selenium web driver does not have any server,but its native API initiates the browser directly.
But this is not suitable for the scenarios where there is a need of running selenium scripts remotely,because we are not running any server.In RC since we are running RC server,we can do that easily.
People of Thought Works came up with a solution for running selenium test remotely.
Here is the procedure for running a selenium test Remotely.

1.Download selenium-server-standalone-2.*.jar Click here to download the jar(If this jar is deprecated please try for a new jar.Just type selenium standalone jar for web driver in Google for new jar).Download the jar file in remote machine.

2.Run the following command java -jar < downloaded jar filepath>in remote machine.

3.Then run the following java code from the machine from which you are going to initiate the remote machine.

import org.openqa.selenium.OutputType;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
public class SeleniumTest {
public static void main(String s[]) throws Exception {
URL url = new URL( "http", "localhost", 4444, "/wd/hub" );
DesiredCapabilities capabilities =DesiredCapabilities.internetExplorer();
WebDriver driver = new RemoteWebDriver(url,capabilities);
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\screenshot\\googlesearch-webdriverapi.png"));

Change the following statement in your code.

URL url = new URL( "http", "localhost", 4444, "/wd/hub" );

instead of localhost give the remote machine ip address.


  1. This works like a charm. Good one

  2. Thank you so much for the code.However,I have one problem.the line File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    is throwing a ClassCastException..Can u tell me y?

  3. oops!! didnt c ur latest post!!..thx works!!

  4. I am using Netbeans 7.2. I created a web project and in default package, I created a junit test and pasted your code. It executes till System.out.println("2");
    then it displayed a Failed status with message:
    Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
    I have ping on the ip address, it is getting a reply. Please guide.

  5. I am getting the below error I am trying to run scripts on windows machine from linux machine

    org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser

    I am using selenium 2.31 and firefox 19 can you please help?

  6. 1. how to check that this code will work on remote machine. I have one office laptop only..
    Installing a VM ware will give me another IP??

    2. How can I run test cases on IE 7, when I am using WIN7 and have IE 10 installed on it??

  7. Thanks Raghavendra! Its working with a rocket speed.

  8. Hi,
    I tried the above procedure but it is opening the firefox browser even if i specify IE. Any idea why it is happening?

    Here is my code:
    URL url = new URL( "http", "", 4444, "/wd/hub" );

    DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();

    WebDriver driver = new RemoteWebDriver(url,capabilities);