Selenium Grid 2 : Local hub and 2 local nodes - selenium

Currently, I am setting up a Selenium Grid for running test suites on a hub and distribute these tests over the nodes.
The idea is to get the test executed on the hub and distributed over 10 nodes to execute them further. For the starters and evaluate the grid, I have set up a local hub and 2 local nodes.
In my test class, I have 4 tests, on running the test using RemoteDriver and passing the hub as URL and configured capabilities, it executes all four tests on Node1 and does not distribute it over to Node2. Also, it runs all 4 tests serially on Node1. Does any one know what could be wrong here. Please find the setup below.
Hub Configuration :
C:\Proto\Selserversidedjars>java
-jar selenium-server-standalone-2.44.0.jar
-role hub -hubConfig DefaultHub.json
Node 1 Configuration :
C:\Proto\Selserversidedjars>java
-jar selenium-server-standalone-2.44.0.jar
-role node
-hub http://localhost:4444/grid/register
Node 2 Configuration :
C:\Proto\Selserversidedjars>java
-jar selenium-server-standalone-2.44.0.jar
-role node
-nodeConfig DefaultNode1.json
-port 6666
Defaulthub.json :
{
"host": null,
"port": 4444,
"newSessionWaitTimeout": -1,
"servlets" : [],
"prioritizer": null,
"capabilityMatcher": "org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
"throwOnCapabilityNotPresent": true,
"nodePolling": 5000,
"cleanUpCycle": 5000,
"timeout": 300000,
"browserTimeout": 0,
"maxSession": 5,
"jettyMaxThreads":-1
}
DefaultNode1.json:
{
"capabilities":
[
{
"browserName": "*firefox",
"maxInstances": 5,
"seleniumProtocol": "Selenium"
},
{
"browserName": "*googlechrome",
"maxInstances": 5,
"seleniumProtocol": "Selenium"
},
{
"browserName": "*iexplore",
"maxInstances": 1,
"seleniumProtocol": "Selenium"
},
{
"browserName": "firefox",
"maxInstances": 5,
"seleniumProtocol": "WebDriver"
},
{
"browserName": "chrome",
"maxInstances": 5,
"seleniumProtocol": "WebDriver"
},
{
"browserName": "internet explorer",
"maxInstances": 1,
"seleniumProtocol": "WebDriver"
}
],
"configuration":
{
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"maxSession": 5,
"port": 6666,
"host": ip,
"register": true,
"registerCycle": 5000,
"hubPort": 4444,
"hubHost": ip
}
}
Now after these configuration setup, my hub is started on port 4444, node 1 is started at 5555, node 2 is started at 6666. In my TestNG tests, I am trying to execute 4 tests. Given that I have multiple tests to run in a single class, is it possible to distribute those tests over multiple nodes or multiple instances on a single node?
Any help on "how to execute multiple instances on Node1 and distribute tests(let's say more than 10 tests) over to the Node2 in this situation" will be highly appreciated.
Thanks in advance!

Running serially or parallely depends on how you are triggering your tests and just doesn't happen automatically just by using the grid. Grid just helps in distributing tests over various nodes - if tests are sent serially it would distribute serially, if sent parallely, it would distribute on various nodes.
You might consider using TestNG to run your tests parallely or you can venture to implement your own parallelism.
You mention your single class has multiple tests - with testng, you can set the parallel attribute to methods which would trigger each test in individual threads which would be sent to the grid and the hub would take care of picking up a free node and executing your test.
Take care to write threadsafe driver launch code so that each thread has it's own driver instance.

Related

How to Run Parallel Appium Test With Selenium Grid Only By Specifying Platform

