Skip to content

👁ī¸ How it Works

How SeleniumBase Works 👁ī¸

👁ī¸đŸ”Ž The primary SeleniumBase syntax format 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 primary SeleniumBase syntax format starts by importing BaseCase:

from seleniumbase import BaseCase

👁ī¸đŸ”Ž This next line activates pytest when a file is called directly with python by accident:

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.)

class MyTestClass(BaseCase):
    def test_abc(self):
        # ...

👁ī¸đŸ”Ž SeleniumBase APIs can be called from tests via self:

class MyTestClass(BaseCase):
    def test_abc(self):
        self.open("https://example.com")

👁ī¸đŸ”Ž Here's what a full test might look like:

from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)

class TestSimpleLogin(BaseCase):
    def test_simple_login(self):
        self.open("https://seleniumbase.io/simple/login")
        self.type("#username", "demo_user")
        self.type("#password", "secret_pass")
        self.click('a:contains("Sign in")')
        self.assert_exact_text("Welcome!", "h1")
        self.assert_element("img#image1")
        self.highlight("#image1")
        self.click_link("Sign out")
        self.assert_text("signed out", "#top_message")

(See the example, test_simple_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/

👁ī¸đŸ”Ž Here's a SeleniumBase syntax format that uses the raw driver. Unlike the format mentioned earlier, it can be run with python instead of pytest. The driver includes original driver methods and new ones added by SeleniumBase:

from seleniumbase import Driver

driver = Driver()
try:
    driver.get("https://seleniumbase.io/simple/login")
    driver.type("#username", "demo_user")
    driver.type("#password", "secret_pass")
    driver.click('a:contains("Sign in")')
    driver.assert_exact_text("Welcome!", "h1")
    driver.assert_element("img#image1")
    driver.highlight("#image1")
    driver.click_link("Sign out")
    driver.assert_text("signed out", "#top_message")
finally:
    driver.quit()

(See the example, raw_login_driver.py, for reference.)

👁ī¸đŸ”Ž Note that regular SeleniumBase formats (ones that use BaseCase, the SB context manager, or the sb pytest fixture) have more methods than the improved driver format. The regular formats also have more features. Some features, (such as the SeleniumBase dashboard), require a pytest format.


✅ 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.

NO MORE FLAKY TESTS!

There are three layers of protection that provide reliability for tests using SeleniumBase:

  • (1): Selenium's default pageLoadStrategy is normal: 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 --> Set pageLoadStrategy 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 as wait_for_ready_state_complete().


SeleniumBase