Re: [selenium-users] How to start hub programmatically in Java

2,215 views
Skip to first unread message
Message has been deleted

François Reynaud

unread,
Aug 29, 2012, 8:24:01 PM8/29/12
to seleniu...@googlegroups.com
grid hub + 1 node started from a java class :
https://github.com/freynaud/testng-support/blob/master/src/main/java/org/openqa/selenium/support/testng/LocalGrid.java

thanks,
françois


On Wed, Aug 29, 2012 at 7:42 PM, sel.cook <selenium...@gmail.com> wrote:
Hi,

I am new to Selenium grid and a technical novice..

I know we can start the grid and nodes through command line (as stated in http://code.google.com/p/selenium/wiki/Grid2). I wonder if there is a way we can start it in a Java class?

Thanks in advance!

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/lX-0SM6X8ygJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Krishnan Mahadevan

unread,
Oct 31, 2012, 7:14:26 AM10/31/12
to seleniu...@googlegroups.com
Jordan,

The best thing about Grid2 is that firefox profile is NO longer an attribute of the Grid by itself, but is now something that becomes part of your test code.

So for you to make use of a firefox profile and then use it against the Grid2, the following would suffice :

DesiredCapabilities dc = new DesiredCapabilities();
dc.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
FirefoxProfile profile = new ProfilesIni().getProfile("myFirefoxProfile");
dc.setCapability(FirefoxDriver.PROFILE, profile);
RemoteWebDriver rwd = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dc);



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



On Wed, Oct 31, 2012 at 5:02 AM, Jordan Leach <jkle...@gmail.com> wrote:
This example helped me greatly, but is it also possible to specify the Firefox Profile Template somewhere?  Before using Grid I was creating a RemoteControlConfiguration object and setting nothing more than the port, log file, and Firefox profile then passing it into a SeleniumServer constructor.  Can I do something similar to a particular node on the grid? 

Thanks,
Jordan
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/x-Y2pOZIvcwJ.

Krishnan Mahadevan

unread,
Nov 1, 2012, 7:23:41 AM11/1/12
to seleniu...@googlegroups.com
Jordan,

Replace : 

Selenium mySelenium = new DefaultSelenium("localhost", 4444, "*firefox", "https://");

With : 
DesiredCapabilities dc = new DesiredCapabilities();
dc.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
FirefoxProfile profile = new ProfilesIni().getProfile("myFirefoxProfile");
dc.setCapability(FirefoxDriver.PROFILE, profile);
RemoteWebDriver rwd = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dc);
WebDriverBackedSelenium mySelenium = new WebDriverBackedSelenium(webDriver, "_blank");


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



On Thu, Nov 1, 2012 at 2:15 AM, Jordan Leach <jkle...@gmail.com> wrote:
Krishnan,
I appreciate the speedy response.  I tried doing what you said but it's not working in my setup.  I'm not using WebDriver, is that the problem?  I'm very new to Selenium and even newer to Grid so forgive my lack of knowledge.  Here's my setup:
 
            GridHubConfiguration config = new GridHubConfiguration();
            config
.setHost("localhost");
            config
.setPort(4444);
           
Hub hub = new Hub(config);
            hub
.start();
           
           
RegistrationRequest req = new RegistrationRequest();
            req
.setRole(GridRole.NODE);
           
           
DesiredCapabilities firefox = DesiredCapabilities.firefox();
            firefox
.setBrowserName("*firefox");
           
FirefoxProfile profile = new ProfilesIni().getProfile("Selenium");
            firefox
.setCapability(FirefoxDriver.PROFILE, profile);
            firefox
.setCapability("seleniumProtocol", SeleniumProtocol.Selenium);
            req
.addDesiredCapability(firefox);
           
           
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, 5555);
            URL remoteURL
= new URL("http://" + hub.getHost() + ":" + 5555);
            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();




Then I create a new Selenium object doing the following:

            Selenium mySelenium = new DefaultSelenium("localhost", 4444, "*firefox", "https://");



It gets assigned to the node just fine and the test runs but it doesn't use the proper Firefox profile... thoughts?

Thanks for your help,
Jordan
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/jehg-c14DH4J.

Atul Nikam

unread,
Jun 17, 2015, 10:52:29 AM6/17/15
to seleniu...@googlegroups.com
One query:

After adding node to hub by below code , how to run scripts on that node.

Please provide the code.

