How to run selenium hub & node programmatically

197 views
Skip to first unread message

Murali Krishna

unread,
Mar 8, 2016, 5:44:19 AM3/8/16
to Selenium Users
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.Platform;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Testing {
public static void main(String[] args) throws Exception {
GridHubConfiguration config = new GridHubConfiguration();
config.setHost("10.220.0.37");
config.setPort(4445);
Hub hub = new Hub(config);
hub.start();
System.err.println(hub.getRegistrationURL().toString());
RegistrationRequest req = new RegistrationRequest();
req.setRole(GridRole.NODE);
DesiredCapabilities firefox = DesiredCapabilities.firefox();
firefox.setBrowserName("*firefox");
firefox.setPlatform(Platform.WINDOWS);
firefox.setVersion("31.0");
FirefoxProfile profile = new ProfilesIni().getProfile("Selenium");
firefox.setCapability(FirefoxDriver.PROFILE, profile);
firefox.setCapability("seleniumProtocol", SeleniumProtocol.WebDriver);

Map<String, Object> nodeConfiguration = new HashMap<String, Object>();

nodeConfiguration.put(RegistrationRequest.AUTO_REGISTER, true);
nodeConfiguration.put(RegistrationRequest.HUB_HOST, hub.getHost());
nodeConfiguration.put(RegistrationRequest.HUB_PORT, hub.getPort());
nodeConfiguration.put(RegistrationRequest.PORT, 5544);
URL remoteURL = new URL("http://" + hub.getHost() + ":" + 5544);
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);

req.setConfiguration(nodeConfiguration);

SelfRegisteringRemote remote = new SelfRegisteringRemote(req);
remote.startRemoteServer();
remote.startRegistrationProcess();
try{
RemoteWebDriver rwd = new RemoteWebDriver(new URL("http://10.220.0.37:4445/wd/hub"), firefox);
System.err.println("Came here-----------------");
System.err.println("Camefffffffff here-----------------");
}catch(Exception e){
e.printStackTrace();
e.getLocalizedMessage();
remote.stopRemoteServer();
hub.stop();
}

}


Getting below exception:

org.openqa.selenium.WebDriverException: Error forwarding the new session cannot find : Capabilities [{seleniumProtocol=WebDriver, browserName=*firefox, version=31.0, platform=WINDOWS, firefox_profile=null}]
Command duration or timeout: 141 milliseconds
Build info: version: '2.48.2', revision: '41bccdd10cf2c0560f637404c2d96164b67d9d67', time: '2015-10-09 13:08:06'
System info: host: 'PC170529', ip: '10.220.0.37', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_40'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:247)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:129)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:156)
at com.ticketmaster.payments.wag.selenium.Testing.main(Testing.java:71)
Caused by: org.openqa.grid.common.exception.GridException: Error forwarding the new session cannot find : Capabilities [{seleniumProtocol=WebDriver, browserName=*firefox, version=31.0, platform=WINDOWS, firefox_profile=null}]
at org.openqa.grid.web.servlet.handler.RequestHandler.process(RequestHandler.java:115)
at org.openqa.grid.web.servlet.DriverServlet.process(DriverServlet.java:83)
at org.openqa.grid.web.servlet.DriverServlet.doPost(DriverServlet.java:67)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.seleniumhq.jetty9.servlet.ServletHolder.handle(ServletHolder.java:808)
at org.seleniumhq.jetty9.servlet.ServletHandler.doHandle(ServletHandler.java:587)
at org.seleniumhq.jetty9.server.session.SessionHandler.doHandle(SessionHandler.java:221)
at org.seleniumhq.jetty9.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
at org.seleniumhq.jetty9.servlet.ServletHandler.doScope(ServletHandler.java:515)
at org.seleniumhq.jetty9.server.session.SessionHandler.doScope(SessionHandler.java:185)
at org.seleniumhq.jetty9.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
at org.seleniumhq.jetty9.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.seleniumhq.jetty9.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.seleniumhq.jetty9.server.Server.handle(Server.java:499)
at org.seleniumhq.jetty9.server.HttpChannel.handle(HttpChannel.java:310)
at org.seleniumhq.jetty9.server.HttpConnection.onFillable(HttpConnection.java:257)
at org.seleniumhq.jetty9.io.AbstractConnection$2.run(AbstractConnection.java:540)
at org.seleniumhq.jetty9.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
at org.seleniumhq.jetty9.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
at java.lang.Thread.run(Thread.java:745)


Krishnan Mahadevan

unread,
Mar 13, 2016, 11:24:42 PM3/13/16
to Selenium Users
You missed setting the capabilities for the node.

Add this line and you should be fine after that.

req.setCapabilities(Arrays.asList(firefox));

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/60a4fa60-5afd-4b3f-8523-390238c81a0a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vikram

unread,
Dec 19, 2016, 3:52:48 PM12/19/16
to Selenium Users
Hey Krishnan,

I tried your's and Murali's solution for Chrome driver but getting error

org.openqa.grid.common.exception.GridConfigurationException: no server set to register to the hub

Can you please share working solution for multiple nodes ?

Thanks & Regards,
Vikram

⇜Krishnan Mahadevan⇝

unread,
Dec 20, 2016, 10:12:57 PM12/20/16
to Selenium Users
Vikram,

The easiest way of doing this would be to invoke org.openqa.grid.selenium.GridLauncherV3#main by passing the required args (the ones that you would usually do when you are starting the hub/node from command prompt) through your main() method.

That way you don't have to write a lot of boiler plate code and still can get your hub or node to get spun off from within your code.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/

To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/d4f6a9b6-d072-48e1-bc10-02b1a6e12e71%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages