seleniumbase / SeleniumBase

📊 Python's all-in-one framework for web crawling, scraping, testing, and reporting. Supports pytest. UC Mode provides stealth. Includes many tools.
https://seleniumbase.io
MIT License
4.45k stars 906 forks source link

[Question]: Can SeleniumBase be used without pytest #581

Closed allandsouza closed 4 years ago

allandsouza commented 4 years ago

I have a need to perform some actions on a webpage and scrape some data in a standalone command line python program. Was wondering if SeleniumBase is a good candidate. Feels like it is; except that I don’t know how to use it outside of pytest.

Any thoughts/samples @mdmintz ?

mdmintz commented 4 years ago

@allandsouza For web scraping, here's a good simple example: https://github.com/seleniumbase/SeleniumBase/blob/master/examples/user_agent_test.py

For running without pytest (using pure Python) here's a sample script: https://github.com/seleniumbase/SeleniumBase/blob/master/examples/raw_parameter_script.py That one runs with:

python raw_parameter_script.py

That's essentially the boilerplate to run any test without pytest by first setting all the command line parameters in the script rather than through pytest arg parsing.

allandsouza commented 4 years ago

Perfect … exactly what I was looking for. Thank you!

mdmintz commented 10 months ago

There are more ways now to run with pure python, for example:

"""Context Manager Test. Runs with "python". (pytest not needed)."""
from seleniumbase import SB

with SB() as sb:  # By default, browser="chrome" if not set.
    sb.open("https://seleniumbase.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")

And:

"""Can run with "python". (pytest not needed)."""
from seleniumbase import js_utils
from seleniumbase import page_actions
from seleniumbase import DriverContext

# Driver Context Manager - (By default, browser="chrome". Lots of options)
with DriverContext() as driver:
    driver.get("https://seleniumbase.github.io/")
    js_utils.highlight_with_js(driver, 'img[alt="SeleniumBase"]', loops=6)

with DriverContext(browser="chrome", incognito=True) as driver:
    driver.get("https://seleniumbase.io/apps/calculator")
    page_actions.wait_for_element(driver, '[id="4"]').click()
    page_actions.wait_for_element(driver, '[id="2"]').click()
    page_actions.wait_for_text(driver, "42", "#output")
    js_utils.highlight_with_js(driver, "#output", loops=6)

with DriverContext() as driver:
    driver.get("https://seleniumbase.github.io/demo_page")
    js_utils.highlight_with_js(driver, "h2", loops=5)
    by_css = "css selector"
    driver.find_element(by_css, "#myTextInput").send_keys("Automation")
    driver.find_element(by_css, "#checkBox1").click()
    js_utils.highlight_with_js(driver, "img", loops=5)

See https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md for more.