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
* Add ``--upgrade`` OR ``-U`` to upgrade an installation.
70
70
* Add ``--force-reinstall`` to also upgrade dependencies.
71
71
72
-
### <imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Download a webdriver:
72
+
<h3><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Download a webdriver:</h3>
73
73
74
74
SeleniumBase can download a webdriver to the [seleniumbase/drivers](https://github.com/seleniumbase/SeleniumBase/tree/master/seleniumbase/drivers) folder with the ``install`` command:
### <imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Run a test on Chrome:
84
+
<h3><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Run a test on Chrome:</h3>
85
85
```bash
86
86
cd examples/
87
87
pytest my_first_test.py
@@ -328,7 +328,7 @@ To pass additional data from the command-line to tests, add ``--data="ANY STRING
328
328
Now inside your tests, you can use ``self.data`` to access that.
329
329
330
330
331
-
### <imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Test Directory Customization:
331
+
<h3><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Test Directory Customization:</h3>
332
332
333
333
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``)
334
334
@@ -340,7 +340,7 @@ pytest my_first_test.py
340
340
```
341
341
342
342
343
-
### <imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Logging / Results from Failing Tests:
343
+
<h3><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Logging / Results from Failing Tests:</h3>
344
344
345
345
Let's try an example of a test that fails:
346
346
```python
@@ -363,11 +363,11 @@ You'll notice that a logs folder, "latest_logs", was created to hold information
363
363
364
364
365
365
<aid="creating_visual_reports"></a>
366
-
### <imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Creating Visual Test Suite Reports:
366
+
<h3><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Creating Visual Test Suite Reports:</h3>
367
367
368
368
(NOTE: Several command-line args are different for Pytest vs Nosetests)
369
369
370
-
#### **Pytest Reports:**
370
+
<h4><b>Pytest Reports:</b></h4>
371
371
372
372
Using ``--html=report.html`` gives you a fancy report of the name specified after your test suite completes.
373
373
```bash
@@ -381,7 +381,7 @@ You can also use ``--junit-xml=report.xml`` to get an xml report instead. Jenkin
381
381
pytest test_suite.py --junit-xml=report.xml
382
382
```
383
383
384
-
#### **Nosetest Reports:**
384
+
<h4><b>Nosetest Reports:</b></h4>
385
385
386
386
The ``--report`` option gives you a fancy report 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.)
393
393
394
394
395
-
### <imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Using a Proxy Server:
395
+
<h3><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Using a Proxy Server:</h3>
396
396
397
397
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.
### <imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Changing the User-Agent:
413
+
<h3><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Changing the User-Agent:</h3>
414
414
415
415
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.
### <imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Building Guided Tours for Websites:
421
+
<h3><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Building Guided Tours for Websites:</h3>
422
422
423
423
Learn about <ahref="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/tour_examples/ReadMe.md">SeleniumBase Interactive Walkthroughs</a> (in the ``examples/tour_examples`` folder). It's great for prototyping a website onboarding experience.
424
424
425
425
426
426
<aid="utilizing_advanced_features"></a>
427
-
### <imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Production Environments & Integrations:
427
+
<h3><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Production Environments & Integrations:</h3>
428
428
429
429
Here are some things you can do to setup a production environment for your testing:
**ProTip™:** In most web browsers, you can right-click on a page and select ``Inspect Element`` to see the CSS selector details that you'll need to create your own scripts.
481
481
482
-
#### Typing Text
482
+
<h4>Typing Text</h4>
483
483
484
484
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:
485
485
```python
@@ -493,23 +493,27 @@ from selenium.webdriver.common.keys import Keys
493
493
self.find_element("textarea").send_keys(Keys.SPACE+ Keys.BACK_SPACE+'\n') # The backspace should cancel out the space, leaving you with the newline
494
494
```
495
495
496
-
#### Getting the text from an element on a page
496
+
<h4>Getting the text from an element on a page</h4>
497
+
497
498
```python
498
499
text =self.get_text("header h2")
499
500
```
500
501
501
-
#### Getting the attribute value from an element on a page
502
+
<h4>Getting the attribute value from an element on a page</h4>
(NOTE: ``self.find_text(TEXT, ELEMENT)`` and ``self.wait_for_text(TEXT, ELEMENT)`` also do this. For backwords compatibility, older method names were kept, but the default timeout may be different.)
540
545
541
-
#### Asserting Anything
546
+
<h4>Asserting Anything</h4>
547
+
542
548
```python
543
549
self.assert_true(myvar1 == something)
544
550
545
551
self.assert_equal(var1, var2)
546
552
```
547
553
548
-
#### Useful Conditional Statements (with creative examples in action)
554
+
<h4>Useful Conditional Statements (with creative examples in action)</h4>
549
555
550
556
is_element_visible(selector) # is an element visible on a page
raiseException("Reports of my assimilation are greatly exaggerated.")
594
600
```
595
601
596
-
#### Switching Tabs
602
+
<h4>Switching Tabs</h4>
597
603
598
604
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:
If you're not sure whether there's an alert before trying to accept or dismiss it, one way to handle that is to wrap your alert-handling code in a try/except block. Other methods such as .text and .send_keys() will also work with alerts.
621
627
622
-
#### Executing Custom jQuery Scripts:
628
+
<h4>Executing Custom jQuery Scripts:</h4>
623
629
624
630
jQuery is a powerful JavaScript library that allows you to perform advanced actions in a web browser.
625
631
If the web page you're on already has jQuery loaded, you can start executing jQuery scripts immediately.
@@ -667,7 +673,7 @@ self.click("a.analytics") # Clicks the generated button
667
673
```
668
674
(Due to popular demand, this traffic generation example has been baked into SeleniumBase with the ``self.generate_referral(start_page, end_page)`` and the ``self.generate_traffic(start_page, end_page, loops)`` methods.)
669
675
670
-
#### Using delayed asserts:
676
+
<h4>Using delayed asserts:</h4>
671
677
672
678
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:
673
679
```python
@@ -690,7 +696,7 @@ class MyTestClass(BaseCase):
690
696
``delayed_assert_element()`` and ``delayed_assert_text()`` will save any exceptions that would be raised.
691
697
To flush out all the failed delayed asserts into a single exception, make sure to call ``self.process_delayed_asserts()`` at the end of your test method. If your test hits multiple pages, you can call ``self.process_delayed_asserts()`` at the end of all your delayed asserts for a single page. This way, the screenshot from your log file will have the location where the delayed asserts were made.
692
698
693
-
#### Accessing raw WebDriver
699
+
<h4>Accessing raw WebDriver</h4>
694
700
695
701
If you need access to any commands that come with standard WebDriver, you can call them directly like this:
(In general, you'll want to use the SeleniumBase versions of methods when available.)
702
708
703
-
#### Retrying failing tests automatically
709
+
<h4>Retrying failing tests automatically</h4>
704
710
705
711
You can use ``--reruns NUM`` to retry failing tests that many times. Use ``--reruns-delay SECONDS`` to wait that many seconds between retries. Example:
Additionally, you can use the ``@retry_on_exception()`` decorator to specifically retry failing methods. (First import: ``from seleniumbase import decorators``) To learn more about SeleniumBase decorators, [click here](https://github.com/seleniumbase/SeleniumBase/tree/master/seleniumbase/common).
## <imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Running Example Tests:
3
+
<h2><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Running Example Tests:</h2>
4
4
5
5
SeleniumBase tests are run with **``pytest``**. Chrome is the default browser if not specifed. During test failures, logs and screenshots from the latest run are saved to the ``latest_logs/`` folder.
You can use SeleniumBase to selectively translate the method names of any test from one language to another via the console scripts interface. Additionally, the ``import`` line at the top of the Python file will change to import the new ``BaseCase``. Example: ``BaseCase`` becomes ``CasoDeTeste`` when a test is translated into Portuguese.
## <imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Customizing test runs
3
+
<h2><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Customizing test runs</h2>
4
4
5
5
You can customize test runs from the command-line thanks to [SeleniumBase's pytest plugin](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/plugins/pytest_plugin.py), which adds command-line options for setting/enabling the browser type, headless mode, mobile mode, multithreading mode, demo mode, proxy config, user agent config, browser extensions, and more.
* A complete test automation framework for web/mobile UI testing.
9
9
* Uses [pytest](https://docs.pytest.org/en/latest/), [unittest](https://docs.python.org/3/library/unittest.html), and [nose](http://nose.readthedocs.io/en/latest/) for test discovery and execution.
10
10
* No more flaky tests! (Smart-waiting code keeps tests reliable.)
## <imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> **How it works:**
4
+
<h2><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> How it works:</h2>
5
5
6
6
At the core, SeleniumBase works by extending [pytest](https://docs.pytest.org/en/latest/) as a direct plugin. SeleniumBase automatically spins up web browsers for tests, and then gives those tests access to the SeleniumBase libraries through the [BaseCase class](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/fixtures/base_case.py). Tests are also given access to [SeleniumBase command-line arguments](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/plugins/pytest_plugin.py) and [SeleniumBase methods](https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/method_summary.md), which provide additional functionality.
## <imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Mobile Testing
2
+
<h2><imgsrc="https://seleniumbase.io/img/sb_icon.png"title="SeleniumBase"width="30" /> Mobile Testing</h2>
3
3
4
4
Use ``--mobile`` to run your SeleniumBase tests using Chrome's (or Edge's) mobile device emulator with default values for device metrics (CSS Width, CSS Height, Pixel-Ratio) and user agent.
0 commit comments