|
2 | 2 |
|
3 | 3 | ## <img src="https://cdn2.hubspot.net/hubfs/100006/images/super_square_logo_3a.png" title="SeleniumBase" height="32"> Customizing test runs
|
4 | 4 |
|
5 |
| -In addition to [settings.py](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/config/settings.py) (which lets you customize SeleniumBase global properties) you can customize test runs from the command line with pytest (or nosetests): |
6 |
| - |
7 |
| -* Choose the browser for tests to use (Default: Chrome) |
8 |
| -* Choose betweeen pytest & nose unittest runners |
9 |
| -* Choose whether to enter Debug Mode on failures |
10 |
| -* Choose additional variables to pass into tests |
11 |
| -* Choose the User-Agent for the browser to use |
12 |
| -* Choose the automation speed (with Demo Mode) |
13 |
| -* Choose whether to run tests multi-threaded |
14 |
| -* Choose whether to rerun failing tests |
15 |
| -* Choose whether to reuse the browser session |
16 |
| -* Choose a Chrome Extension to load |
17 |
| -* Choose a Chrome User Data Directory to use |
18 |
| -* Choose a BrowserStack server to run on |
19 |
| -* Choose a Sauce Labs server to run on |
20 |
| -* Choose a TestingBot server to run on |
21 |
| -* Choose a CrossBrowserTesting server |
22 |
| -* Choose a Selenium Grid to connect to |
23 |
| -* Choose a database to save results to |
24 |
| -* Choose a proxy server to connect to |
25 |
| - |
26 |
| -...and more! |
27 |
| - |
28 |
| -#### **Examples:** |
29 |
| - |
30 |
| -These are run from the **[examples](https://github.com/seleniumbase/SeleniumBase/tree/master/examples)** folder. |
31 |
| -(Chrome is the default browser if not specified.) |
| 5 | +In addition to [settings.py](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/config/settings.py) for customizing global properties, you can customize test runs [from the command-line](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/plugins/pytest_plugin.py). |
| 6 | + |
| 7 | +The following tests can be run from the [examples/](https://github.com/seleniumbase/SeleniumBase/tree/master/examples) folder: |
32 | 8 |
|
33 | 9 | ```bash
|
| 10 | +# Run my_first_test.py in Chrome (default browser) |
34 | 11 | pytest my_first_test.py
|
35 | 12 |
|
| 13 | +# Run my_first_test.py in Firefox |
| 14 | +pytest my_first_test.py --browser=firefox |
| 15 | + |
| 16 | +# Run tests in Demo Mode to see assertions |
36 | 17 | pytest my_first_test.py --demo
|
37 | 18 |
|
38 |
| -pytest my_first_test.py --browser=firefox |
| 19 | +# Run tests in Headless Mode (invisible browser) |
| 20 | +pytest test_suite.py --headless |
| 21 | + |
| 22 | +# Run tests multi-threaded using [n] threads |
| 23 | +pytest test_suite.py -n=4 |
39 | 24 |
|
| 25 | +# Create a pytest html report after tests are done |
40 | 26 | pytest test_suite.py --html=report.html
|
41 | 27 |
|
42 |
| -nosetests test_suite.py --report --show-report |
| 28 | +# Enter Debug Mode on failures |
| 29 | +pytest test_fail.py --pdb -s |
| 30 | + |
| 31 | +# Rerun failing tests more times |
| 32 | +pytest test_suite.py --reruns=1 |
43 | 33 |
|
44 |
| -pytest test_suite.py --headless -n=4 |
| 34 | +# Pass extra data into tests (retrieve: self.data) |
| 35 | +pytest my_first_test.py --data="ABC,DEF" |
45 | 36 |
|
46 |
| -pytest test_suite.py --reruns=1 --reruns-delay=1 |
| 37 | +# Run tests on a local Selenium Grid |
| 38 | +pytest test_suite.py --server=127.0.0.1 |
47 | 39 |
|
| 40 | +# Run tests on a remote Selenium Grid |
48 | 41 | pytest test_suite.py --server=IP_ADDRESS --port=4444
|
49 | 42 |
|
50 |
| -pytest test_suite.py --reuse-session |
| 43 | +# Run tests on a remote Selenium Grid with authentication |
| 44 | +pytest test_suite.py --server=USERNAME:KEY@IP_ADDRESS --port=80 |
51 | 45 |
|
52 |
| -pytest test_fail.py --pdb -s |
| 46 | +# Reuse the same browser session for all tests being run |
| 47 | +pytest test_suite.py --reuse-session |
53 | 48 |
|
| 49 | +# Run tests through a proxy server |
54 | 50 | pytest proxy_test.py --proxy=IP_ADDRESS:PORT
|
55 | 51 |
|
| 52 | +# Run tests through a proxy server with authentication |
56 | 53 | pytest proxy_test.py --proxy=USERNAME:PASSWORD@IP_ADDRESS:PORT
|
57 | 54 |
|
| 55 | +# Run tests while setting the web browser's User Agent |
58 | 56 | pytest user_agent_test.py --agent="USER-AGENT-STRING"
|
59 | 57 |
|
| 58 | +# Run tests using Chrome's mobile device emulator (default settings) |
| 59 | +pytest test_swag_labs.py --mobile |
| 60 | + |
| 61 | +# Run mobile tests specifying CSS Width, CSS Height, and Pixel-Ratio |
| 62 | +pytest test_swag_labs.py --mobile --metrics="411,731,3" |
| 63 | + |
| 64 | +# Run tests while changing SeleniumBase default settings |
60 | 65 | pytest my_first_test.py --settings-file=custom_settings.py
|
61 | 66 | ```
|
62 | 67 |
|
63 |
| -You can interchange **pytest** with **nosetests**, but using pytest is strongly recommended because developers stopped supporting nosetests. Chrome is the default browser if not specified. |
| 68 | +You can interchange **pytest** with **nosetests** for most things, but using pytest is strongly recommended because developers stopped supporting nosetests. Chrome is the default browser if not specified. |
64 | 69 |
|
65 | 70 | (NOTE: If you're using **pytest** for running tests outside of the SeleniumBase repo, **you'll want a copy of [pytest.ini](https://github.com/seleniumbase/SeleniumBase/blob/master/pytest.ini) at the base of the new folder structure**. If using **nosetests**, the same applies for [setup.cfg](https://github.com/seleniumbase/SeleniumBase/blob/master/setup.cfg).)
|
66 | 71 |
|
67 |
| -An easy way to override seleniumbase/config/settings.py is by using a custom settings file. |
| 72 | +An easy way to override [seleniumbase/config/settings.py](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/config/settings.py) is by using a custom settings file. |
68 | 73 | Here's the command-line option to add to tests: (See [examples/custom_settings.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/custom_settings.py))
|
69 | 74 | ``--settings-file=custom_settings.py``
|
70 | 75 | (Settings include default timeout values, a two-factor auth key, DB credentials, S3 credentials, and other important settings used by tests.)
|
@@ -194,6 +199,8 @@ SeleniumBase provides additional Pytest command-line options for tests:
|
194 | 199 | --port=PORT # (The port that's used by the test server.)
|
195 | 200 | --proxy=SERVER:PORT # (This is the proxy server:port combo used by tests.)
|
196 | 201 | --agent=STRING # (This designates the web browser's User Agent to use.)
|
| 202 | +--mobile # (The option to use the mobile emulator while running tests.) |
| 203 | +--metrics=STRING # ("CSSWidth,Height,PixelRatio" for mobile emulator tests.) |
197 | 204 | --extension-zip=ZIP # (Load a Chrome Extension .zip file, comma-separated.)
|
198 | 205 | --extension-dir=DIR # (Load a Chrome Extension directory, comma-separated.)
|
199 | 206 | --headless # (The option to run tests headlessly. The default on Linux OS.)
|
@@ -246,3 +253,18 @@ If you wish to change the User-Agent for your browser tests (Chrome and Firefox
|
246 | 253 | ```bash
|
247 | 254 | pytest user_agent_test.py --agent="Mozilla/5.0 (Nintendo 3DS; U; ; en) Version/1.7412.EU"
|
248 | 255 | ```
|
| 256 | + |
| 257 | +#### **Mobile Device Testing:** |
| 258 | + |
| 259 | +Use ``--mobile`` to quickly run your tests using Chrome's mobile device emulator with default values for device metrics (CSS Width, CSS Height, Pixel-Ratio) and a default value set for the user agent. To configure the mobile device metrics, use ``--metrics="CSS_Width,CSS_Height,Pixel_Ratio"`` to set those values. You'll also be able to set the user agent with ``--agent="USER-AGENT-STRING"`` (a default user agent will be used if not specified). To find real values for device metrics, [see this GitHub Gist](https://gist.github.com/sidferreira/3f5fad525e99b395d8bd882ee0fd9d00). For a list of available user agent strings, [check out this page](https://developers.whatismybrowser.com/useragents/explore/). |
| 260 | + |
| 261 | +```bash |
| 262 | +# Run tests using Chrome's mobile device emulator (default settings) |
| 263 | +pytest test_swag_labs.py --mobile |
| 264 | + |
| 265 | +# Run mobile tests specifying CSS Width, CSS Height, and Pixel-Ratio |
| 266 | +pytest test_swag_labs.py --mobile --metrics="411,731,3" |
| 267 | + |
| 268 | +# Run mobile tests specifying the user agent |
| 269 | +pytest test_swag_labs.py --mobile --agent="Mozilla/5.0 (Linux; Android 9; Pixel 3 XL)" |
| 270 | +``` |
0 commit comments