Krishnan Mahadevan

unread,
Jun 17, 2015, 12:32:37 PM6/17/15
to Selenium Users
You just resort using WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/register"),DesiredCapabilities.firefox());

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
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.

Vikram

unread,
Jul 24, 2017, 11:23:47 AM7/24/17
to Selenium Users
Hi Francois,

As per your answer, I did small modifications and started hub as below

import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;

String strIP = "localhost";


GridHubConfiguration config = new GridHubConfiguration();
   config.host = strIP;
config.port = 4444;


hub = new Hub(config);
hub.start();

But trying to open grid console in browser ( chrome, ff ) take either really long time or doesn't open at all.

Any reason what may be going wrong here ?

Thanks & Regards,
Vikram

⇜Krishnan Mahadevan⇝

unread,
Jul 24, 2017, 11:54:00 AM7/24/17
to Selenium Users

What do you mean by it takes a long time ? Can you please explain ?

Also what happens when you pass in the IP address of your machine to your code and also later use the same to invoke the console ?

What OS are you experiencing this behavior ?

If it's a Mac then you might want to see if this helps https://stackoverflow.com/a/39698914/679824


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.
--

Vikram

unread,
Jul 24, 2017, 1:19:21 PM7/24/17
to Selenium Users
Hi Krishnan,

Please find below answers

- Mac OS 10.12.5
- Even after 5 minutes; grid console url keep on loading without any output
- As per your suggestion, I tried replacing localhost with ip but still see infinite Connecting.... loader for http://xx.yy.zz.qq:4444/grid/console
- let me check per stackoverflow query

Thanks & Regards,
Vikram

⇜Krishnan Mahadevan⇝

unread,
Jul 24, 2017, 1:24:26 PM7/24/17
to Selenium Users
Vikram,
I had suggested that you use the IP address of your machine in both your code which starts the hub and also in the browser wherein you are bringing up the console. Looks like you did it in only one place viz., in your browser.

I have seen this problem with OSX and the easiest way of fixing this would be to add your MBP's hostname to your /etc/hosts file which is what the SO post suggests as well.


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/fc69b58d-cf17-49ee-9f59-5f6f47a8edfe%40googlegroups.com.

Vikram

unread,
Jul 24, 2017, 1:25:29 PM7/24/17
to Selenium Users
regarding stackoverflow query, I doubt if it's localhost mapping issue.

As I could open grid console within couple of seconds when I start hub from command prompt; it's not working when I start from programe only.

Vikram

unread,
Jul 24, 2017, 1:36:46 PM7/24/17
to Selenium Users
Hey Krishnan,

I did explicitly mention ip address both in code as well in grid console url.

Let me add the hostname in /etc/hosts and restart Mac.

Thanks again.

Regards,
Vikram

⇜Krishnan Mahadevan⇝

unread,
Jul 24, 2017, 1:38:12 PM7/24/17
to Selenium Users
Your code is "explicitly" setting the host as "localhost" but when you launch the jar it gets the host name as the one which is neither a loopback address nor is of the IPV6 format. 

So its definitely a mapping issue AFAIK. 
To confirm this, try skipping the part wherein you are setting the host to "localhost".

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/69c1b019-03bd-4b68-8642-861548b261a3%40googlegroups.com.

Vikram

unread,
Jul 24, 2017, 1:51:10 PM7/24/17
to Selenium Users
Just to reconfirm, I changed my code as below

   String strIP = "192.168.178.21"; //"localhost";

GridHubConfiguration config = new GridHubConfiguration();
config.host = strIP;
config.port = 4444;


hub = new Hub(config);
hub.start();

Also added entry into hosts files per stackoverflow link.

Later I tried starting hub by removing  config.host & config.port altogether but no success yet.

do you also start hub similar way ?

Thanks,
Vikram

Vikram

unread,
Jul 24, 2017, 3:21:33 PM7/24/17
to Selenium Users
Hey Krishnan,

After spending last 2 hrs on this problem, I think it's related to Mac Sierra itself.

But even after multiple restarts and trying put solutions per stackoverflow didn't help.

Did you do anything else to get it working ?

I observed a strange issue which is I'm seeing 2 different values for hostname vs Sharing > Computer Name, I tried with both but no success yet.

Thanks,
Vikram

Krishnan Mahadevan

unread,
Jul 24, 2017, 4:49:58 PM7/24/17
to seleniu...@googlegroups.com

