A complete web automation framework for end-to-end testing.
Project description
SeleniumBase
Create reliable end-to-end browser tests with Python
๐ Start |
๐ฐ Features |
๐ Examples |
๐๏ธ Options |
๐ Scripts |
๐ฑ Phone
๐ APIs |
๐ก Formats |
๐ Dashboard |
๐ด Recorder |
๐พ Locales |
๐ Grid
๐๏ธ GUI |
๐ฐ TestPage |
๐๏ธ CasePlans |
๐ต๏ธ Inspector |
๐งฌ Hybrid |
๐ป Farm
๐๏ธ How |
๐ Migrate |
โป๏ธ Templates |
๐ NodeGUI |
๐ถ Charts |
๐ Tours
๐ค CI/CD |
๐น๏ธ JSMgr |
๐ Translator |
๐๏ธ Presenter |
๐ Boxes |
๐ผ๏ธ Visual
Example: test_coffee_cart.py from ./examples/
cd examples/
pytest test_coffee_cart.py --demo
(--demo mode slows down tests and highlights actions)
- Here's a preview of that test:
from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)
class CoffeeCartTest(BaseCase):
def test_coffee_cart(self):
self.open("https://seleniumbase.io/coffee/")
self.assert_title("Coffee Cart")
self.click('div[data-sb="Cappuccino"]')
self.click('div[data-sb="Flat-White"]')
self.click('div[data-sb="Cafe-Latte"]')
self.click('a[aria-label="Cart page"]')
self.assert_exact_text("Total: $53.00", "button.pay")
self.click("button.pay")
self.type("input#name", "Selenium Coffee")
self.type("input#email", "test@test.test")
self.click("button#submit-payment")
self.assert_text("Thanks for your purchase.", "#app .success")
โถ๏ธ How is SeleniumBase different from raw Selenium? (click to expand)
๐ก SeleniumBase is a Python framework for browser automation and testing. SeleniumBase uses Selenium/WebDriver APIs, and incorporates test-runners such as pytest, nosetests, and behave to provide organized structure, test discovery, test execution, test state (eg. passed, failed, or skipped), and command-line options for changing default settings (such as choosing the browser to use). With raw Selenium, you would need to set up your own options-parser for configuring tests from the command-line.
๐ก With raw Selenium, commands that use selectors need to specify the type of selector (eg. "css selector", "button#myButton"). With SeleniumBase, there's auto-detection between CSS Selectors and XPath, which means you don't need to specify the type of selector in your commands (but optionally you could).
๐ก SeleniumBase methods often perform multiple actions in a single method call. For example, self.type(selector,text) does the following:
1. Waits for the element to be visible.
2. Waits for the element to be interactive.
3. Clears the text field.
4. Types in the new text.
5. Presses Enter/Submit if the text ends in "\n".
With raw Selenium, those actions require multiple method calls.
๐ก SeleniumBase uses default timeout values when not set:
โ
self.click("button")
With raw Selenium, methods would fail instantly (by default) if an element needed more time to load:
โself.driver.find_element(by="css selector", value="button").click()
(Reliable code is better than unreliable code.)
๐ก SeleniumBase lets you change the explicit timeout values of methods:
โ
self.click("button",timeout=10)
With raw Selenium, that requires more code:
โWebDriverWait(driver,10).until(EC.element_to_be_clickable("css selector", "button")).click()
(Simple code is better than complex code.)
๐ก SeleniumBase gives you clean error output when a test fails. With raw Selenium, error messages can get very messy.
๐ก SeleniumBase gives you the option to generate a dashboard and reports for tests. It also saves screenshots from failing tests to the ./latest_logs/ folder. Raw Selenium does not have these options out-of-the-box.
๐ก SeleniumBase includes desktop GUI apps for running tests, such as SeleniumBase Commander for pytest, and SeleniumBase Behave GUI.
๐ก SeleniumBase has its own Recorder & Test Generator that can create tests from manual browser actions. SeleniumBase also has many other useful tools and console scripts for getting things done quickly. (See the documentation for more details!)
โถ๏ธ Learn about different ways of writing tests (click to expand)
๐๐ An example test with the BaseCase class. Runs with pytest or nosetests. (Learn more)
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_exact_text("Welcome!", "h1")
self.assert_element("img#image1")
self.click('a:contains("This Page")')
self.save_screenshot_to_logs()
๐๐ An example test with the sb pytest fixture. Runs with pytest.
def test_mfa_login(sb):
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_exact_text("Welcome!", "h1")
sb.assert_element("img#image1")
sb.click('a:contains("This Page")')
sb.save_screenshot_to_logs