SeleniumBase/help_docs/how_it_works.md

46 lines
2.3 KiB
Markdown
Raw Normal View History

2022-08-06 06:27:38 +08:00
## [<img src="https://seleniumbase.io/img/logo6.png" title="SeleniumBase" width="32">](https://github.com/seleniumbase/SeleniumBase/) How SeleniumBase Works:
2019-11-11 12:58:02 +08:00
2017-10-31 09:59:24 +08:00
<a id="how_seleniumbase_works"></a>
2021-03-27 05:52:11 +08:00
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 (using Selenium WebDriver), 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/help_docs/customizing_test_runs.md) and [SeleniumBase methods](https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/method_summary.md), which provide additional functionality.
2017-10-31 09:59:24 +08:00
2020-05-16 01:15:37 +08:00
(NOTE: pytest uses a feature called test discovery to automatically find and run Python methods that start with "``test_``" from the file that you specified on the command line.)
2017-10-31 09:59:24 +08:00
2021-03-27 05:52:11 +08:00
The most common way of using SeleniumBase is by inheriting BaseCase:
2020-08-25 04:47:56 +08:00
2017-10-31 09:59:24 +08:00
```python
from seleniumbase import BaseCase
```
2020-08-25 04:47:56 +08:00
2021-03-27 05:52:11 +08:00
Then have your test classes inherit BaseCase:
2020-08-25 04:47:56 +08:00
2017-10-31 09:59:24 +08:00
```python
class MyTestClass(BaseCase):
```
2020-08-25 04:47:56 +08:00
2022-08-06 06:27:38 +08:00
Here's what a full test might look like:
```python
from seleniumbase import BaseCase
class TestMFALogin(BaseCase):
def test_mfa_login(self):
self.open("https://seleniumbase.io/realworld/login")
self.type("#username", "demo_user")
self.type("#password", "secret_pass")
self.enter_mfa_code("#totpcode", "GAXG2MTEOR3DMMDG") # 6-digit
self.assert_text("Welcome!", "h1")
self.highlight("img#image1") # A fancier assert_element() call
self.click('a:contains("This Page")')
self.save_screenshot_to_logs() # In "./latest_logs/" folder.
self.click_link("Sign out") # Must be "a" tag. Not "button".
self.assert_element('a:contains("Sign in")')
self.assert_exact_text("You have been signed out!", "#top_message")
```
2020-08-25 04:47:56 +08:00
(See the example test, [my_first_test.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/my_first_test.py), for reference.)
2021-03-27 05:52:11 +08:00
--------
2021-04-03 10:48:53 +08:00
For more ways of using SeleniumBase, see: <a href="https://seleniumbase.io/help_docs/syntax_formats/">SyntaxFormats</a>.