I am on Sierra as well. And for me it works fine i.e.,

 

  • Scenario 1 wherein I am doing a config.host = “localhost” and load up http://localhost:4444/grid/console
  • Scenario 2 wherein I am not setting any values for config.host and load up the Grid console by opening up the url as returned by “hub.getUrl()” [ Here hub is of type org.openqa.grid.web.Hub)

 

In terms of putting in a hostname into /etc/hosts file, I merely added the hostname as obtained when running the command “hostname” from a terminal. I didn’t have to do anything extra.

 

For me the output of hostname command matches with the hostname as seen from Sharing > Computer name [ I just have to add “.local” since I am accessing the box from the same local network when I appended the value into /etc/hosts file. This is also mentioned in the instructions on that same screen ]

 

 

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/

 

Vikram

unread,
Jul 24, 2017, 8:07:08 PM7/24/17
to Selenium Users
Hi Krishnan,

Thanks again for detailed input.

Apple is becoming more buggy while releasing new updates, I tried few other options found through google but without success.
Will continue to research to get this resolved; as it has become a blocker issue now.

Regards,
Vikram

⇜Krishnan Mahadevan⇝

unread,
Jul 25, 2017, 1:08:27 AM7/25/17
to seleniu...@googlegroups.com

Vikram

Are you on any VPN when doing this exercise?
I tried all of this on my personal Mac.

The one from work doesn't run on Sierra yet ( we are yet to receive the updates) and is still on El Capitan which doesn't have this problem. So I can't check what is the behavior on a machine which is on a VPN setup.

I also have seen that if you are in the office network which has a proxy server configured then you would need to add exceptions in the proxy server config screen where you tell your system not to route traffic to "localhost" via the Proxy server and instead to bypass it. Else your request would go to the Proxy server and the proxy server would try to figure out who "localhost" (or) your machine IP is and that perhaps is why you see the browser stalling (remember how all traffic from a browser goes via a proxy server ?)

The -host parameter (which is what you were setting via your code for the hub ) was definitely provided for machines which run exotic network setups.

So I think adding your machine name as an alias to "localhost" in "/etc/hosts" file and then setting the host to "localhost" in the Grid startup code you had should definitely solve the problem.



For more options, visit https://groups.google.com/d/optout.

Vikram

unread,
Jul 25, 2017, 5:42:32 AM7/25/17
to Selenium Users
Hi Krishnan,

good morning, thanks again for follow up on this mystery issue. please find below clarifications

- I'm not using VPN
- neither my home or office network uses proxy server
- I've below entries in /etc/hosts

127.0.0.1 localhost vikram-pro.local
255.255.255.255 broadcasthost
::1     127.0.0.1        vikram-pro.local

-
did you enable any setting under System Preferences > Sharing  ?
-
same Mac was working properly from both home & office network before update
- why does it work when I start grid server from command line but not when I start through programme ? I'm still not clear about this mystery as well

Thanks & Regards,
Vikram

Vikram

unread,
Jul 25, 2017, 6:24:31 AM7/25/17
to Selenium Users
Hi Krishnan,

one more interesting observation

- during programme execution ( in debug mode ) grid console url keeps on loading
- when I stop debug mode, it loads immediately though I could see the screen as attached
- also during debug, I did hub.geturl , please check 2nd attachment
- do you set any other parameter explicitly in config

Thanks,
Vikram
Screen Shot 2017-07-25 at 8.21.22 AM.png
Screen Shot 2017-07-25 at 8.23.12 AM.png

Vikram

unread,
Jul 25, 2017, 7:28:27 AM7/25/17
to Selenium Users
Hi Krishnan,

I did one more step updated to latest java jdk version ( but still facing same issue )

java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)

some users complained about older version having issue with Sierra.

do you also have this version of jdk / jre running on your mac ?

Thanks,
Vikrma

Vikram

unread,
Jul 25, 2017, 9:28:05 AM7/25/17
to Selenium Users
Hi Krishnan,

I'm using IDEA which was pointing to older version of jdk, fixed it to point to latest version. But no success yet.

Do you use IDEA or Eclipse ?

Thanks,
Vikram

⇜Krishnan Mahadevan⇝