I am running parallel robot framework Appium tests using pabot. Below are my config files:
configDevice1.json
{
"capabilities":[
{
"platformName": "Android",
"platformVersion": "9.0",
"browserName": "Android-Huawei-Matepad",
"deviceName": "Huawei Matepad",
"udid": "902xxxxxxx",
"appActivity":"com.app.MainActivity",
"appPackage":"com.app.preview",
"noReset":false,
"automationName":"uiautomator2",
"groups":"device",
"maxInstances": 1
}
],
"configuration":
{
"url":"http://127.0.0.1:4724/wd/hub",
"cleanUpCycle":2500,
"timeout":30000,
"maxSession": 1,
"register": true,
"registerCycle": 1000,
"hubPort": 4444,
"hubHost": "localhost"
}
}
configDevice2.json
{
"capabilities":[
{
"platformName": "Android",
"platformVersion": "9.0",
"browserName": "Android-Samsung",
"deviceName": "Android Samsung",
"udid": "111xxxxxxx",
"appActivity":"com.app.MainActivity",
"appPackage":"com.app.preview",
"noReset":false,
"automationName":"uiautomator2",
"groups":"device",
"maxInstances": 1
}
],
"configuration":
{
"url":"http://127.0.0.1:4724/wd/hub",
"cleanUpCycle":2500,
"timeout":30000,
"maxSession": 1,
"register": true,
"registerCycle": 1000,
"hubPort": 4444,
"hubHost": "localhost"
}
}
I started the two Appium nodes in separate process:
appium --nodeconfig configDevice1.json -p 4723 -bp 5723
appium --nodeconfig configDevice2.json -p 4724 -bp 5724
The parallel test works fine if I define the udid on the desired capabilities. However, I am looking for a way to run parallel test without having to specify any udid. For instance, I want to run parallel tests on all devices with platform of Android.
When I run the parallel tests only with capabilities of platformName=Android, all tests run only on one device, conflicting each session from another.
I can see Appium is retrieving connected devices, but then only proceed selecting the first device from that list. This happens in both Appium sessions (Hence making tests failure because it tries to override an existing session)
[debug] [ADB] Connected devices: [{"udid":"111xxxxxxx","state":"device"},{"udid":"902xxxxxxx","state":"device"}]
[AndroidDriver] Using device: 111xxxxxxx
Is there anything one can do to make tests parallel on platform information available on Selenium grid nodes?
Thanks!
Silly me. The config.json value set should be platform and version. It works well now!

How do you setup an HtmlUnit node with Selenium Grid?

