pythonpython-3.xselenium-webdriverpytestseleniumbase

Using Seleniumbase BaseCase class without using pytest


I want to use the seleniumbase BaseCase class in my Python 3 script, but I don't want to use pytest to run it. Actually, I want to use methods that aren't available on Seleniumbase Driver itself (like choose_file or ...)

but I got:

It looks like you are trying to call a SeleniumBase method" from outside the scope of your test class's `self` object," which is initialized by calling BaseCase's setUp() method." The `self` object is where all test variables are defined." If you created a custom setUp() method (that overrided the" the default one), make sure to call super().setUp() in it." When using page objects, be sure to pass the `self` object" from your test class into your page object methods so that" they can call BaseCase class methods with all the required" variables, which are initialized during the setUp() method" that runs automatically before all tests called by pytest."

Is there a way for a class to inherit from the BaseCase class and be called directly with Python in other functions or classes?

I tried @Michael Mintz simple example like this:

with SB(headless=False) as sb:
        sb.open("https://seleniumbase.io/simple/login")
        sb.type("#username", "demo_user")
        sb.type("#password", "secret_pass")
        sb.click('a:contains("Sign in")')
        sb.assert_exact_text("Welcome!", "h1")
        sb.assert_element("img#image1")
        sb.highlight("#image1")
        sb.click_link("Sign out")
        sb.assert_text("signed out", "#top_message")

but I got :

Selenium Manager binary found at: ~/mysimplecode/.venv/lib/python3.10/site-packages/selenium/webdriver/common/linux/selenium-manager
Executing process: ~/mysimplecode/.venv/lib/python3.10/site-packages/selenium/webdriver/common/linux/selenium-manager --browser chrome --debug --output json
Found chromedriver 121.0.6167.85 in PATH: ~/mysimplecode/.venv/lib/python3.10/site-packages/seleniumbase/drivers/chromedriver
chrome detected at /usr/bin/google-chrome
Running command: /usr/bin/google-chrome --version
Output: "Google Chrome 121.0.6167.85 "
Detected browser: chrome 121.0.6167.85
Required driver: chromedriver 121.0.6167.85
Driver path: ~/mysimplecode/.venv/lib/python3.10/site-packages/seleniumbase/drivers/chromedriver
Browser path: /usr/bin/google-chrome
Using driver at: ~/mysimplecode/.venv/lib/python3.10/site-packages/seleniumbase/drivers/chromedriver
Started executable: `~/mysimplecode/.venv/lib/python3.10/site-packages/seleniumbase/drivers/chromedriver` in a child process with pid: 14671 using 0 to output -3

It seems something went wrong here

I tested the code on Windows 10; it's working there, but on my PC, it failed to run. I'm using Ubuntu 22.04, python 3.10 and chrome 121.0.6167.85


Solution

  • Yes, you can run SeleniumBase scripts without pytest by using the SB() format, which includes the SeleniumBase BaseCase methods.

    Here's a simple example:

    from seleniumbase import SB
    
    with SB() as sb:
        sb.open("https://seleniumbase.io/simple/login")
        sb.type("#username", "demo_user")
        sb.type("#password", "secret_pass")
        sb.click('a:contains("Sign in")')
        sb.assert_exact_text("Welcome!", "h1")
        sb.assert_element("img#image1")
        sb.highlight("#image1")
        sb.click_link("Sign out")
        sb.assert_text("signed out", "#top_message")
    

    Here's another example:

    from seleniumbase import SB
    
    with SB() as sb:  # By default, browser="chrome" if not set.
        sb.open("https://seleniumbase.github.io/realworld/login")
        sb.type("#username", "demo_user")
        sb.type("#password", "secret_pass")
        sb.enter_mfa_code("#totpcode", "GAXG2MTEOR3DMMDG")  # 6-digit
        sb.assert_text("Welcome!", "h1")
        sb.highlight("img#image1")  # A fancier assert_element() call
        sb.click('a:contains("This Page")')  # Use :contains() on any tag
        sb.click_link("Sign out")  # Link must be "a" tag. Not "button".
        sb.assert_element('a:contains("Sign in")')
        sb.assert_exact_text("You have been signed out!", "#top_message")
    

    See SeleniumBase Syntax Formats for details on all ways of structuring tests.