Skip to content

Commit bbd67a0

Browse files
authored
Merge pull request #1146 from seleniumbase/update-refactor-grid-capabilities
Update & refactor browser capabilities and Grid code
2 parents bbdaed4 + 3287de5 commit bbd67a0

File tree

13 files changed

+93
-61
lines changed

13 files changed

+93
-61
lines changed

examples/capabilities/ReadMe.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### <img src="https://seleniumbase.io/img/green_logo.png" title="SeleniumBase" width="32" /> Using Desired Capabilities
1+
<h3><img src="https://seleniumbase.io/img/green_logo.png" title="SeleniumBase" width="32" /> Using Desired Capabilities</h3>
22

33
You can specify browser desired capabilities for webdriver when running SeleniumBase tests on a remote SeleniumGrid server such as [BrowserStack](https://www.browserstack.com/automate/capabilities) or [Sauce Labs](https://wiki.saucelabs.com/display/DOCS/Platform+Configurator#/).
44

@@ -40,13 +40,15 @@ capabilities = {
4040

4141
(Note that the browser is now being specified in the capabilities file, rather than with ``--browser=BROWSER`` when using a **remote** Selenium Grid. If using a **local** Selenium Grid, specify the browser, eg: ``--browser=chrome`` or ``--browser=firefox``.)
4242

43-
<b>You can generate specific desired capabilities using:</b>
43+
<div><b>You can generate specific desired capabilities using:</b></div>
44+
4445
<ul>
4546
<li><a href="https://www.browserstack.com/automate/capabilities">BrowserStack desired capabilities</a></li>
4647
<li><a href="https://wiki.saucelabs.com/display/DOCS/Platform+Configurator#/">Sauce Labs desired capabilities</a></li>
4748
</ul>
4849

49-
<b>Parsing desired capabilities:</b>
50+
<div><b>Parsing desired capabilities:</b></div>
51+
5052
SeleniumBase has a desired capabilities parser that can capture all lines from the specified file in the following formats:
5153

5254
```python
@@ -81,18 +83,18 @@ pytest test_swag_labs.py --cap-string='{"browserName":"chrome","name":"test1"}'
8183
If you pass ``"*"`` into the ``"name"`` field of ``--cap-string``, the name will become the test identifier. Example:
8284

8385
```bash
84-
pytest test_swag_labs.py --cap-string='{"browserName":"chrome","name":"*"}' --server="127.0.0.1" --browser=chrome
86+
pytest my_first_test.py --cap-string='{"browserName":"chrome","name":"*"}' --server="127.0.0.1" --browser=chrome
8587
```
8688

8789
Example name: ``"my_first_test.MyTestClass.test_basics"``
8890

89-
### Using a local Selenium Grid
91+
<h3>Using a local Selenium Grid</h3>
9092

9193
If using a local Selenium Grid with SeleniumBase, start up the Grid Hub and nodes first:
9294

9395
```bash
94-
seleniumbase grid-hub start
95-
seleniumbase grid-node start
96+
sbase grid-hub start
97+
sbase grid-node start
9698
```
9799

98100
(The Selenium Server JAR file will be automatically downloaded for first-time Grid users. You'll also need Java installed to start up the Grid.)

examples/capabilities/mac_cap_file.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Desired capabilities example file for generic macOS Grid nodes
2+
3+
capabilities = {
4+
"platformName": "MAC",
5+
"browserVersion": "latest",
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Desired capabilities example file for Windows 10 Grid nodes
2+
3+
capabilities = {
4+
"platformName": "WIN10",
5+
"browserVersion": "latest",
6+
}

help_docs/desired_capabilities.md

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,65 @@
1-
<h3><img src="https://seleniumbase.io/img/logo6.png" title="SeleniumBase" width="32" /> Using Desired Capabilities</h3>
1+
<h3><img src="https://seleniumbase.io/img/green_logo.png" title="SeleniumBase" width="32" /> Using Desired Capabilities</h3>
22

3-
You can specify browser desired capabilities for webdriver when running SeleniumBase tests on a remote SeleniumGrid server such as [BrowserStack](https://www.browserstack.com/automate/capabilities), [Sauce Labs](https://wiki.saucelabs.com/display/DOCS/Platform+Configurator#/), or [TestingBot](https://testingbot.com/support/other/test-options).
3+
You can specify browser desired capabilities for webdriver when running SeleniumBase tests on a remote SeleniumGrid server such as [BrowserStack](https://www.browserstack.com/automate/capabilities) or [Sauce Labs](https://wiki.saucelabs.com/display/DOCS/Platform+Configurator#/).
44

55
Sample run commands may look like this when run from the [SeleniumBase/examples/](https://github.com/seleniumbase/SeleniumBase/tree/master/examples) folder: (The browser is now specified in the capabilities file.)
66

77
```bash
8-
pytest my_first_test.py --browser=remote --server=USERNAME:[email protected] --port=80 --cap_file=capabilities/sample_cap_file_BS.py
8+
pytest test_demo_site.py --browser=remote --server=USERNAME:[email protected] --port=80 --cap_file=capabilities/sample_cap_file_BS.py
99
```
1010

1111
```bash
12-
pytest my_first_test.py --browser=remote --server=USERNAME:[email protected] --port=80 --cap_file=capabilities/sample_cap_file_SL.py
12+
pytest test_demo_site.py --browser=remote --server=USERNAME:KEY@ondemand.us-east-1.saucelabs.com --port=443 --protocol=https --cap_file=capabilities/sample_cap_file_SL.py
1313
```
1414

1515
(Parameters: ``--browser=remote``, ``--server=SERVER``, ``--port=PORT``, and ``--cap_file=CAP_FILE.py``)
1616

1717
Here's an example desired capabilities file for BrowserStack:
18+
1819
```python
1920
desired_cap = {
20-
'os': 'OS X',
21-
'os_version': 'High Sierra',
22-
'browser': 'Chrome',
23-
'browser_version': '77.0',
24-
'browserstack.local': 'false',
25-
'browserstack.selenium_version': '3.141.59'
21+
"os": "OS X",
22+
"os_version": "Mojave",
23+
"browser": "Chrome",
24+
"browser_version": "latest",
25+
"browserstack.local": "false",
26+
"browserstack.selenium_version": "3.141.59",
2627
}
2728
```
2829

2930
Here's an example desired capabilities file for Sauce Labs:
31+
3032
```python
3133
capabilities = {
32-
'browserName': 'firefox',
33-
'browserVersion': '70.0',
34-
'platformName': 'macOS 10.13',
35-
'sauce:options': {
36-
}
34+
"browserName": "chrome",
35+
"browserVersion": "latest",
36+
"platformName": "macOS 10.14",
37+
"sauce:options": {},
3738
}
3839
```
3940

40-
(You'll notice that the browser is now being specified in the capabilities file, rather than with ``--browser=BROWSER``)
41+
(Note that the browser is now being specified in the capabilities file, rather than with ``--browser=BROWSER`` when using a **remote** Selenium Grid. If using a **local** Selenium Grid, specify the browser, eg: ``--browser=chrome`` or ``--browser=firefox``.)
42+
43+
<div><b>You can generate specific desired capabilities using:</b></div>
4144

42-
<b>You can generate specific desired capabilities using:</b>
4345
<ul>
4446
<li><a href="https://www.browserstack.com/automate/capabilities">BrowserStack desired capabilities</a></li>
4547
<li><a href="https://wiki.saucelabs.com/display/DOCS/Platform+Configurator#/">Sauce Labs desired capabilities</a></li>
46-
<li><a href="https://testingbot.com/support/other/test-options">TestingBot desired capabilities</a></li>
4748
</ul>
4849

49-
<b>Parsing desired capabilities:</b>
50+
<div><b>Parsing desired capabilities:</b></div>
51+
5052
SeleniumBase has a desired capabilities parser that can capture all lines from the specified file in the following formats:
51-
``'KEY': 'VALUE'``
52-
``'KEY': True``
53-
``'KEY': False``
54-
``caps['KEY'] = "VALUE"``
55-
``caps['KEY'] = True``
56-
``caps['KEY'] = False``
53+
54+
```python
55+
'KEY': 'VALUE'
56+
'KEY': True
57+
'KEY': False
58+
caps['KEY'] = "VALUE"
59+
caps['KEY'] = True
60+
caps['KEY'] = False
61+
```
62+
5763
(Each pair must be on a separate line. You can interchange single and double quotes.)
5864

5965
You can also swap ``--browser=remote`` with an actual browser, eg ``--browser=chrome``, which will combine the default SeleniumBase desired capabilities with those that were specified in the capabilities file when using ``--cap_file=FILE.py``. Capabilities will override other parameters, so if you set the browser to one thing and the capabilities browser to another, SeleniumBase will use the capabilities browser as the browser.
@@ -67,22 +73,28 @@ You'll need default SeleniumBase capabilities for:
6773

6874
You can also set browser desired capabilities from a command line string:
6975
Example:
76+
7077
```bash
7178
pytest test_swag_labs.py --cap-string='{"browserName":"chrome","name":"test1"}' --server="127.0.0.1" --browser=remote
7279
```
80+
7381
(Enclose cap-string in single quotes. Enclose parameter keys in double quotes.)
7482

7583
If you pass ``"*"`` into the ``"name"`` field of ``--cap-string``, the name will become the test identifier. Example:
84+
7685
```bash
77-
pytest test_swag_labs.py --cap-string='{"browserName":"chrome","name":"*"}' --server="127.0.0.1" --browser=chrome
86+
pytest my_first_test.py --cap-string='{"browserName":"chrome","name":"*"}' --server="127.0.0.1" --browser=chrome
7887
```
88+
7989
Example name: ``"my_first_test.MyTestClass.test_basics"``
8090

81-
<h3><img src="https://seleniumbase.io/img/sb_icon.png" title="SeleniumBase" width="30" /> Using a local Selenium Grid</h3>
91+
<h3>Using a local Selenium Grid</h3>
8292

8393
If using a local Selenium Grid with SeleniumBase, start up the Grid Hub and nodes first:
94+
8495
```bash
85-
seleniumbase grid-hub start
86-
seleniumbase grid-node start
96+
sbase grid-hub start
97+
sbase grid-node start
8798
```
99+
88100
(The Selenium Server JAR file will be automatically downloaded for first-time Grid users. You'll also need Java installed to start up the Grid.)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ packaging>=21.3;python_version>="3.6"
55
setuptools>=44.1.1;python_version<"3.5"
66
setuptools>=50.3.2;python_version>="3.5" and python_version<"3.6"
77
setuptools>=59.6.0;python_version>="3.6" and python_version<"3.7"
8-
setuptools>=60.0.5;python_version>="3.7"
8+
setuptools>=60.1.0;python_version>="3.7"
99
setuptools-scm>=5.0.2;python_version<"3.6"
1010
setuptools-scm>=6.3.2;python_version>="3.6"
1111
tomli>=1.2.2;python_version>="3.6" and python_version<"3.7"

seleniumbase/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "2.3.2"
2+
__version__ = "2.3.3"

seleniumbase/core/browser_launcher.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,13 +986,19 @@ def get_remote_driver(
986986
selenoid = False
987987
selenoid_options = None
988988
screen_resolution = None
989+
browser_version = None
990+
platform_name = None
989991
for key in desired_caps.keys():
990992
capabilities[key] = desired_caps[key]
991993
if key == "selenoid:options":
992994
selenoid = True
993995
selenoid_options = desired_caps[key]
994996
elif key == "screenResolution":
995997
screen_resolution = desired_caps[key]
998+
elif key == "version" or key == "browserVersion":
999+
browser_version = desired_caps[key]
1000+
elif key == "platform" or key == "platformName":
1001+
platform_name = desired_caps[key]
9961002
if selenium4:
9971003
chrome_options.set_capability("cloud:options", capabilities)
9981004
if selenoid:
@@ -1001,6 +1007,12 @@ def get_remote_driver(
10011007
if screen_resolution:
10021008
scres = screen_resolution
10031009
chrome_options.set_capability("screenResolution", scres)
1010+
if browser_version:
1011+
br_vers = browser_version
1012+
chrome_options.set_capability("browserVersion", br_vers)
1013+
if platform_name:
1014+
plat_name = platform_name
1015+
chrome_options.set_capability("platformName", plat_name)
10041016
return webdriver.Remote(
10051017
command_executor=address,
10061018
options=chrome_options,
@@ -1038,13 +1050,16 @@ def get_remote_driver(
10381050
selenoid = False
10391051
selenoid_options = None
10401052
screen_resolution = None
1053+
platform_name = None
10411054
for key in desired_caps.keys():
10421055
capabilities[key] = desired_caps[key]
10431056
if key == "selenoid:options":
10441057
selenoid = True
10451058
selenoid_options = desired_caps[key]
10461059
elif key == "screenResolution":
10471060
screen_resolution = desired_caps[key]
1061+
elif key == "platform" or key == "platformName":
1062+
platform_name = desired_caps[key]
10481063
if selenium4:
10491064
firefox_options.set_capability("cloud:options", capabilities)
10501065
if selenoid:
@@ -1053,6 +1068,9 @@ def get_remote_driver(
10531068
if screen_resolution:
10541069
scres = screen_resolution
10551070
firefox_options.set_capability("screenResolution", scres)
1071+
if platform_name:
1072+
plat_name = platform_name
1073+
firefox_options.set_capability("platformName", plat_name)
10561074
return webdriver.Remote(
10571075
command_executor=address,
10581076
options=firefox_options,

seleniumbase/utilities/selenium_grid/grid-hub

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ case "$1" in
5252
echo ""
5353
exit 1
5454
else
55-
# START_HUB_CMD="java -Djava.util.logging.config.file=test/logging.properties -jar ${WEBDRIVER_SERVER_JAR} ${WEBDRIVER_HUB_PARAMS}"
5655
START_HUB_CMD="java ${VERBOSITY_STRING}-jar ${WEBDRIVER_SERVER_JAR} ${WEBDRIVER_HUB_PARAMS}"
5756
echo ""
5857
echo $START_HUB_CMD
@@ -71,8 +70,8 @@ case "$1" in
7170
echo "Stopping Selenium-WebDriver Grid Hub..."
7271
if [ -f $WEBDRIVER_HUB_PIDFILE ]; then
7372
PID=$(cat $WEBDRIVER_HUB_PIDFILE)
74-
kill $PID
7573
rm $WEBDRIVER_HUB_PIDFILE
74+
kill $PID
7675
sleep 1
7776
if [[ $(ps -A | egrep "^${PID}") ]]; then
7877
echo "${FAIL_MSG} Tried to kill the Grid Hub with PID ${PID}, but was unsuccessful. You need to kill it with something stronger, like 'kill -9'"

seleniumbase/utilities/selenium_grid/grid-node

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ if [ "$GRID_NODE_VERBOSE_LOGS" == "True" ]; then
4040
fi
4141

4242
WEBDRIVER_SERVER_JAR=${DIR}/selenium-server-standalone.jar
43-
# WEBDRIVER_NODE_PARAMS="-role node -hubHost ${GRID_HUB_SERVER_IP} -hubPort 4444 -host 127.0.0.1 -browser browserName=chrome,maxInstances=5,version=ANY,platform=ANY -browser browserName=firefox,maxInstances=5,version=ANY,platform=ANY -browser browserName=MicrosoftEdge,maxInstances=1,version=ANY,platform=WIN10"
44-
WEBDRIVER_NODE_PARAMS="-role node -hub http://${GRID_HUB_SERVER_IP}:4444/grid/register -browser browserName=chrome,maxInstances=5,version=ANY,seleniumProtocol=WebDriver -browser browserName=firefox,maxInstances=5,version=ANY,seleniumProtocol=WebDriver -browser browserName=MicrosoftEdge,maxInstances=1,version=ANY,platform=WIN10,seleniumProtocol=WebDriver"
43+
WEBDRIVER_NODE_PARAMS="-role node -hub http://${GRID_HUB_SERVER_IP}:4444/grid/register -browser browserName=chrome,maxInstances=5,version=latest,seleniumProtocol=WebDriver -browser browserName=firefox,maxInstances=5,version=latest,seleniumProtocol=WebDriver"
4544
WEBDRIVER_NODE_PIDFILE="/tmp/webdriver_node.pid"
4645

4746
if [ ! -f $WEBDRIVER_SERVER_JAR ]; then
@@ -56,7 +55,6 @@ case "$1" in
5655
echo "${FAIL_MSG} Selenium-WebDriver Grid node already running with PID $(cat $WEBDRIVER_NODE_PIDFILE). Run 'grid-node stop' or 'grid-node restart'."
5756
exit 1
5857
else
59-
# START_NODE_CMD="java -Djava.util.logging.config.file=test/logging.properties -jar ${WEBDRIVER_SERVER_JAR} ${WEBDRIVER_NODE_PARAMS}"
6058
START_NODE_CMD="java ${VERBOSITY_STRING}-jar ${WEBDRIVER_SERVER_JAR} ${WEBDRIVER_NODE_PARAMS}"
6159
echo ""
6260
echo $START_NODE_CMD
@@ -66,17 +64,15 @@ case "$1" in
6664
echo $PID > "${WEBDRIVER_NODE_PIDFILE}"
6765
echo "${SUCCESS_MSG} Selenium-WebDriver Grid node started successfully."
6866
echo ""
69-
echo "Browser Sessions: http://127.0.0.1:5555/wd/hub/static/resource/hub.html"
70-
echo ""
7167
# echo "To see full log output, remove the java.util.logging.config.file parameter from script/grid-node"
7268
fi
7369
;;
7470
stop)
7571
echo "Stopping Selenium-WebDriver Grid node..."
7672
if [ -f $WEBDRIVER_NODE_PIDFILE ]; then
7773
PID=$(cat $WEBDRIVER_NODE_PIDFILE)
78-
kill $PID
7974
rm $WEBDRIVER_NODE_PIDFILE
75+
kill $PID
8076
sleep 1
8177
if [[ $(ps -A | egrep "^${PID}") ]]; then
8278
echo "${FAIL_MSG} Tried to kill the node with PID ${PID}, but was unsuccessful. You need to kill it with something stronger, like 'kill -9'"

seleniumbase/utilities/selenium_grid/grid_node.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,16 @@ def main():
115115
else:
116116
if grid_hub_command == "start" or grid_hub_command == "restart":
117117
shell_command = (
118-
"""java -jar %s/selenium-server-standalone.jar -role node """
119-
"""-hub http://%s:4444/grid/register -browser browser"""
120-
"""Name=chrome,maxInstances=5,version=ANY,seleniumProtocol="""
121-
"""WebDriver -browser browserName=firefox,maxInstances=5,"""
122-
"""version=ANY,seleniumProtocol=WebDriver -browser browser"""
123-
"""Name=MicrosoftEdge,maxInstances=1,version=ANY,"""
124-
"""platform=WIN10,seleniumProtocol=WebDriver"""
118+
"""java -jar %s/selenium-server-standalone.jar -role node"""
119+
""" -hub http://%s:4444/grid/register -browser browser"""
120+
"""Name=chrome,maxInstances=5,version=latest,"""
121+
"""seleniumProtocol=WebDriver -browser browserName=firefox,"""
122+
"""maxInstances=5,version=latest,seleniumProtocol=WebDriver"""
125123
% (dir_path, server_ip)
126124
)
127125
print("\nStarting Selenium-WebDriver Grid node...\n")
128126
print(shell_command)
129127
print("")
130-
print(
131-
"""Browser Sessions: http://127.0.0.1:5555"""
132-
"""/wd/hub/static/resource/hub.html"""
133-
)
134-
print("")
135128
subprocess.check_call(shell_command, shell=True)
136129
elif grid_hub_command == "stop":
137130
print("")

0 commit comments

Comments
 (0)