I'm having trouble setting up an HtmlUnit node with Selenium Grid.
I'm launching my Hub with:
java -jar selenium-server-standalone.jar -role hub -port 4444
And HtmlUnitDriver nodes with:
java -jar selenium-server-standalone.jar -role node -browser browserName=htmlunit,maxInstances=5 -hub http://localhost:4444/grid/register -port 5564
My code to connect to the node, which is getting the exception, looks something like:
DesiredCapabilities cap = new DesiredCapabilities();
cap.setBrowserName("htmlunit");
cap.setJavascriptEnabled(false);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), cap);
The exception I'm getting is:
Unable to create session from {
"desiredCapabilities": {
"browserName": "htmlunit",
"server:CONFIG_UUID": "93a0486d-c9f6-46da-8065-603ab07c0294",
"javascriptEnabled": false
},
"capabilities": {
"firstMatch": [
{
"browserName": "htmlunit",
"server:CONFIG_UUID": "93a0486d-c9f6-46da-8065-603ab07c0294"
}
]
}
}
To set up a Selenium Grid instance, I use the Selenium Foundation library. The "standalone" JAR brings in scads of transitive dependencies you don't need that end up creating conflicts. I created a sample project (local-grid-utility) that launches a Selenium 2 Grid that dispenses PhantomJS sessions, which could be easily altered to launch a Selenium 3 Grid dispensing HtmlUnit sessions.
The key to getting HtmlUnit sessions stood up in Grid is providing a complete class path that includes all of the dependencies. Here's what the library assembles for HtmlUnitDriver 2.40.0:
${JAVA_HOME}/jre/bin/java -cp ${M2_ROOT}/repository/org/seleniumhq/selenium/selenium-support/3.141.59/selenium-support-3.141.59.jar:${M2_ROOT}/repository/org/seleniumhq/selenium/htmlunit-driver/2.40.0/htmlunit-driver-2.40.0.jar:${M2_ROOT}/repository/org/apache/commons/commons-lang3/3.8.1/commons-lang3-3.8.1.jar:${M2_ROOT}/repository/xerces/xercesImpl/2.12.0/xercesImpl-2.12.0.jar:${M2_ROOT}/repository/org/seleniumhq/selenium/selenium-api/3.141.59/selenium-api-3.141.59.jar:${M2_ROOT}/repository/net/sourceforge/htmlunit/htmlunit-core-js/2.40.0/htmlunit-core-js-2.40.0.jar:${M2_ROOT}/repository/org/eclipse/jetty/websocket/websocket-api/9.4.28.v20200408/websocket-api-9.4.28.v20200408.jar:${M2_ROOT}/repository/xalan/xalan/2.7.2/xalan-2.7.2.jar:${M2_ROOT}/repository/org/eclipse/jetty/jetty-xml/9.4.28.v20200408/jetty-xml-9.4.28.v20200408.jar:${M2_ROOT}/repository/org/apache/httpcomponents/httpclient/4.5.12/httpclient-4.5.12.jar:${M2_ROOT}/repository/org/eclipse/jetty/jetty-http/9.4.28.v20200408/jetty-http-9.4.28.v20200408.jar:/Applications/Eclipse.app/Contents/Eclipse/plugins/com.beust.jcommander_1.72.0.jar:${M2_ROOT}/repository/org/apache/httpcomponents/httpcore/4.4.13/httpcore-4.4.13.jar:${M2_ROOT}/repository/xml-apis/xml-apis/1.4.01/xml-apis-1.4.01.jar:${M2_ROOT}/repository/commons-codec/commons-codec/1.11/commons-codec-1.11.jar:${M2_ROOT}/repository/net/sourceforge/htmlunit/neko-htmlunit/2.40.0/neko-htmlunit-2.40.0.jar:${M2_ROOT}/repository/com/squareup/okhttp3/okhttp/3.11.0/okhttp-3.11.0.jar:${M2_ROOT}/repository/commons-net/commons-net/3.6/commons-net-3.6.jar:${M2_ROOT}/repository/org/seleniumhq/selenium/selenium-server/3.141.59/selenium-server-3.141.59.jar:${M2_ROOT}/repository/xalan/serializer/2.7.2/serializer-2.7.2.jar:${M2_ROOT}/repository/org/eclipse/jetty/jetty-client/9.4.28.v20200408/jetty-client-9.4.28.v20200408.jar:${M2_ROOT}/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar:${M2_ROOT}/repository/org/apache/commons/commons-text/1.8/commons-text-1.8.jar:${M2_ROOT}/repository/org/eclipse/jetty/jetty-io/9.4.28.v20200408/jetty-io-9.4.28.v20200408.jar:${M2_ROOT}/repository/net/sourceforge/htmlunit/htmlunit-cssparser/1.5.0/htmlunit-cssparser-1.5.0.jar:${M2_ROOT}/repository/commons-logging/commons-logging/1.2/commons-logging-1.2.jar:${M2_ROOT}/repository/org/apache/httpcomponents/httpmime/4.5.12/httpmime-4.5.12.jar:${M2_ROOT}/repository/org/seleniumhq/selenium/jetty-repacked/9.4.12.v20180830/jetty-repacked-9.4.12.v20180830.jar:${M2_ROOT}/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar:${M2_ROOT}/repository/org/eclipse/jetty/websocket/websocket-client/9.4.28.v20200408/websocket-client-9.4.28.v20200408.jar:${M2_ROOT}/repository/org/brotli/dec/0.1.2/dec-0.1.2.jar:${M2_ROOT}/repository/com/google/guava/guava/28.1-jre/guava-28.1-jre.jar:${M2_ROOT}/repository/com/squareup/okio/okio/1.14.0/okio-1.14.0.jar:${M2_ROOT}/repository/net/sourceforge/htmlunit/htmlunit/2.40.0/htmlunit-2.40.0.jar:${M2_ROOT}/repository/org/seleniumhq/selenium/selenium-remote-driver/3.141.59/selenium-remote-driver-3.141.59.jar:${M2_ROOT}/repository/commons-io/commons-io/2.6/commons-io-2.6.jar:${M2_ROOT}/repository/org/eclipse/jetty/jetty-util/9.4.28.v20200408/jetty-util-9.4.28.v20200408.jar:${M2_ROOT}/repository/org/eclipse/jetty/websocket/websocket-common/9.4.28.v20200408/websocket-common-9.4.28.v20200408.jar org.openqa.grid.selenium.GridLauncherV3 -role node -servlets org.openqa.grid.web.servlet.LifecycleServlet -host 192.168.1.6 -port 34464 -nodeConfig /Users/bb8d/github/Selenium-Foundation/target-s3/classes/nodeConfig-s3-9CF40153.json
You could create your own standalone package to bundle up the dependencies if that suits your fancy. The node server is provided by the GridLauncherV3 class. I also add the LifecycleServlet plugin to enable remote shutdown. The host specification corresponds to the local machine, and the port is auto-selected from whatever is currently available.
The JSON configuration file looks like this:
{
"capabilities": [
{
"browserName": "htmlunit",
"javascriptEnabled": true,
"maxInstances": 5,
"seleniumProtocol": "WebDriver"
}
],
"custom": {
},
"debug": false,
"downPollingLimit": 2,
"enablePlatformVerification": true,
"host": "0.0.0.0",
"hub": "http:\u002f\u002f192.168.1.6:4445\u002fwd\u002fhub",
"maxSession": 5,
"nodePolling": 5000,
"nodeStatusCheckTimeout": 5000,
"port": -1,
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"register": true,
"registerCycle": 5000,
"role": "node",
"servlets": [
],
"unregisterIfStillDownAfter": 60000,
"withoutServlets": [
]
}
The critical settings can be specified in the command line, but I think the config file is more manageable.

