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.67k stars 929 forks source link

Refresh the instant Page Object Generator with newer APIs #1721

Closed mdmintz closed 1 year ago

mdmintz commented 1 year ago

Refresh the instant Page Object Generator with newer APIs

In case anyone was not aware how this tool works, if you run:

sbase objectify basic_test.py

on this file:

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

class MyTestClass(BaseCase):
    def test_basics(self):
        self.open("https://www.saucedemo.com")
        self.type("#user-name", "standard_user")
        self.type("#password", "secret_sauce\n")
        self.assert_element("div.inventory_list")
        self.assert_exact_text("PRODUCTS", "span.title")
        self.click('button[name*="backpack"]')
        self.click("#shopping_cart_container a")
        self.assert_exact_text("YOUR CART", "span.title")
        self.assert_text("Backpack", "div.cart_item")
        self.click('button:contains("Remove")')  # HTML innerText
        self.assert_text_not_visible("Backpack", "div.cart_item")
        self.js_click("a#logout_sidebar_link")
        self.assert_element("div#login_button_container")

Then page_objects.py is created:

# PAGE OBJECTS FILE >>> (autogenerated)

class Page(object):
    html = "html"
    css_1 = "#user-name"
    css_2 = "#password"
    css_3 = "div.inventory_list"
    css_4 = "span.title"
    css_5 = 'button[name*="backpack"]'
    css_6 = "#shopping_cart_container a"
    css_7 = "div.cart_item"
    css_8 = 'button:contains("Remove")'
    css_9 = "a#logout_sidebar_link"
    css_10 = "div#login_button_container"

And you get a file like this that uses the page objects:

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

class MyTestClass(BaseCase):
    def test_basics(self):
        self.open("https://www.saucedemo.com")
        self.type(Page.css_1, "standard_user")
        self.type(Page.css_2, "secret_sauce\n")
        self.assert_element(Page.css_3)
        self.assert_exact_text("PRODUCTS", Page.css_4)
        self.click(Page.css_5)
        self.click(Page.css_6)
        self.assert_exact_text("YOUR CART", Page.css_4)
        self.assert_text("Backpack", Page.css_7)
        self.click(Page.css_8)  # HTML innerText
        self.assert_text_not_visible("Backpack", "div.cart_item")
        self.js_click(Page.css_9)
        self.assert_element(Page.css_10)

To revert changes to that file, you can do this:

sbase revert-objects basic_test.py
mdmintz commented 1 year ago

For more detailed information on that functionality:


extract-objects

sbase extract-objects [SB_FILE.py]

Creates page objects based on selectors found in a seleniumbase Python file and saves those objects to the "page_objects.py" file in the same folder as the tests.

inject-objects

sbase inject-objects [SB_FILE.py] [OPTIONS]

-c, --comments (Add object selectors to the comments.)

Takes the page objects found in the "page_objects.py" file and uses those to replace matching selectors in the selected seleniumbase Python file.

objectify

sbase objectify [SB_FILE.py] [OPTIONS]

-c, --comments (Add object selectors to the comments.)

A modified version of the file where the selectors have been replaced with variable names defined in "page_objects.py", supporting the Page Object Pattern. (This has the same outcome as combining extract-objects with inject-objects)

revert-objects

sbase revert-objects [SB_FILE.py] [OPTIONS]

-c, --comments (Keep existing comments for the lines.)

Reverts the changes made by seleniumbase objectify ... or seleniumbase inject-objects ... when run against a seleniumbase Python file. Objects will get replaced by selectors stored in the "page_objects.py" file.

mdmintz commented 1 year ago

This was released in 4.12.4 - https://github.com/seleniumbase/SeleniumBase/releases/tag/v4.12.4 - (https://github.com/seleniumbase/SeleniumBase/pull/1731)