Skip to content

Instantly share code, notes, and snippets.

@krmahadevan
Created July 26, 2011 07:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save krmahadevan/1106183 to your computer and use it in GitHub Desktop.
Save krmahadevan/1106183 to your computer and use it in GitHub Desktop.
This sample shows how to spawn a Grid2 hub via code, attach a webdriver node and RC node to it and run an automation test on this hub
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.openqa.grid.common.GridRole;
import org.openqa.grid.common.RegistrationRequest;
import org.openqa.grid.common.SeleniumProtocol;
import org.openqa.grid.internal.utils.GridHubConfiguration;
import org.openqa.grid.internal.utils.SelfRegisteringRemote;
import org.openqa.grid.web.Hub;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class SpawnHubViaCode {
String hubHost = "localhost";
int hubPort = 4444;
Hub myHub = null;
SelfRegisteringRemote remoteWebDriverNode = null, remoteRCNode = null;
@BeforeClass
public void bringUpHubAndNode() throws Exception {
GridHubConfiguration gridHubConfig = new GridHubConfiguration();
gridHubConfig.setHost(hubHost);
gridHubConfig.setPort(hubPort);
myHub = new Hub(gridHubConfig);
myHub.start();
DesiredCapabilities chrome = DesiredCapabilities.chrome();
chrome.setBrowserName("*googlechrome");
remoteRCNode = attachNodeToHub(chrome, GridRole.NODE, 5555,
SeleniumProtocol.Selenium);
remoteWebDriverNode = attachNodeToHub(DesiredCapabilities.firefox(),
GridRole.NODE, 5556, SeleniumProtocol.WebDriver);
}
private SelfRegisteringRemote attachNodeToHub(
DesiredCapabilities capability, GridRole role, int nodePort,
SeleniumProtocol protocol) throws Exception {
SelfRegisteringRemote node = null;
RegistrationRequest registrationRequest = RegistrationRequest
.webdriverNoCapabilities();
capability.setCapability("seleniumProtocol", protocol);
registrationRequest.addDesiredCapability(capability);
registrationRequest.setRole(role);
registrationRequest.setConfiguration(fetchNodeConfiguration(role,
nodePort, protocol));
node = new SelfRegisteringRemote(registrationRequest);
node.startRemoteServer();
node.startRegistrationProcess();
return node;
}
private Map<String, Object> fetchNodeConfiguration(GridRole role,
int portToRun, SeleniumProtocol protocol)
throws MalformedURLException {
Map<String, Object> nodeConfiguration = new HashMap<String, Object>();
nodeConfiguration.put(RegistrationRequest.AUTO_REGISTER, true);
nodeConfiguration.put(RegistrationRequest.HUB_HOST, myHub.getHost());
nodeConfiguration.put(RegistrationRequest.HUB_PORT, myHub.getPort());
nodeConfiguration.put(RegistrationRequest.PORT, portToRun);
URL remoteURL = new URL("http://" + myHub.getHost() + ":" + portToRun);
nodeConfiguration.put(RegistrationRequest.PROXY_CLASS,
"org.openqa.grid.selenium.proxy.DefaultRemoteProxy");
nodeConfiguration.put(RegistrationRequest.MAX_SESSION, 1);
nodeConfiguration.put(RegistrationRequest.CLEAN_UP_CYCLE, 2000);
nodeConfiguration.put(RegistrationRequest.REMOTE_HOST, remoteURL);
nodeConfiguration.put(RegistrationRequest.MAX_INSTANCES, 1);
return nodeConfiguration;
}
@Test
public void testLocalGrid() throws MalformedURLException {
URL remoteURL = new URL("http://" + myHub.getHost() + ":" +
myHub.getPort() + "/wd/hub");
WebDriver myDriver = new RemoteWebDriver(remoteURL,
DesiredCapabilities.firefox());
myDriver.get("http://www.google.com");
myDriver.quit();
Selenium selenium = new DefaultSelenium(myHub.getHost(),
myHub.getPort(), "*googlechrome", "http://www.google.com");
selenium.start();
selenium.open("http://www.facebook.com");
selenium.stop();
}
@AfterClass
public void shutDownNodeAndHub() throws Exception {
if (remoteWebDriverNode != null) {
remoteWebDriverNode.stopRemoteServer();
Reporter.log("WebDriver Node shutdown", true);
}
if (remoteRCNode != null) {
remoteRCNode.stopRemoteServer();
Reporter.log("RC Node shutdown", true);
}
if (myHub != null) {
myHub.stop();
Reporter.log("Local hub shutdown", true);
}
}
}
@mastermason11
Copy link