Selenium: Session externalKey not available

The Selenium Grid Hub I am running displays this error message whenever a Robot Framework automation test (kicked off by a Jenkins job) requests a Chrome browser from the Hub:
Session [(null externalkey)] not available and is not among the last
1000 terminated sessions.
I've spent hours searching and trying proposed solutions with no luck.
Running a Linux server as a Selenium Grid Hub and a windows server as a Selenium Grid Node. Have tried each node with versions 3.0.1, 3.5.3, 3.13.0, and 3.141.59 of the selenium-server-standalone-<version>.jar files. A third server runs Jenkins.
The Hub appears to be config'd properly because this works fine:
http://10.1.10.231:4444/grid/console
I've also run the jar files at the command line with -debug at the end but no further information is displayed. Have also tried -Dselenium.LOGGER.level=WARNING on the "java side" of the command line with no further information displayed than using -debug.
We have another set of servers with the same config that runs just fine.
Anyone have an idea of what this message is indicating?
Nitty-gritty follows.
Grid Hub:
java -jar -Xms1024m -Xmx2048m selenium-server-standalone-3.141.59.jar -role hub -hubConfig ./hub-config.json
hub-config.json:
{
"port": 4444,
"newSessionWaitTimeout": -1,
"servlets" : [],
"withoutServlets": [],
"custom": {},
"capabilityMatcher": "org.openqa.grid.internal.utils.DefaultCapabilityMatcher",
"throwOnCapabilityNotPresent": true,
"cleanUpCycle": 5000,
"role": "hub",
"debug": false,
"browserTimeout": 360,
"timeout": 1800
}
Grid Node:
SET CHROMEDRIVER=.\bin\chromedriver_win32-v2.37\chromedriver.exe
SET IEDRIVER=.\bin\IEDriverServer.exe
SET FFDRIVER=.\bin\geckodriver-v0.11.1-win32\geckodriver.exe
START "SeleniumNode" java -Xms1024m -Xmx2048m ^
-Dwebdriver.gecko.driver=%FFDRIVER% ^
-Dwebdriver.ie.driver=%IEDRIVER% ^
-Dwebdriver.chrome.driver=%CHROMEDRIVER% -Dwebdriver.chrome.args="--disable-extensions" ^
-jar .\bin\selenium-server-standalone-3.141.59.jar -role node -nodeConfig .\node-config.json
node-config.json:
{
"capabilities":
[
{
"browserName": "firefox",
"maxInstances": 5,
"platform": "WINDOWS",
"seleniumProtocol": "WebDriver"
},
{
"browserName": "chrome",
"maxInstances": 5,
"platform": "WINDOWS",
"seleniumProtocol": "WebDriver"
},
{
"browserName": "internet explorer",
"maxInstances": 5,
"platform": "WINDOWS",
"seleniumProtocol": "WebDriver"
}
],
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"maxSession": 5,
"port": 5555,
"register": true,
"registerCycle": 5000,
"hub": "http://10.1.10.231:4444",
"nodeStatusCheckTimeout": 5000,
"nodePolling": 5000,
"role": "node",
"unregisterIfStillDownAfter": 60000,
"downPollingLimit": 2,
"debug": false,
"servlets" : [],
"withoutServlets": [],
"custom": {},
"browserTimeout": 0,
"timeout": 1800,
"cleanUpCycle": 2000
}
Figured this one out! Hopefully it will help others who have reported this error message and not received a solution.
As is most often the case with an issue like this, especially when there's a known-working setup right beside the broken instance, it was an (embarrassingly) silly config issue. The Robot tests were pointing to this URL to get a browser:
http://10.1.10.75:4444/wd/hub/static/resource/hub.html
and it should have been pointing to this path:
http://10.1.10.75:4444/wd/hub
At least it was an easy fix.

