Created
July 26, 2011 07:19
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)