You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* A [Python virtual env](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/) is recommended. <i><ahref="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/virtualenv_instructions.md">See shortcut</a>.</i>
55
55
* Upgrade **[pip](https://pypi.org/project/pip/)** to prevent warnings:
56
+
56
57
```bash
57
58
python -m pip install -U pip
58
59
```
@@ -128,6 +129,7 @@ class MyTestClass(BaseCase):
128
129
* By default, **[CSS Selectors](https://www.w3schools.com/cssref/css_selectors.asp)** are used for finding page elements.
129
130
* If you're new to CSS Selectors, games like [Flukeout](http://flukeout.github.io/) can help you learn.
130
131
* Here are some common ``SeleniumBase`` methods you might find in tests:
132
+
131
133
```python
132
134
self.open(URL) # Navigate to the web page
133
135
self.click(SELECTOR) # Click a page element
@@ -150,6 +152,7 @@ self.switch_to_default_content() # Switch webdriver control out of the current
150
152
self.switch_to_window(WINDOW_NUMBER) # Switch to a different window/tab
151
153
self.save_screenshot(FILE_NAME) # Save a screenshot of the current page
152
154
```
155
+
153
156
For the complete list of SeleniumBase methods, see: <b><ahref="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/method_summary.md">Method Summary</a></b>
@@ -159,30 +162,37 @@ SeleniumBase automatically handles common WebDriver actions such as spinning up
159
162
160
163
<h4>Simplified code:</h4>
161
164
SeleniumBase uses simple syntax for commands, such as:
165
+
162
166
```python
163
167
self.update_text("input", "dogs\n")
164
168
```
169
+
165
170
The same command with regular WebDriver is very messy:
166
171
(<i>And it doesn't include SeleniumBase smart-waiting.</i>)
172
+
167
173
```python
168
174
from selenium.webdriver.common.by import By
169
175
element =self.driver.find_element(by=By.CSS_SELECTOR, value="input")
170
176
element.clear()
171
177
element.send_keys("dogs")
172
178
element.submit()
173
179
```
180
+
174
181
As you can see, the old WebDriver way is very bad!
175
182
Use SeleniumBase to make testing much easier!
176
183
(<i>You can still use ``self.driver`` in your code.</i>)
177
184
178
185
<h4>Run tests with ``pytest`` or ``nosetests`` in any browser:</h4>
179
186
(<i>Using **pytest** is recommended. **Chrome** is the default browser.</i>)
187
+
180
188
```bash
181
189
pytest my_first_test.py --browser=chrome
182
190
183
191
nosetests test_suite.py --browser=firefox
184
192
```
193
+
185
194
All Python methods that start with ``test_`` will automatically be run when using ``pytest`` or ``nosetests`` on a Python file, (<i>or on folders containing Python files</i>). You can also be more specific on what to run within a file by using the following: (<i>Note that the syntax is different for pytest vs nosetests.</i>)
Next, choose between **pytest** and **nosetests** test runners. (<i>Mostly interchangeable.</i>)
229
+
218
230
```bash
219
231
cd examples/
220
232
221
233
pytest my_first_test.py --browser=chrome
222
234
223
235
nosetests my_first_test.py --browser=firefox
224
236
```
237
+
225
238
(<i>If no browser is specified, Chrome is used by default.</i>)
226
239
With Pytest, a green dot means a test passed. An "F" means a test failed.
227
240
228
241
<aid="seleniumbase_demo_mode"></a> **Use Demo Mode to help you see what tests are asserting.**
229
242
230
243
If the example test is moving too fast for your eyes, you can run it in **Demo Mode** by adding ``--demo`` on the command-line, which pauses the browser briefly between actions, highlights page elements being acted on, and lets you know what test assertions are happening in real time:
244
+
231
245
```bash
232
246
pytest my_first_test.py --demo
233
247
```
@@ -237,6 +251,7 @@ Python filenames that start with ``test_`` or end with ``_test.py``.
237
251
Python methods that start with ``test_``.
238
252
The Python class name can be anything since SeleniumBase's ``BaseCase`` class inherits from the ``unittest.TestCase`` class.
239
253
You can see which tests are getting discovered by ``pytest`` by using:
254
+
240
255
```bash
241
256
pytest --collect-only -q
242
257
```
@@ -249,13 +264,16 @@ import ipdb; ipdb.set_trace() # Enter debugging mode. n = next, c = continue, s
249
264
import pytest; pytest.set_trace() # Enter debugging mode. n = next, c = continue, s = step.
250
265
```
251
266
252
-
**To pause an active test that throws an exception or error, add ``--pdb -s``:**
267
+
<b>To pause an active test that throws an exception or error, add ``--pdb -s``:</b>
268
+
253
269
```bash
254
270
pytest my_first_test.py --pdb -s
255
271
```
272
+
256
273
The code above will leave your browser window open in case there's a failure. (ipdb commands: 'n', 'c', 's' => next, continue, step).
257
274
258
275
Here are some useful command-line options that come with Pytest:
276
+
259
277
```bash
260
278
-v # Prints the full test name for each test.
261
279
-q # Prints fewer details in the console output when running tests.
@@ -270,6 +288,7 @@ Here are some useful command-line options that come with Pytest:
270
288
```
271
289
272
290
SeleniumBase provides additional Pytest command-line options for tests:
291
+
273
292
```bash
274
293
--browser=BROWSER # (The web browser to use.)
275
294
--cap-file=FILE # (The web browser's desired capabilities to use.)
--visual-baseline # (Set the visual baseline for Visual/Layout tests.)
316
335
--timeout-multiplier=MULTIPLIER # (Multiplies the default timeout values.)
317
336
```
337
+
318
338
(For more details, see the full list of command-line options **[here](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/plugins/pytest_plugin.py)**.)
319
339
320
340
During test failures, logs and screenshots from the most recent test run will get saved to the ``latest_logs/`` folder. Those logs will get moved to ``archived_logs/`` if you add --archive_logs to command-line options, or have ARCHIVE_EXISTING_LOGS set to True in [settings.py](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/config/settings.py), otherwise log files with be cleaned up at the start of the next test run. The ``test_suite.py`` collection contains tests that fail on purpose so that you can see how logging works.
341
+
321
342
```bash
322
343
cd examples/
323
344
@@ -340,6 +361,7 @@ Now inside your tests, you can use ``self.data`` to access that.
340
361
For running tests outside of the SeleniumBase repo with **Pytest**, you'll want a copy of **[pytest.ini](https://github.com/seleniumbase/SeleniumBase/blob/master/pytest.ini)** on the root folder. For running tests outside of the SeleniumBase repo with **Nosetests**, you'll want a copy of **[setup.cfg](https://github.com/seleniumbase/SeleniumBase/blob/master/setup.cfg)** on the root folder. (Subfolders should include a blank ``__init__.py`` file.) These files specify default configuration details for tests. (For nosetest runs, you can also specify a .cfg file by using ``--config``. Example ``nosetests [MY_TEST].py --config=[MY_CONFIG].cfg``)
341
362
342
363
As a shortcut, you'll be able to run ``seleniumbase mkdir [DIRECTORY_NAME]`` to create a new folder that already contains necessary files and some example tests that you can run. Example:
364
+
343
365
```bash
344
366
seleniumbase mkdir ui_tests
345
367
cd ui_tests/
@@ -350,6 +372,7 @@ pytest my_first_test.py
350
372
<h3><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Logging / Results from Failing Tests:</h3>
351
373
352
374
Let's try an example of a test that fails:
375
+
353
376
```python
354
377
""" test_fail.py """
355
378
from seleniumbase import BaseCase
@@ -362,6 +385,7 @@ class MyTestClass(BaseCase):
362
385
```
363
386
364
387
You can run it from the ``examples`` folder like this:
388
+
365
389
```bash
366
390
pytest test_fail.py
367
391
```
@@ -377,23 +401,27 @@ You'll notice that a logs folder, "latest_logs", was created to hold information
377
401
<h4><b>Pytest Reports:</b></h4>
378
402
379
403
Using ``--html=report.html`` gives you a fancy report of the name specified after your test suite completes.
(NOTE: You can add ``--show-report`` to immediately display Nosetest reports after the test suite completes. Only use ``--show-report`` when running tests locally because it pauses the test run.)
<h3><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Using a Proxy Server:</h3>
403
431
404
432
If you wish to use a proxy server for your browser tests (Chrome and Firefox only), you can add ``--proxy=IP_ADDRESS:PORT`` as an argument on the command-line.
433
+
405
434
```bash
406
435
pytest proxy_test.py --proxy=IP_ADDRESS:PORT
407
436
```
408
437
409
438
If the proxy server that you wish to use requires authentication, you can do the following (Chrome only):
To make things easier, you can add your frequently-used proxies to PROXY_LIST in [proxy_list.py](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/config/proxy_list.py), and then use ``--proxy=KEY_FROM_PROXY_LIST`` to use the IP_ADDRESS:PORT of that key.
<h3><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Changing the User-Agent:</h3>
421
452
422
453
If you wish to change the User-Agent for your browser tests (Chrome and Firefox only), you can add ``--agent="USER AGENT STRING"`` as an argument on the command-line.
@@ -446,6 +478,7 @@ Here are some things you can do to setup a production environment for your testi
446
478
* If you're using AWS, you can setup an [Amazon S3](https://aws.amazon.com/s3/) account for saving log files and screenshots from your tests. To activate this feature, modify [settings.py](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/config/settings.py) with connection details in the S3 section, and add "``--with-s3-logging``" on the command-line when running your tests.
447
479
448
480
Here's an example of running tests with additional features enabled:
@@ -470,6 +503,7 @@ self.get_page_source() # This method returns the current page source.
470
503
```
471
504
472
505
<b>ProTip™:</b> You may need to use the get_page_source() method along with Python's find() command to parse through the source to find something that Selenium wouldn't be able to. (You may want to brush up on your Python programming skills for that.)
self.update_text(selector, text) # updates the text from the specified element with the specified value. An exception is raised if the element is missing or if the text field is not editable. Example:
527
+
492
528
```python
493
529
self.update_text("input#id_value", "2012")
494
530
```
495
-
496
531
You can also use self.add_text() or the WebDriver .send_keys() command, but those won't clear the text box first if there's already text inside.
497
532
If you want to type in special keys, that's easy too. Here's an example:
533
+
498
534
```python
499
535
from selenium.webdriver.common.keys import Keys
500
536
self.find_element("textarea").send_keys(Keys.SPACE+ Keys.BACK_SPACE+'\n') # The backspace should cancel out the space, leaving you with the newline
(NOTE: The short versions of this are ``self.find_element(ELEMENT)`` and ``self.assert_element(ELEMENT)``. The find_element() version returns the element)
528
564
529
565
Since the line above returns the element, you can combine that with .click() as shown below:
@@ -538,6 +575,7 @@ self.click("a.my_class") # DO IT THIS WAY!
538
575
**ProTip™:** You can use dots to signify class names (Ex: ``div.class_name``) as a simplified version of ``div[class="class_name"]`` within a CSS selector.
539
576
540
577
You can also use ``*=`` to search for any partial value in a CSS selector as shown below:
578
+
541
579
```python
542
580
self.click('a[name*="partial_name"]')
543
581
```
@@ -574,6 +612,7 @@ else:
574
612
current_url =self.get_current_url()
575
613
self.contact_the_nsa(url=current_url, message="Dark Zone Found") # Not a real SeleniumBase method
What if your test opens up a new tab/window and now you have more than one page? No problem. You need to specify which one you currently want Selenium to use. Switching between tabs/windows is easy:
651
+
612
652
```python
613
653
self.switch_to_window(1) # This switches to the new tab (0 is the first one)
614
654
```
615
655
616
656
**ProTip™:** iFrames follow the same principle as new windows - you need to specify the iFrame if you want to take action on something in there
It's OK if you want to use jQuery on a page that doesn't have it loaded yet. To do so, run the following command first:
688
+
645
689
```python
646
690
self.activate_jquery()
647
691
```
648
692
649
693
Some websites have a restrictive [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) to prevent users from loading jQuery and other external libraries onto their websites. If you need to use jQuery or another JS library on such a website, add ``--disable_csp`` on the command-line.
650
694
651
695
Here are some examples of using jQuery in your scripts:
696
+
652
697
```python
653
698
self.execute_script('jQuery, window.scrollTo(0, 600)') # Scrolling the page
@@ -683,6 +729,7 @@ self.click("a.analytics") # Clicks the generated button
683
729
<h4>Using delayed asserts:</h4>
684
730
685
731
Let's say you want to verify multiple different elements on a web page in a single test, but you don't want the test to fail until you verified several elements at once so that you don't have to rerun the test to find more missing elements on the same page. That's where delayed asserts come in. Here's the example:
732
+
686
733
```python
687
734
from seleniumbase import BaseCase
688
735
@@ -706,6 +753,7 @@ To flush out all the failed delayed asserts into a single exception, make sure t
706
753
<h4>Accessing raw WebDriver</h4>
707
754
708
755
If you need access to any commands that come with standard WebDriver, you can call them directly like this:
You can use ``--reruns NUM`` to retry failing tests that many times. Use ``--reruns-delay SECONDS`` to wait that many seconds between retries. Example:
0 commit comments