How to remove Selenium Grid Node from grid if 'Failed to Connect'? - selenium

I am using AWS EC2 instances to create a Selenium Grid. I have one instance acting as the Hub and other instances acting as nodes.
Once I terminate the nodes, they still remain in the grid for quite sometime (like 15-20 minutes) before being completely removed, therefore, the API is returning incorrect values:
For example, the API is returning:
"slotCounts": {
"free": 9,
"total": 10
},
But really, there are ONLY 4 slots total, because the other nodes are no longer connected. Is there a configuration setting that will remove the nodes from grid faster after there is no longer a connection?
I start the Hub like this:
java -jar selenium-server-standalone-3.141.59.jar -role hub -port 4444;
And the clients like this:
java -jar -Dwebdriver.chrome.driver=chromedriver selenium-server-standalone-3.141.59.jar -role node -hub http://10.0.1.119:4444/grid/register -browser browserName=chrome,maxInstances=2,version=latest,seleniumProtocol=WebDriver
The nodes do eventually disappear from the grid but only after they have been quit (by quit, I mean after the computer that runs the node has been shutdown) for quite awhile (maybe 10-20 mins).
I looked at this Stack Overflow question: SO
And I can't figure out if I need to set nodePolling, timeout, browsertimout or a combination of them?
Update:
I just tested it again, and after the computer that runs the nodes was shutdown, it took around 20-22 mins for the grid to reflect that in the API.

As per the Selenium Grid helptext:
-timeout, -sessionTimeout
<Integer> in seconds : Specifies the timeout before the server
automatically kills a session that hasn't had any activity in the last X
seconds. The test slot will then be released for another test to use.
This is typically used to take care of client crashes. For grid hub/node
roles, cleanUpCycle must also be set. If a node does not specify it, the
hub value will be used.
So once the nodes are terminated to remove them completely from the console you can configure either of the following parameters:
-timeout
java -jar -Dwebdriver.chrome.driver=chromedriver selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.225.173:4444/grid/register -browser browserName=chrome,maxInstances=2 -timeout 2
-sessionTimeout:
java -jar -Dwebdriver.chrome.driver=chromedriver selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.225.173:4444/grid/register -browser browserName=chrome,maxInstances=2 -sessionTimeout 3
cleanUpCycle:
java -jar -Dwebdriver.chrome.driver=chromedriver selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.225.173:4444/grid/register -browser browserName=chrome,maxInstances=2 -cleanUpCycle 4
References
You can find a couple of relevant detailed discussions in:
Is there a way too prevent selenium automatically terminating idle sessions?

Related

Why do we need org.openqa.grid.selenium.GridLauncherV3 in selenium grid

Is there any specific reason why we use GridlauncherV3, Currently doing a project where I use custom servlets for executing some task
For launching node:
java -Dwebdriver.chrome.driver=chromedriver -cp testservlet.jar:selenium-server-standalone.jar org.openqa.grid.selenium.GridLauncherV3 -role node -maxSession 10 -port 4444 -host localhost -hub http://localhost:4444/grid/register -browser browserName=chrome,version=103,platform=LINUX,maxInstances=10 -servlets testservlet.copies.DemoServlet,testservlet.copies.Demo2Servlet
Couldn't find any reason but if removed it throws an error, Were it needs a params
Unrecognized option: -role
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Thanks in advance.
As you are using are using java -cp, it is a requirement to specify the class that contains the main() method.
In the case of creating a jvm to run as selenium grid, you require the class that contains the main() method, in this case that is org.openqa.grid.selenium.GridLauncherV3.
This method is used to initiate the grid with all the required configuration in your cli command.
See the method here: GridLauncherV3

Unable to define maxInstances when launching hub via Selenium 3.0.0 beta3

I've been using the selenium-server-standalone-2.53.0.jar and recently tried to upgrade to version 3.0.0-beta3.
I'm attempting to spin-up a hub using the maxInstances parameter with the following command:
java -jar %~dp0DriverRepo\selenium-server-standalone-3.0.0-beta3.jar -role hub -port 5555 -maxInstances 9
This was working in 2.53.0, but in 3.0.0-beta3 I'm met with the following exception:
Exception in thread "main" com.beust.jcommander.ParameterException: Unknown option: -maxInstances
at com.beust.jcommander.JCommander.parseValues(JCommander.java:742)
at com.beust.jcommander.JCommander.parse(JCommander.java:282)
at com.beust.jcommander.JCommander.parse(JCommander.java:265)
at com.beust.jcommander.JCommander.<init>(JCommander.java:210)
at org.openqa.grid.selenium.GridLauncherV3$2.setConfiguration(GridLauncherV3.java:224)
at org.openqa.grid.selenium.GridLauncherV3.buildLauncher(GridLauncherV3.java:138)
at org.openqa.grid.selenium.GridLauncherV3.main(GridLauncherV3.java:67)
Apparently maxInstances is no longer a valid argument. I've searched for documentation regarding any changes that may have been made for use of the maxInstances parameter but I've had no luck. Has anyone else run into this issue, or is anyone aware of the proper way to spin-up a hub in 3.0.0-beta3 in a comparable way?
I don't remember ever using an argument called maxInstances. I think that earlier there was no validation for invalid command line arguments but with Selenium 3, they perhaps enabled it.
Here's an example of why I feel my theory is true
Selenium 2.53.1 output wherein I am providing an invalid argument named krishnan
selenium-server -role hub -krishnan 100
22:28:37.762 INFO - Launching Selenium Grid hub
2016-09-26 22:28:38.366:INFO::main: Logging initialized #758ms
22:28:38.378 INFO - Will listen on 4444
22:28:38.421 INFO - Will listen on 4444
2016-09-26 22:28:38.424:INFO:osjs.Server:main: jetty-9.2.z-SNAPSHOT
2016-09-26 22:28:38.452:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler#32eebfca{/,null,AVAILABLE}
2016-09-26 22:28:38.479:INFO:osjs.ServerConnector:main: Started ServerConnector#6ec8211c{HTTP/1.1}{0.0.0.0:4444}
And here's how Selenium 3 beta version behaves for the same command line.
java -jar selenium-server-standalone-3.0.0-beta2.jar -role hub -krishnan 100
Exception in thread "main" com.beust.jcommander.ParameterException: Unknown option: -krishnan
at com.beust.jcommander.JCommander.parseValues(JCommander.java:742)
at com.beust.jcommander.JCommander.parse(JCommander.java:282)
at com.beust.jcommander.JCommander.parse(JCommander.java:265)
at com.beust.jcommander.JCommander.<init>(JCommander.java:210)
at org.openqa.grid.selenium.GridLauncherV3$2.setConfiguration(GridLauncherV3.java:216)
at org.openqa.grid.selenium.GridLauncherV3.buildLauncher(GridLauncherV3.java:130)
at org.openqa.grid.selenium.GridLauncherV3.main(GridLauncherV3.java:67)
Selenium has never had any argument named maxInstances. It only had something called maxSession`.
The only usage for maxInstances has been within a node configuration file as shown here which is passed to a Selenium node via the -nodeConfig argument. This represents the number of concurrent browser instances per browser flavor that can be opened up in a node.

Running two instance of ie/firefox browser through selenium remote webdriver

My scenario is, I will launch a instance of a browser and perform some operations and then without closing current browser new browser of same type will be opened and verification needs to be done.
Webdriver driver = driverInitialize.getDriver();
//Perform some operations
Webdriver driver2 = driverInitialize.getDriver();
//Perform some operations
The problem is only with firefox and IE as second instance of browser is not getting launched till the timeout of first instance happens but the same works fine with chrome browser. On launching the node and hub, for firefox and IE it says "WARN - Max instance not specified. Using default = 1" even after specifying the maximum instance in the respective nodes. The commands used for launching hub and node is presented below
Hub : java -jar selenium-server-standalone-2.44.0.jar -role hub -port 4444
Firefox Node : java -jar selenium-server-standalone-2.44.0.jar -role node -hub http://127.0.0.1:4444/grid/register -port 5553 -browser browserName=firefox,maxInstance=5 -Dwebdriver.version="30"
IE Node : java -jar selenium-server-standalone-2.44.0.jar -role webdriver -hub http://127.0.0.1:4444/grid/register -port 5555 browserName=ie,maxInstance=5 -Dwebdriver.ie.driver="C:\IEDriverServer.exe"
Chrome Node : java -jar selenium-server-standalone-2.44.0.jar -role webdriver -hub http://127.0.0.1:4444/grid/register -port 5555 browserName=chrome maxInstance=5 -Dwebdriver.chrome.driver="C:\chromedriver.exe"
Versions of browser
Chrome : 40.0.2214.115; Firefox : 34.0; IE : 10.0.9200.17228;
I think you should take a look at this stack overflow question:
Selenium Grid: MaxSessions vs MaxInstances
Max sessions will override max instances, so you need to set both.
If you have max sessions = 1 and max instances = 10, only 1 test will be allowed to run. Check your configuration in this case.
You can also see these options on the Grid 2 wiki:
https://code.google.com/p/selenium/wiki/Grid2
EDIT**
This has moved to: https://github.com/SeleniumHQ/selenium/wiki/Grid2

"-role" option in Selenium Grid

What is the difference between "-role node" and "-role webdriver" in selenium grid ?
i.e.
is there any difference between
java -jar selenium-server-standalone-2.16.0.jar -role node -hub http://a.b.c.d:4444/grid/register
and
java -jar selenium-server-standalone-2.16.0.jar -role webdriver -hub http://a.b.c.d:4444/grid/register
?
Ok..I got the answer :)
The following lines throws light on my question :
For backwards compatibility "wd"(webdriver) and "rc"(remotecontrol) roles are still a valid subset of the "node" role. But those roles limit the types of remote connections to their corresponding API, while "node" allows both RC and WebDriver remote connections.

Selenium Grid: MaxSessions vs MaxInstances

I was wondering if anybody could shed some light on a Selenium question that has been giving us a bit of head scratching.
We're confused on meaning of MaxSession and MaxInstances of Selenium Grid. We think that the MaxSession is the total number of test sessions that can run on a single node. And we also think that the MaxInstances is the total number of browsers that a test can open.
Or is MaxInstances the total number of browsers available to the node?
The command that we are using is:
java -Xrs -jar selenium-server.jar -role node -port 44506 -hub http://localhost:44500
/grid/register -firefoxProfileTemplate SeleniumProfile -timeout 300000 -browser
"browserName=firefox,maxInstances=10,platform=ANY,seleniumProtocol=WebDriver" -browser
"browserName=chrome,maxInstances=10,platform=ANY,seleniumProtocol=WebDriver"
We think the way we are using our node (above) is 5 concurrent test sessions by default.
Does each test have 20 browsers available to it?
Or does each test session share the 20 browsers (10 chrome/10 FF) in a pool - with the other test sessions?
Nice question....i would say it's bit confusing.... But will try to answer it in simple terms..
MaxInstances This says....how many instances of same version of browser can run over the Remote System.
For example, i have a FF12,IE and i declared the command as follows
-browser browserName=firefox,version=12,maxInstances=5,platform=LINUX
-browser browserName=InternetExplorer,version=9.0,maxInstances=5,platform=LINUX
So i can run 5 instances of Firefox 12 and as well as 5 instances of IE9 at the same time in remote machine. So total user can run 10 instances of different browsers (FF12 & IE9) in parallel.
MaxSession This says....how many browsers (Any Browser and any version) can run in parallel at a time in the remote system. So this overrides the Max Instances settings and can restrict the number of browser instances that can run in parallel.
For above example, when maxSession=1 forces that you never have more than 1 browser running.
With maxSession=2 you can have 2 Firefox tests at the same time, or 1 Internet Explorer and 1 Firefox test).
Irrespective of what MaxInstances you have defined.
For more clear info do visit - https://seleniumhq.github.io/docs/grid.html
To expand upon Anuragh27crony's answer, I've drawn up a quick diagram:
If this is your node config, then you can execute at most 5 tests in parallel, for example in the following combinations:
3 * chrome, 2 * firefox
2 * chrome, 2 * firefox, 1 * edge
5 * edge
3 * chrome
The following combinations are NOT possible:
4 * chrome (exceeds Chrome maxInstances)
6 * edge (exceeds maxSessions)
3 * chrome, 3 * firefox (exceeds maxSessions)
As mentioned by Anuragh, MaxInstances applies to a specific browser, while MaxSessions applies to the entire node.
As per the documentation in Configuring the nodes by default, starting a Selenium Grid Node allows for concurrent usage of 11 browsers:
5 Firefox
5 Chrome
1 Internet Explorer
The maximum number of concurrent tests is set to 5 by default. To change this and other browser settings, you can pass in parameters to each -browser switch (each switch represents a node based on your parameters). If you use the -browser parameter, the default browsers will be ignored and only what you specify command line will be used.
maxInstances
maxInstances is an optional parameter which can be passed through the -browser optional parameter.
Usecase 1
To configure a Selenium Grid Node for 20 instances of Firefox version=X.Y.Z you can use the following solution:
Command:
java -Dwebdriver.gecko.driver=geckodriver.exe -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.1.125:4444/grid/register -browser browserName=firefox,version=X.Y.Z,maxInstances=20,platform=WINDOWS
Node Console Logs:
C:\Utility\SeleniumGrid>java -Dwebdriver.gecko.driver=geckodriver.exe -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.1.125:4444/grid/register -browser browserName=firefox,version=X.Y.Z,maxInstances=20,platform=WINDOWS
16:54:11.843 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.59, revision: e82be7d358
16:54:12.003 INFO [GridLauncherV3.lambda$buildLaunchers$7] - Launching a Selenium Grid node on port 6318
2020-02-14 16:54:12.523:INFO::main: Logging initialized #1022ms to org.seleniumhq.jetty9.util.log.StdErrLog
16:54:12.860 INFO [WebDriverServlet.<init>] - Initialising WebDriverServlet
16:54:12.974 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 6318
16:54:12.974 INFO [GridLauncherV3.lambda$buildLaunchers$7] - Selenium Grid nodeis up and ready to register to the hub
16:54:13.161 INFO [SelfRegisteringRemote$1.run] - Starting auto registration thread. Will try to register every 5000 ms.
16:54:13.765 INFO [SelfRegisteringRemote.registerToHub] - Registering the node to the hub: http://192.168.1.125:4444/grid/register
16:54:13.962 INFO [SelfRegisteringRemote.registerToHub] - The node is registered to the hub and ready to use
Grid Console Snapshot:
Usecase 2
To configure a Selenium Grid Node for 10 instances of Firefox version=A.B.C and 20 instances of Chrome version=X.Y.Z you can use the following solution:
Command:
java -Dwebdriver.gecko.driver=geckodriver.exe -Dwebdriver.chrome.driver=chromedriver.exe -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.1.125:4444/grid/register -browser browserName=firefox,version=X.Y.Z,maxInstances=10,platform=WINDOWS -browser browserName=chrome,version=X.Y.Z,maxInstances=20,platform=WINDOWS
Grid Console Snapshot:
-maxSession
-maxSession is also an optional parameter which can be passed as a main parameter to configure the maximum number of browsing contexts that can run in parallel on a particular node. This is different from the maxInstance of supported browsers (Example: For a node that supporting Firefox version A.B.C, Firefox version P.Q.R and Chrome version X.Y.Z, maxSession=1 will ensure that you never have more than 1 browser running. With maxSession=2 you can have 2 Firefox tests executing at the same time, or 1 Firefox and 1 Chrome test).
Example:
java -Dwebdriver.gecko.driver=geckodriver.exe -Dwebdriver.chrome.driver=chromedriver.exe -jar selenium-server-standalone-3.141.59.jar -role node -hub http://192.168.1.125:4444/grid/register -browser "browserName=firefox,version=A.B.C,maxInstances=10,platform=WINDOWS" -browser "browserName=firefox,version=P.Q.R,maxInstances=10,platform=WINDOWS" -browser "browserName=chrome,version=X.Y.Z,maxInstances=20,platform=WINDOWS" -maxSession 2
Grid Console Snapshot:
MaxInstances: this gives you more slots per node, For example, MaxInstances=5 will allow a maximum of 5 browsers per node
MaxSession: sets the maximum amount of tests that can run at the same time in a node.
if MaxInstances=5, then MaxSession should also be at least 5.
see more at https://github.com/SeleniumHQ/docker-selenium