unread,
Jul 25, 2017, 9:40:15 AM7/25/17
to Selenium Users
I make use of Community edition of IntelliJ at work and use a Open Source Licensed version of IntelliJ at home.
I doubt if this has got anything to do with the JDK version.
I am not allowed to enable sharing at work, and at home also I dont have any sharing enabled.

>>>>why does it work when I start grid server from command line but not when I start through programme ? I'm still not clear about this mystery as well

Its actually no different in either ways. When you spin off the grid using 

java -jar standalone.jar -role hub
You are essentially causing Selenium to figure out the IP address. You can trace through the calls in debug mode for this.

And when you dont pass in a host value to your hub which you spin off using your code, you would essentially be causing the same effect.

I dont set any extra parameters. Just for the sake of troubleshooting, can you quickly try what happens when you the method that starts off a hub just has this :

public void startHub() throws Exception {
    String[] args = new String[] {"-role", "hub"};
    GridLauncherV3.main(args);
}

This code snippet is literally equivalent to what you are seeing when you do a java -jar for spinning of the hub.


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/e0b64eaf-41ea-4027-af16-173dbc9ff8c7%40googlegroups.com.

Vikram

unread,
Jul 25, 2017, 10:14:08 AM7/25/17
to Selenium Users

OK so some more head spinner with good news.

- It WORKED PERFECTLY WITH ECLIPSE ( Neon release 4.6.0 ) , but with IDEA community version it's NOT working at all.

- I've IDEA 2017.2 and it's doing great mischief which I need to find out now ( argh these chain of problems are really painful ).

- I've recently shifted to IDEA from Eclipse and thought it to be more friendlier, less buggier. Any thoughts what's going wrong with IDEA ?

- Regarding method you provided, here is results

import org.openqa.grid.selenium.GridLauncherV3;
GridLauncherV3
IDEA output ( but grid console still NOT opening )

12:06:51.198 INFO - Selenium build info: version: '3.4.0', revision: 'unknown'
12:06:51.199 INFO - Launching Selenium Grid hub
2017-07-25 12:06:55.184:INFO::main: Logging initialized @13178ms to org.seleniumhq.jetty9.util.log.StdErrLog
12:06:55.270 INFO - Will listen on 4444
2017-07-25 12:06:55.603:INFO:osjs.Server:main: jetty-9.4.z-SNAPSHOT
2017-07-25 12:06:55.732:INFO:osjs.session:main: DefaultSessionIdManager workerName=node0
2017-07-25 12:06:55.732:INFO:osjs.session:main: No SessionScavenger set, using defaults
2017-07-25 12:06:55.739:INFO:osjs.session:main: Scavenging every 600000ms
2017-07-25 12:06:55.778:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@4b213651{/,null,AVAILABLE}
2017-07-25 12:06:55.835:INFO:osjs.AbstractConnector:main: Started ServerConnector@106cc338{HTTP/1.1,[http/1.1]}{0.0.0.0:4444}
2017-07-25 12:06:55.837:INFO:osjs.Server:main: Started @13831ms
12:06:55.837 INFO - Nodes should register to http://yyy.xxx.zzz.cc:4444/grid/register/
12:06:55.838 INFO - Selenium Grid hub is up and running

Eclipse ( works perfectly, could open grid console quickly )

same output as above

Thanks & Regards,
Vikram

shashank shekhar

unread,
Aug 7, 2018, 3:30:32 AM8/7/18
to Selenium Users
Hi Vikram,

I am using the same code of yours in my windows machine to setup Grid but i am not able to launch the console on browser and getting site cant be reached.Not sure if this is due to any security reason as i am in company network.
Also ,i get error as java.lang.NoClassDefFoundError: org/openqa/selenium/remote/JsonException
I am using testng with selenium server 3.12 and selenium java 3.4 .

thanks and regards
Shashank

shashank shekhar

unread,
Aug 7, 2018, 4:12:32 AM8/7/18
to Selenium Users
Hi Vikram,

In the code provided by you in git i find below snippet.
AppiumDriverLocalService driverLocalService1 = AppiumDriverLocalService.buildService(new AppiumServiceBuilder().withAppiumJS(new File("/usr/local/lib/node_modules/appium/build/lib/main.js")) Can you please advice me what is the path /usr/local/lib/node_modules/appium/build/lib/main.js. Just for your info i am installing appium via npm using npm install -g appium so not sure where is mu main.js. Kindly advice and help. Regards Shashank

Reply all
Reply to author
Forward
0 new messages