I'm actually getting this although I have the correct dependencies that I know of.... Can you help ?

FAILED CONFIGURATION: @BeforeClass bringUpHubAndNode
java.lang.NoClassDefFoundError: javax/servlet/jsp/JspApplicationContext
at org.apache.jasper.compiler.JspRuntimeContext.(JspRuntimeContext.java:103)
at org.apache.jasper.servlet.JspServlet.init(JspServlet.java:134)
at org.openqa.jetty.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:383)
at org.openqa.jetty.jetty.servlet.ServletHolder.start(ServletHolder.java:243)
at org.openqa.jetty.jetty.servlet.ServletHandler.initializeServlets(ServletHandler.java:446)
at org.openqa.jetty.jetty.servlet.WebApplicationHandler.initializeServlets(WebApplicationHandler.java:321)
at org.openqa.jetty.jetty.servlet.WebApplicationContext.doStart(WebApplicationContext.java:509)
at org.openqa.jetty.util.Container.start(Container.java:72)
at org.openqa.jetty.http.HttpServer.doStart(HttpServer.java:708)
at org.openqa.jetty.util.Container.start(Container.java:72)
at org.openqa.grid.web.Hub.start(Hub.java:173)
at com.dexone.test.ams.template.bringUpHubAndNode(template.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:543)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:212)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
at org.testng.TestRunner.privateRun(TestRunner.java:758)
at org.testng.TestRunner.run(TestRunner.java:613)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1137)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1062)
at org.testng.TestNG.run(TestNG.java:974)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)
Caused by: java.lang.ClassNotFoundException: javax.servlet.jsp.JspApplicationContext
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 36 more

@krmahadevan
Copy link
Author

What version of Selenium are you working with ?
I dont think this issue surfaces any more, atleast not in my knowledge, because this was fixed within selenium.
Can you run a mvn dependency:tree and share the output of that ?

@mastermason11
Copy link

I'm now able to clear the dependencies above. Now, I'm using selenium-server version 2.18.0. Do you mind trying your code above ? it doesn't seem to work for me. It creates the hub and node but when you instantiate the remotewebdriver connecting to the hub, i get a nullpointerexception.

@mastermason11
Copy link

Here's the stacktrace...

