Skip to content

Commit a2aacba

Browse files
committed
Update the docs
1 parent 6fa98b1 commit a2aacba

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

README.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
* [<img src="https://img.shields.io/pypi/pyversions/seleniumbase.svg?color=22AAEE" alt="Python:2.7|3.5|3.6|3.7|3.8" />](https://www.python.org/downloads/)
5454
* A [Python virtual env](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/) is recommended. <i><a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/virtualenv_instructions.md">See shortcut</a>.</i>
5555
* Upgrade **[pip](https://pypi.org/project/pip/)** to prevent warnings:
56+
5657
```bash
5758
python -m pip install -U pip
5859
```
@@ -128,6 +129,7 @@ class MyTestClass(BaseCase):
128129
* By default, **[CSS Selectors](https://www.w3schools.com/cssref/css_selectors.asp)** are used for finding page elements.
129130
* If you're new to CSS Selectors, games like [Flukeout](http://flukeout.github.io/) can help you learn.
130131
* Here are some common ``SeleniumBase`` methods you might find in tests:
132+
131133
```python
132134
self.open(URL) # Navigate to the web page
133135
self.click(SELECTOR) # Click a page element
@@ -150,6 +152,7 @@ self.switch_to_default_content() # Switch webdriver control out of the current
150152
self.switch_to_window(WINDOW_NUMBER) # Switch to a different window/tab
151153
self.save_screenshot(FILE_NAME) # Save a screenshot of the current page
152154
```
155+
153156
For the complete list of SeleniumBase methods, see: <b><a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/method_summary.md">Method Summary</a></b>
154157

155158
<h2><img src="https://seleniumbase.io/img/sb_icon.png" title="SeleniumBase" width="30" /> Learn More:</h2>
@@ -159,30 +162,37 @@ SeleniumBase automatically handles common WebDriver actions such as spinning up
159162

160163
<h4>Simplified code:</h4>
161164
SeleniumBase uses simple syntax for commands, such as:
165+
162166
```python
163167
self.update_text("input", "dogs\n")
164168
```
169+
165170
The same command with regular WebDriver is very messy:
166171
(<i>And it doesn't include SeleniumBase smart-waiting.</i>)
172+
167173
```python
168174
from selenium.webdriver.common.by import By
169175
element = self.driver.find_element(by=By.CSS_SELECTOR, value="input")
170176
element.clear()
171177
element.send_keys("dogs")
172178
element.submit()
173179
```
180+
174181
As you can see, the old WebDriver way is very bad!
175182
Use SeleniumBase to make testing much easier!
176183
(<i>You can still use ``self.driver`` in your code.</i>)
177184

178185
<h4>Run tests with ``pytest`` or ``nosetests`` in any browser:</h4>
179186
(<i>Using **pytest** is recommended. **Chrome** is the default browser.</i>)
187+
180188
```bash
181189
pytest my_first_test.py --browser=chrome
182190

183191
nosetests test_suite.py --browser=firefox
184192
```
193+
185194
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>)
195+
186196
```bash
187197
pytest [FILE_NAME].py::[CLASS_NAME]::[METHOD_NAME]
188198
nosetests [FILE_NAME].py:[CLASS_NAME].[METHOD_NAME]
@@ -206,6 +216,7 @@ For a full list of SeleniumBase features, <a href="https://github.com/seleniumba
206216
<h4><b>Here's how to run the example script on various web browsers:</b></h4>
207217

208218
First install a webdriver for each browser you intend to use:
219+
209220
```bash
210221
seleniumbase install chromedriver
211222
seleniumbase install geckodriver
@@ -215,19 +226,22 @@ seleniumbase install operadriver
215226
```
216227

217228
Next, choose between **pytest** and **nosetests** test runners. (<i>Mostly interchangeable.</i>)
229+
218230
```bash
219231
cd examples/
220232

221233
pytest my_first_test.py --browser=chrome
222234

223235
nosetests my_first_test.py --browser=firefox
224236
```
237+
225238
(<i>If no browser is specified, Chrome is used by default.</i>)
226239
With Pytest, a green dot means a test passed. An "F" means a test failed.
227240

228241
<a id="seleniumbase_demo_mode"></a> **Use Demo Mode to help you see what tests are asserting.**
229242

230243
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+
231245
```bash
232246
pytest my_first_test.py --demo
233247
```
@@ -237,6 +251,7 @@ Python filenames that start with ``test_`` or end with ``_test.py``.
237251
Python methods that start with ``test_``.
238252
The Python class name can be anything since SeleniumBase's ``BaseCase`` class inherits from the ``unittest.TestCase`` class.
239253
You can see which tests are getting discovered by ``pytest`` by using:
254+
240255
```bash
241256
pytest --collect-only -q
242257
```
@@ -249,13 +264,16 @@ import ipdb; ipdb.set_trace() # Enter debugging mode. n = next, c = continue, s
249264
import pytest; pytest.set_trace() # Enter debugging mode. n = next, c = continue, s = step.
250265
```
251266

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+
253269
```bash
254270
pytest my_first_test.py --pdb -s
255271
```
272+
256273
The code above will leave your browser window open in case there's a failure. (ipdb commands: 'n', 'c', 's' => next, continue, step).
257274

258275
Here are some useful command-line options that come with Pytest:
276+
259277
```bash
260278
-v # Prints the full test name for each test.
261279
-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:
270288
```
271289

272290
SeleniumBase provides additional Pytest command-line options for tests:
291+
273292
```bash
274293
--browser=BROWSER # (The web browser to use.)
275294
--cap-file=FILE # (The web browser's desired capabilities to use.)
@@ -315,9 +334,11 @@ SeleniumBase provides additional Pytest command-line options for tests:
315334
--visual-baseline # (Set the visual baseline for Visual/Layout tests.)
316335
--timeout-multiplier=MULTIPLIER # (Multiplies the default timeout values.)
317336
```
337+
318338
(For more details, see the full list of command-line options **[here](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/plugins/pytest_plugin.py)**.)
319339

320340
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+
321342
```bash
322343
cd examples/
323344

@@ -340,6 +361,7 @@ Now inside your tests, you can use ``self.data`` to access that.
340361
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``)
341362

342363
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+
343365
```bash
344366
seleniumbase mkdir ui_tests
345367
cd ui_tests/
@@ -350,6 +372,7 @@ pytest my_first_test.py
350372
<h3><img src="https://seleniumbase.io/img/sb_icon.png" title="SeleniumBase" width="30" /> Logging / Results from Failing Tests:</h3>
351373

352374
Let's try an example of a test that fails:
375+
353376
```python
354377
""" test_fail.py """
355378
from seleniumbase import BaseCase
@@ -362,6 +385,7 @@ class MyTestClass(BaseCase):
362385
```
363386

364387
You can run it from the ``examples`` folder like this:
388+
365389
```bash
366390
pytest test_fail.py
367391
```
@@ -377,23 +401,27 @@ You'll notice that a logs folder, "latest_logs", was created to hold information
377401
<h4><b>Pytest Reports:</b></h4>
378402

379403
Using ``--html=report.html`` gives you a fancy report of the name specified after your test suite completes.
404+
380405
```bash
381406
pytest test_suite.py --html=report.html
382407
```
383408

384409
<img src="https://cdn2.hubspot.net/hubfs/100006/images/pytest_report_3c.png" alt="Example Pytest Report" title="Example Pytest Report" width="520" />
385410

386411
You can also use ``--junit-xml=report.xml`` to get an xml report instead. Jenkins can use this file to display better reporting for your tests.
412+
387413
```bash
388414
pytest test_suite.py --junit-xml=report.xml
389415
```
390416

391417
<h4><b>Nosetest Reports:</b></h4>
392418

393419
The ``--report`` option gives you a fancy report after your test suite completes.
420+
394421
```bash
395422
nosetests test_suite.py --report
396423
```
424+
397425
<img src="https://cdn2.hubspot.net/hubfs/100006/images/Test_Report_2.png" alt="Example Nosetest Report" title="Example Nosetest Report" width="320" />
398426

399427
(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.)
@@ -402,16 +430,19 @@ nosetests test_suite.py --report
402430
<h3><img src="https://seleniumbase.io/img/sb_icon.png" title="SeleniumBase" width="30" /> Using a Proxy Server:</h3>
403431

404432
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+
405434
```bash
406435
pytest proxy_test.py --proxy=IP_ADDRESS:PORT
407436
```
408437

409438
If the proxy server that you wish to use requires authentication, you can do the following (Chrome only):
439+
410440
```bash
411441
pytest proxy_test.py --proxy=USERNAME:PASSWORD@IP_ADDRESS:PORT
412442
```
413443

414444
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.
445+
415446
```bash
416447
pytest proxy_test.py --proxy=proxy1
417448
```
@@ -420,6 +451,7 @@ pytest proxy_test.py --proxy=proxy1
420451
<h3><img src="https://seleniumbase.io/img/sb_icon.png" title="SeleniumBase" width="30" /> Changing the User-Agent:</h3>
421452

422453
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.
454+
423455
```bash
424456
pytest user_agent_test.py --agent="Mozilla/5.0 (Nintendo 3DS; U; ; en) Version/1.7412.EU"
425457
```
@@ -446,6 +478,7 @@ Here are some things you can do to setup a production environment for your testi
446478
* 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.
447479

448480
Here's an example of running tests with additional features enabled:
481+
449482
```bash
450483
pytest [YOUR_TEST_FILE].py --with-db-reporting --with-s3-logging
451484
```
@@ -470,6 +503,7 @@ self.get_page_source() # This method returns the current page source.
470503
```
471504

472505
<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.)
506+
473507
```python
474508
source = self.get_page_source()
475509
head_open_tag = source.find('<head>')
@@ -480,6 +514,7 @@ everything_inside_head = source[head_open_tag+len('<head>'):head_close_tag]
480514
<h4>Clicking</h4>
481515

482516
To click an element on the page:
517+
483518
```python
484519
self.click("div#my_id")
485520
```
@@ -489,12 +524,13 @@ self.click("div#my_id")
489524
<h4>Typing Text</h4>
490525

491526
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+
492528
```python
493529
self.update_text("input#id_value", "2012")
494530
```
495-
496531
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.
497532
If you want to type in special keys, that's easy too. Here's an example:
533+
498534
```python
499535
from selenium.webdriver.common.keys import Keys
500536
self.find_element("textarea").send_keys(Keys.SPACE + Keys.BACK_SPACE + '\n') # The backspace should cancel out the space, leaving you with the newline
@@ -527,6 +563,7 @@ self.wait_for_element_visible("a.my_class", timeout=5)
527563
(NOTE: The short versions of this are ``self.find_element(ELEMENT)`` and ``self.assert_element(ELEMENT)``. The find_element() version returns the element)
528564

529565
Since the line above returns the element, you can combine that with .click() as shown below:
566+
530567
```python
531568
self.find_element("a.my_class", timeout=5).click()
532569

@@ -538,6 +575,7 @@ self.click("a.my_class") # DO IT THIS WAY!
538575
**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.
539576

540577
You can also use ``*=`` to search for any partial value in a CSS selector as shown below:
578+
541579
```python
542580
self.click('a[name*="partial_name"]')
543581
```
@@ -574,6 +612,7 @@ else:
574612
current_url = self.get_current_url()
575613
self.contact_the_nsa(url=current_url, message="Dark Zone Found") # Not a real SeleniumBase method
576614
```
615+
577616
Another example:
578617
```python
579618
def is_there_a_cloaked_klingon_ship_on_this_page():
@@ -609,11 +648,13 @@ def get_mirror_universe_captain_picard_superbowl_ad(superbowl_year):
609648
<h4>Switching Tabs</h4>
610649

611650
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+
612652
```python
613653
self.switch_to_window(1) # This switches to the new tab (0 is the first one)
614654
```
615655

616656
**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
657+
617658
```python
618659
self.switch_to_frame('ContentManagerTextBody_ifr')
619660
# Now you can act inside the iFrame
@@ -624,6 +665,7 @@ self.switch_to_default_content() # Exit the iFrame when you're done
624665
<h4>Handling Pop-Up Alerts</h4>
625666

626667
What if your test makes an alert pop up in your browser? No problem. You need to switch to it and either accept it or dismiss it:
668+
627669
```python
628670
self.wait_for_and_accept_alert()
629671

@@ -637,18 +679,21 @@ If you're not sure whether there's an alert before trying to accept or dismiss i
637679
jQuery is a powerful JavaScript library that allows you to perform advanced actions in a web browser.
638680
If the web page you're on already has jQuery loaded, you can start executing jQuery scripts immediately.
639681
You'd know this because the web page would contain something like the following in the HTML:
682+
640683
```html
641684
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
642685
```
643686

644687
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+
645689
```python
646690
self.activate_jquery()
647691
```
648692

649693
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.
650694

651695
Here are some examples of using jQuery in your scripts:
696+
652697
```python
653698
self.execute_script('jQuery, window.scrollTo(0, 600)') # Scrolling the page
654699

@@ -670,6 +715,7 @@ self.execute_script("return jQuery('textarea')[2].value") # Returns the css "va
670715
```
671716

672717
In the following example, JavaScript is used to plant code on a page that Selenium can then touch after that:
718+
673719
```python
674720
start_page = "https://xkcd.com/465/"
675721
destination_page = "https://github.com/seleniumbase/SeleniumBase"
@@ -683,6 +729,7 @@ self.click("a.analytics") # Clicks the generated button
683729
<h4>Using delayed asserts:</h4>
684730

685731
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+
686733
```python
687734
from seleniumbase import BaseCase
688735

@@ -706,6 +753,7 @@ To flush out all the failed delayed asserts into a single exception, make sure t
706753
<h4>Accessing raw WebDriver</h4>
707754

708755
If you need access to any commands that come with standard WebDriver, you can call them directly like this:
756+
709757
```python
710758
self.driver.delete_all_cookies()
711759
capabilities = self.driver.capabilities
@@ -716,6 +764,7 @@ self.driver.find_elements_by_partial_link_text("GitHub")
716764
<h4>Retrying failing tests automatically</h4>
717765

718766
You can use ``--reruns NUM`` to retry failing tests that many times. Use ``--reruns-delay SECONDS`` to wait that many seconds between retries. Example:
767+
719768
```
720769
pytest --reruns 5 --reruns-delay 1
721770
```

0 commit comments

Comments
 (0)