Can't reach connection to Selenium Server hub from virtual node

So, I have PC with Windows 10 Pro, and virtual Windows 10 Pro installed on the same PC via VMWare workstation, network adapter mode in virtual OS set to "NAT".
I run this command on real OS (hub):
java -jar selenium-server-standalone-3.141.59.jar -role hub
And this command on virtual OS (node):
java -jar selenium-server-standalone-3.141.59.jar -role node -nodeConfig nodeconfig.json
nodeconfig.json contains this:
{
"capabilities":
[
{
"browserName": "chrome",
"maxInstances": 5,
"seleniumProtocol": "WebDriver"
}
],
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"maxSession": 5,
"port": -1,
"register": true,
"registerCycle": 5000,
"hub": "http://192.168.116.1:4444/grid/register/",
"nodeStatusCheckTimeout": 5000,
"nodePolling": 5000,
"role": "node",
"unregisterIfStillDownAfter": 60000,
"downPollingLimit": 2,
"debug": false,
"servlets" : [],
"withoutServlets": [],
"custom": {}
}
When I'm trying to connect node to hub, I got this message in console:
Registered a node http://192.168.116.1:4444
Marking the node http://192.168.116.1:4444 as down: cannot reach the node for 2 tries
How can I solve this ? I already tried to set virtual OS network adapter mode to "Bridged", but in this case I can't get connection at all ...
That was a problem with firewall, after turning it off everything seems to work.

Setting up a node on a VM

I have my selenium all configured but seem to be having issues getting it to launch the browser on my windows VM. I think it's because I haven't haven't included the correct path to the driver. Currently it stands as...
java -jar .\selenium-server-standalone-2.39.0.jar -role node -port 1001 -hub http://xxx.xxx.xx.xxx:4444/grid/register
After reading online I assumed it's cause I'm missing the following...
-Dwebdriver.ie.driver="C:\selenium\IEDriverServer.exe"
I've tried various different ways to include this path into the above statement but keep getting 'poorly formatted Java property setting (I expect to see '=') -Dwebdriver: Usage: java -jar selenium-server.jar [-interactive] [options]'
if anyone could show me the correct format I'd really appreciate it.
Thanks.
According to documentation, you can use a system property:
System.setProperty("webdriver.ie.driver", "C:\selenium\IEDriverServer.exe")
Of course this path would have be the correct on the remote machine!
If you use Maven for your builds, then the Selenium binary downloader might interest you.
I would suggest running the node and having the node load a .json config file that looks something like this.
{
"capabilities":
[
{
"browserName": "firefox",
"acceptSslCerts": true,
"javascriptEnabled": true,
"takesScreenshot": false,
"firefox_profile": "",
"browser-version": "31",
"platform": "WINDOWS",
"maxInstances": 5
},
{
"browserName": "chrome",
"maxInstances": 5,
"platform": "WINDOWS"
},
{
"browserName": "internetExplorer",
"maxInstances": 1,
"platform": "WINDOWS"
}
],
"configuration":
{
"_comment" : "This is configuration for the grid node 1.",
"cleanUpCycle": 2000,
"timeout": 30000,
"proxy": "org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
"port": 5555,
"host": ip,
"Dwebdriver.chrome.driver=C:\\Selenium\\chromedriver.exe": "",
"Dwebdriver.ie.driver=C:\\Selenium\\IEDriverServer.exe": "",
"register": true,
"hubPort": 4444,
"maxSessions": 5
}
}