Jan 29, 2012 1:24:30 AM org.openqa.jetty.http.HttpServer doStart
INFO: Version Jetty/5.1.x
Jan 29, 2012 1:24:30 AM org.openqa.jetty.util.FileResource
INFO: Checking Resource aliases
Jan 29, 2012 1:24:30 AM org.openqa.jetty.util.Container start
INFO: Started org.openqa.jetty.jetty.servlet.WebApplicationHandler@5ebbfa5c
2012-01-29 01:24:30.078::INFO: Logging to STDERR via org.mortbay.log.StdErrLog
Jan 29, 2012 1:24:30 AM org.openqa.jetty.util.Container start
INFO: Started WebApplicationContext[/,/]
Jan 29, 2012 1:24:30 AM org.openqa.jetty.http.SocketListener start
INFO: Started SocketListener on 0.0.0.0:4444
Jan 29, 2012 1:24:30 AM org.openqa.jetty.util.Container start
INFO: Started org.openqa.jetty.jetty.Server@3d860038
01:24:46.152 INFO - Java: Apple Inc. 20.4-b02-402
01:24:46.153 INFO - OS: Mac OS X 10.7.2 x86_64
01:24:46.158 INFO - v2.18.0, with Core v2.18.0. Built from revision 15704
01:24:46.224 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:5555/wd/hub
01:24:46.224 INFO - Version Jetty/5.1.x
01:24:46.225 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
01:24:46.225 INFO - Started HttpContext[/selenium-server,/selenium-server]
01:24:46.225 INFO - Started HttpContext[/,/]
01:24:46.226 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@3445378f
01:24:46.226 INFO - Started HttpContext[/wd,/wd]
01:24:46.230 INFO - Started SocketListener on 0.0.0.0:5555
01:24:46.230 INFO - Started org.openqa.jetty.jetty.Server@78ba6c13
01:24:46.231 INFO - using the json request : {"class":"org.openqa.grid.common.RegistrationRequest","capabilities":[{"platform":"ANY","browserName":"firefox","version":""}],"configuration":{"port":5555,"register":true,"cleanUpCycle":10000,"maxInstances":2,"proxy":"org.openqa.grid.selenium.proxy.WebDriverRemoteProxy","maxSession":5,"hubHost":"127.0.0.1","hubPort":4444,"remoteHost":"http://127.0.0.1:5555/wd/hub"}}
01:24:46.231 INFO - Registering the node to hub :http://127.0.0.1:4444/grid/register
01:24:46.373 WARN - Max instance not specified. Using default = 1 instance
01:24:46.739 INFO - Stopping Acceptor ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=5555]
01:24:46.842 INFO - Stopped SocketListener on 0.0.0.0:5555
01:24:46.914 INFO - Stopped HttpContext[/selenium-server/driver,/selenium-server/driver]
01:24:46.979 INFO - Stopped HttpContext[/selenium-server,/selenium-server]
01:24:47.041 INFO - Stopped HttpContext[/,/]
01:24:47.041 INFO - Exiting session cleaner thread
01:24:47.042 INFO - Stopped org.openqa.jetty.jetty.servlet.ServletHandler@3445378f
01:24:47.105 INFO - Stopped HttpContext[/wd,/wd]
01:24:47.105 INFO - Stopped org.openqa.jetty.jetty.Server@78ba6c13
WebDriver Node shutdown
01:24:47.106 INFO - Stopping Acceptor ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=4444]
01:24:47.207 INFO - Stopped SocketListener on 0.0.0.0:4444
01:24:47.210 INFO - Stopped org.openqa.jetty.jetty.servlet.WebApplicationHandler@5ebbfa5c
01:24:47.273 INFO - Stopped WebApplicationContext[/,/]
01:24:47.273 INFO - Stopped org.openqa.jetty.jetty.Server@3d860038
Local hub shutdown
FAILED: testLocalGrid
java.lang.NullPointerException
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:142)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:94)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:102)
at com.dexone.test.ams.template.testLocalGrid(template.java:125)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:691)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:883)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1208)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:758)
at org.testng.TestRunner.run(TestRunner.java:613)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1137)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1062)
at org.testng.TestNG.run(TestNG.java:974)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)

@krmahadevan
Copy link
Author

From the time I created this sample till now I guess Selenium Grid2 code has undergone some refactoring which explains why you started seeing all sorts of errors. I have revamped the code and updated it to run with Selenium 2.18.0

Incase you see any ClassNotFoundErrors, you might have to add the following to your pom file as a dependency (I am yet to find out why is this coming up though)

    <dependency>
        <groupId>org.apache.servicemix.bundles</groupId>
        <artifactId>org.apache.servicemix.bundles.jasper</artifactId>
        <version>6.0.14_4</version>
    </dependency>

@krmahadevan
Copy link
Author

Since at home I run on a Ubuntu LINUX machine, I switched from using IE to chrome so that I could test out the code, but the concept is still the same.

@velagapudi
Copy link

Hi,

Will this code work from linux Server/Windows workstation as client? I was trying to implement selenium webdriver on linux server and trying to run from windows machine. I was not able to succeed. Do we have make some other changes other then you shown in this example. any help would be appreciated.

Thanks,
-Ravi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment