How to run test on specific browser by selenium grid - selenium

I have register two platform with chrome browser on windows 7 and windows 10 into selenium grid server.
I want to run a test at Chrome browser on Windows 10 but when i run the test, the test randomly run on Windows 7 and randomly run on Windows 10 on chrome.
Do you have an idea how to do the configuration to run the test on specific browser and platform when we have registered multiple platform?
hub:
java -jar selenium-server-standalone-3.7.1.jar -role hub
register node on windows 7:
java -Dwebdriver.chrome.driver=chromedriver_2.33.exe -jar selenium-server-standalone-3.7.1.jar -role node -hub http://localhost:4444
register node on windows 10:
java -Dwebdriver.chrome.driver=chromedriver_2.33.exe -jar selenium-server-standalone-3.7.1.jar -role node -hub http://localhost:4444
protractor.conf.js
exports.config = {
specs: [
'**/*.mytest.e2e-spec.ts'
],
multiCapabilities: [
{
browserName: 'chrome',
platform: 'WIN10',
}
],
seleniumAddress: 'http://localhost:4444/wd/hub',
baseUrl: 'http://localhost:4200/',
framework: 'jasmine',
jasmineNodeOpts: {
print: function () {
}
},
onPrepare: function () {
require('ts-node').register({
project: 'e2e/tsconfig.e2e.json'
});
}
};

Selenium Grid Installation Process:-
Download Selenium server standalone jar file in which you want to
create Hub
Create a new java project and add it to build path
Start the Hub Using Below Command in your machine:-
java -jar selenium-server-standalone-3.9.1.jar –role hub -port 4446
Open ip address/grid/console in host browser "or" localhost: portnumber/grid/console
Start the Node Using Below Command in another machine:
Download Selenium server standalone jar file in another laptop
it is not necessary that node contains eclipse. But Java Should be installed.
go to file path where selenium standalone is kept
Type Below command
java -jar selenium-server-standalone-3.9.1.jar –role webdriver –hub ipaddress/grid/
register –port 5566
(but Here your test will fail because here file path for chrome Driver or gecko driver is not provided in the hub.)
java –Dwebdriver.chrome.driver="provide path for chrome driver" -jar selenium-server-standalone-3.9.1.jar –role webdriver –hub ipaddress/grid/register –port 5566

I would suggest that you do the following
Create a node configuration file as seen here, wherein for the WINDOWS7 node, you specify the appropriate platform (make sure you pick a value from here)
Now you start off the node by specifying this newly created nodeConfig json using the command line parameter -nodeConfig node.json ( For more details refer to my blog post here )
Now based on the PLATFORM capabilities specified by your test, it would be routed to the appropriate node.

You need to tweak the commands for registering the nodes as follows :
Register node on Windows 7:
java -Dwebdriver.chrome.driver=chromedriver_2.33.exe -jar selenium-server-standalone-3.7.1.jar -role node -hub http://localhost:4444/grid/register
Register node on Windows 10:
java -Dwebdriver.chrome.driver=chromedriver_2.33.exe -jar selenium-server-standalone-3.7.1.jar -role node -hub http://localhost:4444/grid/register

As per your question and DebanjanB answer, You have two nodes and two hubs. You need to register the nodes with same hub. Please try with following configuration, it may works.
Start your hub on windows 7 Machine with following command,
java -jar selenium-server-standalone-3.7.1.jar -role hub
Start/register your first node on windows 7 with following command,
java -Dwebdriver.chrome.driver=chromedriver_2.33.exe -jar selenium-server-standalone-3.7.1.jar -role node
start/register your second node on windows 10 with following command,
java -Dwebdriver.chrome.driver=chromedriver_2.33.exe -jar selenium-server-standalone-3.7.1.jar -role node -hub http://"IP Address OF Windows 7 machine":4444/grid/register
To verify the nodes are listed on hub, just open the url http://localhost:4444/grid/console on windows 7 machine. Then, verify your nodes are listed on the page.
You have to run your script from the windows 7 machine because your selenium address is using localhost address( seleniumAddress: 'http://localhost:4444/wd/hub'). if you want to run from windows 10 machine or any other machine. change the selenium address as 'http://"windows7ipaddress":4444/wd/hub' in config file.
I have tested it on my machine it is working fine. It will start the chrome on windows 10 machine as your multiCapabilities platform value is WIN10.
note: replace the value of ip address value in place where ever required.

Related

How to run a test on a specific IP using Selenium Grid

i have a few queries regarding Selenium Grid.
Consider the below scenario:
Machine A:
registered as hub by command=
java -jar selenium-server-standalone-2.44.0.jar -role hub
Machine B:having Windows 7 and chrome browser
registered as node by command=java -Dwebdriver.chrome.driver="path of chrome driver" –jar selenium-server-standalone-2.44.0.jar –role webdriver –hub http://ipnameofHub:4444/grid/register -port 5566
Machine C: having Windows 7 and chrome browser​
registered as node​ by command=java -Dwebdriver.chrome.driver="path of chrome driver" –jar selenium-server-standalone-2.44.0.jar –role webdriver –hub http://ipnameofHub:4444/grid/register -port 5566
Machine D: having Windows 7 and chrome browser​
registered as node​ by command=java -Dwebdriver.chrome.driver="path of chrome driver" –jar selenium-server-standalone-2.44.0.jar –role webdriver –hub http://ipnameofHub:4444/grid/register -port 5566
DesiredCapabilities dc=new DesiredCapabilities();
dc.setBrowserName("chrome");
dc.setPlatform(Platform.WINDOWS);
​WebDriver driver=new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),dc);
When i run a test, which node will grid choose and on what basis, since all the nodes have the same platform and browser as specified in the DesiredCapabilities.
Is the node randomly chosen or the first node matching the DesiredCpabilities is chosen?
Question No.2
If i want to run a test on specifically Machine D, how can that be done.
Thanks in advance.
it is chosen based on first matching capability and available node.
To run on specific node, you can write your own algorithm to choose. the following link may help to write custom capability matcher https://www.assertthat.com/posts/make_your_selenium_grid_nodes_personalized

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

How to run chrome browser in grid using MAC as hub and Windows as Nod?

Hub: MAC 64-bit
Nod: Windows 32-bit
Unable to run chrome browser using Selinum grid MAC as hub and Windows as nod?
using below code i am getting an error (The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list Command duration or timeout: 668 milliseconds)
public void chromeWindows() throws MalformedURLException{
System.setProperty("webdriver.chrome.driver", "/Users/vinayakkhatate/Desktop/jar/chromedriver2");
ChromeOptions opt = new ChromeOptions();
opt.setBinary("C:/Users/user/AppData/Local/Google/Chrome/Application/chrome.exe");
DesiredCapabilities capabilies = DesiredCapabilities.chrome();
capabilies.setBrowserName("chrome");
capabilies.setPlatform(Platform.VISTA);
driver = new RemoteWebDriver(new URL("http://10.0.11.118:5566/wd/hub"), capabilies);
driver.get(baseUrl);
System.out.println(driver.getTitle());
driver.close();
driver.quit();
}
I have solution to run Chrome browser from Mac machine to Windows Vista
(download and save chromedriver in windows vista machine)
Start the hub in Mac with below command
java -jar selenium-server-standalone-2.33.0.jar -role hub
Start the node in windows with below command
java -jar selenium-server-standalone-2.33.0.jar -role node -hub http://localhost:4444/grid/register -maxSession 15 -browser browserName="chrome",version=ANY,platform=WINDOWS,maxInstances=5 -Dwebdriver.chrome.driver=pathtochromedriver\chromedriver.exe
Now write code in eclipse in Mac machine
DesiredCapabilities capabilies = DesiredCapabilities.chrome();
capabilies.setBrowserName("chrome");
capabilies.setPlatform(Platform.ANY);
driver = new RemoteWebDriver(new URL("http://<ip address of windows machine>:5555/wd/hub"), capabilies);
Actually, the chromedriver.exe has to be stored on the Windows node. I do it by creating subfolder /lib in my test folder where I store the chromedriver and all other selenium grid related stuff. Later on, when running the node, do it like this:
java -jar lib/selenium-server-standalone-2.28.0.jar -role node -hub http://localhost:4444/grid/register -maxSession 15 -browser browserName="chrome",version=ANY,platform=WINDOWS,maxInstances=15 -Dwebdriver.chrome.driver=lib\chromedriver.exe
especially note the -D switch:
-Dwebdriver.chrome.driver=lib\chromedriver.exe
Thats how I set up the chromedriver.exe path. Notice the relative path, so I do not have to care really about where in absolute path the tool is running. Hope it helps
EDIT
Obviously, the hub and node computers should be acessible by IP. For instance, my work PC has IP 10.131.7.11 in our internal network so if this would be the hub computer, then the node setup would be like this:
java -jar lib/selenium-server-standalone-2.28.0.jar -role node -hub http://10.131.7.11:4444/grid/register -maxSession 15 -browser browserName="chrome",version=ANY,platform=WINDOWS,maxInstances=15 -Dwebdriver.chrome.driver=lib\chromedriver.exe
Please note that the localhost changed to IP of the hub. So next steps for you are:
Set up hub and node to be on same network and being accessible by IP adress
Run hub on the mac machine
Run node on the vista, pointing out to the IP adress of the hub
Cross your fingers :)
And try running the chrome again
EDIT2
This is how I run chrome:
if (System.getProperty("os.name").contains("Windows")) {
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "chromedriver.exe");
} else {
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "chromedriver");
}
capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub"), capabilities);
Start the hub in Mac with below command
java -jar selenium-server-standalone-2.33.0.jar -role hub
Start the node in windows with below command
java -jar selenium-server-standalone-2.33.0.jar -role node -hub http://localhost:4444/grid/register -maxSession 15 -browser browserName="chrome",version=ANY,platform=WINDOWS,maxInstances=15 -Dwebdriver.chrome.driver=pathtochromedriver\chromedriver.exe
Download chromedriver from below location
https://code.google.com/p/chromedriver/downloads/list
Now intialize driver instance with below logic
System.setProperty("webdriver.chrome.driver", "/Users/test/chromedriver");
DesiredCapabilities dc=new DesiredCapabilities();
dc.setBrowserName("chrome");
dc.setPlatform(Platform.WINDOWS);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dc);
driver.get(Constants.SERVER_URL_NAME);

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

Selenium grid newbie questions

Ok, I have already several testcases written for Webdriver approach. But now I need get the Selenium Grid for possible stress testing of the webapp.
I found this demo but its unable to control the Firefox 11. Then I found out this wiki page which is two level higher than I can understand, but that JAR File is supposedly able to control the Firefox 11.
What I need - some resources how to get the Grid started and how to let it do simple test - like writing "Hello World" into Google search bar and then clicking "Search."
EDIT
This is the error when I am trying to run the grid as node
D:\_dev\selenium-grid-1.0.8\lib>java -jar selenium-server-standalone-2.20.0.jar -role node -hub http://localhost:4444/grid/register
22-Mar-2012 10:33:48 org.openqa.grid.selenium.GridLauncher main
INFO: Launching a selenium grid node
Exception in thread "main" java.lang.NoSuchMethodError: java.lang.String.isEmpty()Z
at org.openqa.grid.common.RegistrationRequest.getRemoteControlConfiguration(RegistrationRequest.java:585)
at org.openqa.grid.internal.utils.SelfRegisteringRemote.startRemoteServer(SelfRegisteringRemote.java:86)
at org.openqa.grid.selenium.GridLauncher.main(GridLauncher.java:72)
And this is output from server which seems ok
D:\_dev\selenium-grid-1.0.8\lib>java -jar selenium-server-standalone-2.20.0.jar -role hub
22-Mar-2012 10:33:33 org.openqa.grid.selenium.GridLauncher main
INFO: Launching a selenium grid server
360 [main] INFO org.seleniumhq.jetty7.server.Server - jetty-7.x.y-SNAPSHOT
422 [main] INFO org.seleniumhq.jetty7.server.handler.ContextHandler - started o.s.j.s.ServletContextHandler{/,null}
438 [main] INFO org.seleniumhq.jetty7.server.AbstractConnector - Started SocketConnector#0.0.0.0:4444
If you are working with windows, you need to add the path to firefox to your PATH environment variable. The reason for this is, that windows then knows what program to start, when you call firefox from command line, for example.
Then you need to donwload the selenium server standalone and then start the hub
java -jar selenium-server-standalone-2.20.0.jar -role hub
and also the client:
java -jar selenium-server-standalone-2.20.0.jar -role node -hub http://localhost:4444/grid/register
Now you need to create a new Java Project in Eclipse, for example like so:
class MyFristTest{
//using the #test annotation tells eclipse
//to use junit (or tells you to import it)
#Test
public void myTest(){
Selenium selenium = new DefaultSelenium(“localhost”, 4444, “*firefox”, “http://www.google.com”);
DesiredCapabilities capability = DesiredCapabilities.firefox();
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
driver.get("http://www.example.com");
driver.findElement(By.linkText("RFC 2606")).click();
driver.findElement(By.linkText("txt")).click();
}
If you now start the JUnit Test, it should magically work.