đī¸ How it Works
How SeleniumBase Works đ
đī¸đ At the core, SeleniumBase works by extending pytest 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. Tests are also given access to SeleniumBase command-line options and SeleniumBase methods, which provide additional functionality.
đī¸đ pytest
uses a feature called test discovery to automatically find and run Python methods that start with test_
when those methods are located in Python files that start with test_
or end with _test.py
.
đī¸đ The most common way of using SeleniumBase is by importing BaseCase
:
from seleniumbase import BaseCase
đī¸đ This line activates pytest
when a file is called directly with python
:
BaseCase.main(__name__, __file__)
đī¸đ Classes can inherit BaseCase
to gain SeleniumBase functionality:
class MyTestClass(BaseCase):
đī¸đ Test methods inside BaseCase
classes become SeleniumBase tests: (These tests automatically launch a web browser before starting, and quit the web browser after ending. Default settings can be changed via command-line options.)
def test_abc(self):
đī¸đ SeleniumBase APIs can be called from tests via self
:
self.open("https://example.com")
đī¸đ Here's what a full test might look like:
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)
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")') # Use :contains() on any tag
self.save_screenshot_to_logs() # ("./latest_logs" folder for test)
self.click_link("Sign out") # Link must be "a" tag. Not "button".
self.assert_element('a:contains("Sign in")')
self.assert_exact_text("You have been signed out!", "#top_message")
(See the example, test_mfa_login.py, for reference.)
đī¸đ Here are some examples of running tests with pytest
:
pytest test_mfa_login.py
pytest --headless -n8 --dashboard --html=report.html -v --rs --crumbs
pytest -m marker2
pytest -k agent
pytest offline_examples/
(See Syntax_Formats for more ways of structuring SeleniumBase tests.)
â No More Flaky Tests!¶
SeleniumBase methods automatically wait for page elements to finish loading before interacting with them (up to a timeout limit). This means you no longer need random time.sleep()
statements in your scripts.
There are three layers of protection that provide reliability for tests using SeleniumBase:
-
(1): Selenium's default
pageLoadStrategy
isnormal
: This strategy causes Selenium to wait for the full page to load, with HTML content and sub-resources downloaded and parsed. -
(2): SeleniumBase includes methods such as
wait_for_ready_state_complete()
, which run inside other SeleniumBase methods to ensure that it's safe to proceed with the next command. -
(3): SeleniumBase methods automatically wait for elements to be visible and interactable before interacting with those elements.
If you want to speed up your tests and you think the third level of protection is enough by itself, you can use command-line options to remove the first, the second, or both of those first two levels of protection:
-
--pls=none
--> SetpageLoadStrategy
to"none"
: This strategy causes Selenium to return immediately after the initial HTML content is fully received by the browser. -
--sjw
--> Skip JS Waits, such aswait_for_ready_state_complete()
.