Tuesday, June 8, 2010

Selenium Webdriver running remotely

Recently i started developing my automation code using Selenium2 code .Most of my code is for firefox driver .I need to confess that i liked Selenium2 really there are some stability issues in comparison to Selenium1 but it is good .

Default usage of Selenium2 is that it will run server and client from same instance and that will control the browser elements,but Selenium2 provide a way to run server and client on remote location as you use to do in Selenium1.Remote Webdriver is the class which help you to run client and server separately .RemoteWebdriver provides a DriverServlet which if you add inside any servlet container you got your selenium server up.
There are few examples for this in Selenium2 wiki site .As i have never ran servlet application before so i got in to little bit of issues following that .But finally i got it running and i used jetty as servlet container .Two ways you can include that in jetty

1.Run jetty server on machine and put a war file on location pointed by jetty config file .
2.Start jetty server from your application and add you servlet class to it .(I followed this method)

I downloaded the selenium2 code and built remotewebdriver werver and client.
command to built server
./go clean selenium-java selenium-server-standalone which will result in build/remote/server/server-standalone.jar
command to built client
./go //remote/client:client:uber which will result in build/remote/client/client-standalone.jar
*go is script comes with selenium2 code and code below i got it from wiki just made few changes .

Server code
AppServer.java
=============================================
import org.mortbay.jetty.Server;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;

import java.io.File;

import org.openqa.selenium.remote.server.DriverServlet;

public class AppServer {

public static void main(String s[]) throws Exception{
Server server = new Server();


WebAppContext context = new WebAppContext();
context.setContextPath("");
File st=new File(".");

context.setWar(st.getPath());
server.addHandler(context);

context.addServlet(DriverServlet.class, "/wd/*");

SelectChannelConnector connector = new SelectChannelConnector();
connector.setPort(3002);
server.addConnector(connector);

server.start();
}
}
================================
Add server-standalone.jar ,jetty-6.1.9.jar and jetty-util-5.1.9.jar(you can download from jetty site or selenium2 code also has in third_party folder) to classpath and run above code.
Your server will up with following message ---
2010-06-08 11:07:00.730::INFO: Started SelectChannelConnector@0.0.0.0:3002

Client example code
FirefoxClient.java
================================
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.WebDriver;

public class FirefoxClient {

public static void main(String s[]) throws Exception {

URL url=new URL("http://127.0.0.1:3002/wd");
DesiredCapabilities capabilities = new DesiredCapabilities();

// ... but only if it supports javascript
capabilities.setJavascriptEnabled(true);
capabilities.setBrowserName("firefox");

// Get a handle to the driver. This will throw an exception
// if a matching driver cannot be located
WebDriver driver = new RemoteWebDriver(url,capabilities);

// Query the driver to find out more information
//Capabilities actualCapabilities = ((RemoteWebDriver) driver).getCapabilities();

// And now use it
driver.get("http://www.google.com");
}

}
=====================================
Add client-standalone.jar to class path and run the above code .

3 comments:

  1. Hi Nitin,
    Do you happen to start the remote server on the remote host by any chance?
    I can execute the code locally but have trouble starting the server remotely.

    I am doing connector.setHost("remoteserverIPAddress");

    Thanks,
    -Nilesh

    ReplyDelete
  2. Hi,

    I am trying to make a web application which has a button which when clicked starts the servlet code. In the servlet code i have this line where my program crashes
    Firefoxdriver dr=new FirfoxDriver();

    I am trying to run the code on tomcat server. Please instruct me how to proceed.

    ReplyDelete
  3. Hi ganes you were got that to work/

    ReplyDelete