|
| 1 | +### <img src="https://seleniumbase.io/img/sb_icon.png" title="SeleniumBase" height="30" /> Using Desired Capabilities |
| 2 | + |
| 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). |
| 4 | + |
| 5 | +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.) |
| 6 | + |
| 7 | +```bash |
| 8 | +pytest my_first_test.py --browser=remote --server=USERNAME:KEY@hub.browserstack.com --port=80 --cap_file=capabilities/sample_cap_file_BS.py |
| 9 | +``` |
| 10 | + |
| 11 | +```bash |
| 12 | +pytest my_first_test.py --browser=remote --server=USERNAME:KEY@ondemand.saucelabs.com --port=80 --cap_file=capabilities/sample_cap_file_SL.py |
| 13 | +``` |
| 14 | + |
| 15 | +(Parameters: ``--browser=remote``, ``--server=SERVER``, ``--port=PORT``, and ``--cap_file=CAP_FILE.py``) |
| 16 | + |
| 17 | +Here's an example desired capabilities file for BrowserStack: |
| 18 | +```python |
| 19 | +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' |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +Here's an example desired capabilities file for Sauce Labs: |
| 30 | +```python |
| 31 | +capabilities = { |
| 32 | + 'browserName': 'firefox', |
| 33 | + 'browserVersion': '70.0', |
| 34 | + 'platformName': 'macOS 10.13', |
| 35 | + 'sauce:options': { |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +(You'll notice that the browser is now being specified in the capabilities file, rather than with ``--browser=BROWSER``) |
| 41 | + |
| 42 | +You can generate desired capabilities for [BrowserStack](https://www.browserstack.com/automate/capabilities), [Sauce Labs](https://wiki.saucelabs.com/display/DOCS/Platform+Configurator#/), and [TestingBot](https://testingbot.com/support/other/test-options) by following those links to their respective websites. |
| 43 | + |
| 44 | +A regex parser was built into SeleniumBase to capture all lines from the specified desired capabilities file in the following formats: |
| 45 | +``'KEY': 'VALUE'`` |
| 46 | +``'KEY': True`` |
| 47 | +``'KEY': False`` |
| 48 | +``caps['KEY'] = "VALUE"`` |
| 49 | +``caps['KEY'] = True`` |
| 50 | +``caps['KEY'] = False`` |
| 51 | +(Each pair must be on a separate line. You can interchange single and double quotes.) |
| 52 | + |
| 53 | +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. |
| 54 | + |
| 55 | +You'll need default SeleniumBase capabilities for: |
| 56 | +* Using a proxy server (not the same as a Selenium Grid server) |
| 57 | +* Downloading files to a desired folder |
| 58 | +* Disabling some warnings on Chrome |
| 59 | +* Overriding a website's Content Security Policy on Chrome |
| 60 | +* Other possible reasons |
| 61 | + |
| 62 | +You can also set browser desired capabilities from a command line string: |
| 63 | +Example: |
| 64 | +```bash |
| 65 | +pytest test_swag_labs.py --cap-string='{"browserName":"chrome","name":"test1"}' --server="127.0.0.1" --browser=remote |
| 66 | +``` |
| 67 | +(Enclose cap-string in single quotes. Enclose parameter keys in double quotes.) |
| 68 | + |
| 69 | +If you pass ``"*"`` into the ``"name"`` field of ``--cap-string``, the name will become the test identifier. Example: |
| 70 | +```bash |
| 71 | +pytest test_swag_labs.py --cap-string='{"browserName":"chrome","name":"*"}' --server="127.0.0.1" --browser=chrome |
| 72 | +``` |
| 73 | +Example name: ``"my_first_test.MyTestClass.test_basic"`` |
| 74 | + |
| 75 | +### Using a local Selenium Grid |
| 76 | + |
| 77 | +If using a local Selenium Grid with SeleniumBase, start up the Grid Hub and nodes first: |
| 78 | +```bash |
| 79 | +seleniumbase grid-hub start |
| 80 | +seleniumbase grid-node start |
| 81 | +``` |
| 82 | +(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.) |
0 commit comments