Load balance, your Selenium Grid hub servers to improve the testing speed. There are multiple providers available in the market to load balance your applications. Few open source softwares are
nginx, apache httpd, etc.,
This page will help you in setting up your selenium grid hub servers in a load balanced environment by using the nginx.
1. As a primary step install the nginx on your local machine. Those who use mac can use the home brew to install the nginx.
command: brew install nginx
if the nginx is already installed and needs to be upgraded, then run "brew update nginx"
2. Run <ps -ef|grep "nginx"> to check if there are any nginx process running in the background.
If you find any nginx process running, then either just stop the service or kill the process
3. Now, download the selenium-server-standalone jar file from the seleniumhq.org. This example uses "selenium-server-standalone-2.53.1.jar".
4. From the server directory, run the hub as follows
java -jar selenium-server-standalone-2.53.1.jar -role hub --- call it ---- HUB A
17:24:16.808 INFO - Launching Selenium Grid hub
2017-05-15 17:24:17.359:INFO::main: Logging initialized @691ms
17:24:17.367 INFO - Will listen on 4444
17:24:17.402 INFO - Will listen on 4444
2017-05-15 17:24:17.405:INFO:osjs.Server:main: jetty-9.2.z-SNAPSHOT
2017-05-15 17:24:17.425:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@a74868d{/,null,AVAILABLE}
2017-05-15 17:24:17.445:INFO:osjs.ServerConnector:main: Started ServerConnector@5c072e3f{HTTP/1.1}{0.0.0.0:4444}
2017-05-15 17:24:17.446:INFO:osjs.Server:main: Started @779ms
17:24:17.446 INFO - Nodes should register to http://169.254.86.238:4444/grid/register/
17:24:17.446 INFO - Selenium Grid hub is up and running
17:40:18.394 INFO - Registered a node http://169.254.86.238:5555
5. From the same directory, create another hub as follows
java -jar selenium-server-standalone-2.53.1.jar -role hub -port 4445 ---- call it ---- HUB B
17:25:00.741 INFO - Launching Selenium Grid hub
2017-05-15 17:25:01.296:INFO::main: Logging initialized @696ms
17:25:01.304 INFO - Will listen on 4445
17:25:01.344 INFO - Will listen on 4445
2017-05-15 17:25:01.346:INFO:osjs.Server:main: jetty-9.2.z-SNAPSHOT
2017-05-15 17:25:01.371:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@a74868d{/,null,AVAILABLE}
2017-05-15 17:25:01.401:INFO:osjs.ServerConnector:main: Started ServerConnector@5c072e3f{HTTP/1.1}{0.0.0.0:4445}
2017-05-15 17:25:01.401:INFO:osjs.Server:main: Started @801ms
17:25:01.402 INFO - Nodes should register to http://169.254.86.238:4445/grid/register/
17:25:01.402 INFO - Selenium Grid hub is up and running
17:42:49.934 INFO - Registered a node http://169.254.86.238:5556
6. Now register a node to hub A as follows,
java -jar selenium-server-standalone-2.53.1.jar -role wd -hub http://localhost:4444/grid/register
17:40:17.905 INFO - Launching a Selenium Grid node
17:40:18.258 INFO - Java: Oracle Corporation 25.51-b03
17:40:18.258 INFO - OS: Mac OS X 10.11.6 x86_64
17:40:18.265 INFO - v2.53.1, with Core v2.53.1. Built from revision a36b8b1
17:40:18.316 INFO - Driver provider org.openqa.selenium.ie.InternetExplorerDriver registration is skipped:
registration capabilities Capabilities [{ensureCleanSession=true, browserName=internet explorer, version=, platform=WINDOWS}] does not match the current platform MAC
17:40:18.316 INFO - Driver provider org.openqa.selenium.edge.EdgeDriver registration is skipped:
registration capabilities Capabilities [{browserName=MicrosoftEdge, version=, platform=WINDOWS}] does not match the current platform MAC
17:40:18.316 INFO - Driver class not found: com.opera.core.systems.OperaDriver
17:40:18.316 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
17:40:18.318 INFO - Driver class not found: org.openqa.selenium.htmlunit.HtmlUnitDriver
17:40:18.318 INFO - Driver provider org.openqa.selenium.htmlunit.HtmlUnitDriver is not registered
17:40:18.356 INFO - Selenium Grid node is up and ready to register to the hub
17:40:18.376 INFO - Starting auto registration thread. Will try to register every 5000 ms.
17:40:18.376 INFO - Registering the node to the hub: http://localhost:4444/grid/register
17:40:18.395 INFO - The node is registered to the hub and ready to use
7. Now register a node to hub B as follows,
java -jar selenium-server-standalone-2.53.1.jar -role wd -hub http://localhost:4445/grid/register -port 5556
17:42:49.494 INFO - Launching a Selenium Grid node
17:42:49.817 INFO - Java: Oracle Corporation 25.51-b03
17:42:49.817 INFO - OS: Mac OS X 10.11.6 x86_64
17:42:49.820 INFO - v2.53.1, with Core v2.53.1. Built from revision a36b8b1
17:42:49.862 INFO - Driver provider org.openqa.selenium.ie.InternetExplorerDriver registration is skipped:
registration capabilities Capabilities [{ensureCleanSession=true, browserName=internet explorer, version=, platform=WINDOWS}] does not match the current platform MAC
17:42:49.863 INFO - Driver provider org.openqa.selenium.edge.EdgeDriver registration is skipped:
registration capabilities Capabilities [{browserName=MicrosoftEdge, version=, platform=WINDOWS}] does not match the current platform MAC
17:42:49.863 INFO - Driver class not found: com.opera.core.systems.OperaDriver
17:42:49.863 INFO - Driver provider com.opera.core.systems.OperaDriver is not registered
17:42:49.864 INFO - Driver class not found: org.openqa.selenium.htmlunit.HtmlUnitDriver
17:42:49.864 INFO - Driver provider org.openqa.selenium.htmlunit.HtmlUnitDriver is not registered
17:42:49.896 INFO - Selenium Grid node is up and ready to register to the hub
17:42:49.916 INFO - Starting auto registration thread. Will try to register every 5000 ms.
17:42:49.917 INFO - Registering the node to the hub: http://localhost:4445/grid/register
17:42:49.934 INFO - The node is registered to the hub and ready to use
8. Now you have setup 2 grid servers and registered a node each.
9. Let us now start configuring the nginx with a round-robin load balanced configuration.
add the following config to http node in the nginx configuration file "/usr/local/etc/nginx/nginx.conf"
upstream gridapp {
server localhost:4444;
server localhost:4445;
}
in the above, gridapp is loadbalanced name
Then update the server that listens on 80 port as follows.
server {
listen 80;
location / {
proxy_pass http://gridapp;
}
}
This means, when users access http://localhost:80, nginx proxy passes to loadbalanced servers (localhost:4444, localhost:4445) named with gridapp.
To Test:
From your browser, hit http://localhost:80 and observe different nodes displayed.