SeleniumBase/help_docs/desired_capabilities.md

103 lines
4.2 KiB
Markdown
Raw Normal View History

2022-11-05 13:27:22 +08:00
<!-- SeleniumBase Docs -->
2022-11-27 04:43:30 +08:00
## [<img src="https://seleniumbase.github.io/img/logo6.png" title="SeleniumBase" width="32">](https://github.com/seleniumbase/SeleniumBase/) Using Desired Capabilities
2020-05-18 09:50:11 +08:00
2023-08-15 01:29:23 +08:00
You can specify browser capabilities when running SeleniumBase tests on a remote Selenium Grid server such as <a href="https://www.browserstack.com/automate/capabilities" target="_blank">BrowserStack</a> or <a href="https://wiki.saucelabs.com/display/DOCS/Platform+Configurator#/" target="_blank">Sauce Labs</a>.
2020-05-18 09:50:11 +08:00
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.)
```bash
2021-12-25 06:36:27 +08:00
pytest test_demo_site.py --browser=remote --server=USERNAME:KEY@hub.browserstack.com --port=80 --cap_file=capabilities/sample_cap_file_BS.py
2020-05-18 09:50:11 +08:00
```
2022-03-30 03:46:17 +08:00
2020-05-18 09:50:11 +08:00
```bash
2021-12-25 06:36:27 +08:00
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
2020-05-18 09:50:11 +08:00
```
(Parameters: ``--browser=remote``, ``--server=SERVER``, ``--port=PORT``, and ``--cap_file=CAP_FILE.py``)
Here's an example desired capabilities file for BrowserStack:
2021-12-25 06:36:27 +08:00
2020-05-18 09:50:11 +08:00
```python
desired_cap = {
2022-06-03 02:29:21 +08:00
"os" : "Windows",
"os_version" : "11",
"browser" : "Chrome",
"browser_version" : "101.0",
"browserstack.local" : "false",
"browserstack.debug" : "true",
"browserstack.selenium_version" : "4.1.2",
2020-05-18 09:50:11 +08:00
}
```
2022-03-30 03:46:17 +08:00
2020-05-18 09:50:11 +08:00
Here's an example desired capabilities file for Sauce Labs:
2021-12-25 06:36:27 +08:00
2020-05-18 09:50:11 +08:00
```python
capabilities = {
2021-12-25 06:36:27 +08:00
"browserName": "chrome",
"browserVersion": "latest",
"platformName": "macOS 10.14",
"sauce:options": {},
2020-05-18 09:50:11 +08:00
}
```
2023-10-27 15:31:51 +08:00
(Note that the browser is now being specified in the capabilities file, rather than with ``--BROWSER`` when using a **remote** Selenium Grid. If using a **local** Selenium Grid, specify the browser, eg: ``--firefox``.)
2021-12-25 06:36:27 +08:00
<div><b>You can generate specific desired capabilities using:</b></div>
2020-05-18 09:50:11 +08:00
2020-05-18 10:53:06 +08:00
<ul>
2022-06-03 02:29:21 +08:00
<li><a href="https://www.browserstack.com/automate/capabilities" target="_blank">BrowserStack desired capabilities</a></li>
<li><a href="https://wiki.saucelabs.com/display/DOCS/Platform+Configurator#/" target="_blank">Sauce Labs desired capabilities</a></li>
2020-05-18 10:53:06 +08:00
</ul>
2020-05-18 09:50:11 +08:00
2021-12-25 06:36:27 +08:00
<div><b>Parsing desired capabilities:</b></div>
2020-05-18 10:53:06 +08:00
SeleniumBase has a desired capabilities parser that can capture all lines from the specified file in the following formats:
2021-12-25 06:36:27 +08:00
```python
'KEY': 'VALUE'
'KEY': True
'KEY': False
caps['KEY'] = "VALUE"
caps['KEY'] = True
caps['KEY'] = False
```
2020-05-18 09:50:11 +08:00
(Each pair must be on a separate line. You can interchange single and double quotes.)
2023-10-27 15:31:51 +08:00
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.
2020-05-18 09:50:11 +08:00
You'll need default SeleniumBase capabilities for:
* Using a proxy server (not the same as a Selenium Grid server)
* Downloading files to a desired folder
* Disabling some warnings on Chrome
* Overriding a website's Content Security Policy on Chrome
* Other possible reasons
2023-10-27 15:31:51 +08:00
You can also set browser desired capabilities from a command-line string. Eg:
2021-12-25 06:36:27 +08:00
2020-05-18 09:50:11 +08:00
```bash
pytest test_swag_labs.py --cap-string='{"browserName":"chrome","name":"test1"}' --server="127.0.0.1" --browser=remote
```
2021-12-25 06:36:27 +08:00
2020-05-18 09:50:11 +08:00
(Enclose cap-string in single quotes. Enclose parameter keys in double quotes.)
If you pass ``"*"`` into the ``"name"`` field of ``--cap-string``, the name will become the test identifier. Example:
2021-12-25 06:36:27 +08:00
2020-05-18 09:50:11 +08:00
```bash
2021-12-25 06:36:27 +08:00
pytest my_first_test.py --cap-string='{"browserName":"chrome","name":"*"}' --server="127.0.0.1" --browser=chrome
2020-05-18 09:50:11 +08:00
```
2021-12-25 06:36:27 +08:00
2021-01-26 03:40:13 +08:00
Example name: ``"my_first_test.MyTestClass.test_basics"``
2020-05-18 09:50:11 +08:00
2021-12-25 06:36:27 +08:00
<h3>Using a local Selenium Grid</h3>
2020-05-18 09:50:11 +08:00
If using a local Selenium Grid with SeleniumBase, start up the Grid Hub and nodes first:
2021-12-25 06:36:27 +08:00
2020-05-18 09:50:11 +08:00
```bash
2021-12-25 06:36:27 +08:00
sbase grid-hub start
sbase grid-node start
2020-05-18 09:50:11 +08:00
```
2021-12-25 06:36:27 +08:00
2020-05-18 09:50:11 +08:00
(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.)