-
-
Save krmahadevan/1106183 to your computer and use it in GitHub Desktop.
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); | |
} | |
} | |
} |
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>
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.
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
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)