Skip to content

Playwright

qa_pytest_playwright

__all__ = ['PlaywrightSteps', 'PlaywrightTests', 'PlaywrightUiContext', 'PlaywrightUiElement'] module-attribute

PlaywrightSteps

Bases: UiSteps[PlaywrightSteps[TConfiguration]]

BDD-style step definitions for Playwright-based UI operations.

Attributes:

Name Type Description
_ui_context UiContext[UiElement]

The Playwright UI context used for browser automation.

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_steps.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
class PlaywrightSteps[TConfiguration: UiConfiguration](
    UiSteps[TConfiguration]
):
    """
    BDD-style step definitions for Playwright-based UI operations.

    Type Parameters:
        TConfiguration: The configuration type, must be a UiConfiguration.

    Attributes:
        _ui_context (UiContext[UiElement]): The Playwright UI context used for browser automation.
    """
    pass

PlaywrightTests

Bases: AbstractTestsBase[PlaywrightTests[TSteps], PlaywrightTests[TConfiguration]]

Base class for Playwright-based UI test cases.

This class manages the lifecycle of a Playwright browser and page for each test method. It is generic over the types of steps and configuration used.

Attributes:

Name Type Description
_playwright Playwright

The Playwright instance.

_browser Browser

The Playwright browser instance (not thread safe).

_page Page

The Playwright page instance (not thread safe).

Type Parameters: TSteps: The type of the steps class, typically derived from PlaywrightSteps. TConfiguration: The type of the configuration class, typically derived from UiConfiguration.

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_tests.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class PlaywrightTests[
    TSteps: PlaywrightSteps[Any],
    TConfiguration: UiConfiguration
](AbstractTestsBase[TSteps, TConfiguration]):
    """
    Base class for Playwright-based UI test cases.

    This class manages the lifecycle of a Playwright browser and page for each test method.
    It is generic over the types of steps and configuration used.

    Attributes:
        _playwright (Playwright): The Playwright instance.
        _browser (Browser): The Playwright browser instance (not thread safe).
        _page (Page): The Playwright page instance (not thread safe).
    Type Parameters:
        TSteps: The type of the steps class, typically derived from PlaywrightSteps.
        TConfiguration: The type of the configuration class, typically derived from UiConfiguration.
    """
    _playwright: Playwright  # not thread safe
    _browser: Browser  # not thread safe
    _page: Page  # not thread safe

    @property
    def browser(self) -> Browser:
        """
        Returns the Playwright browser instance.

        Returns:
            Browser: The Playwright browser instance.
        """
        return self._browser

    @property
    def page(self) -> Page:
        """
        Returns the Playwright page instance.

        Returns:
            Page: The Playwright page instance.
        """
        return self._page

    @property
    def ui_context(self) -> UiContext[UiElement]:
        return PlaywrightUiContext(self._page)

    @override
    def setup_method(self):
        """
        Initializes Playwright browser and page before each test method.

        If you need to customize browser options or use a different browser,
        override this method in your test class.
        """
        super().setup_method()

        from playwright.sync_api import sync_playwright
        self._playwright = sync_playwright().start()
        self._browser = self._playwright.chromium.launch(
            args=["--disable-gpu"],
            headless=False)
        self._page = self._browser.new_page(viewport=None)

    @override
    def teardown_method(self):
        """
        Closes the Playwright page, browser, and context after each test method.
        """
        try:
            try:
                self._page.close()
            finally:
                try:
                    self._browser.close()
                finally:
                    self._playwright.stop()
        finally:
            super().teardown_method()

browser property

Returns the Playwright browser instance.

Returns:

Name Type Description
Browser Browser

The Playwright browser instance.

page property

Returns the Playwright page instance.

Returns:

Name Type Description
Page Page

The Playwright page instance.

ui_context property

setup_method()

Initializes Playwright browser and page before each test method.

If you need to customize browser options or use a different browser, override this method in your test class.

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_tests.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
@override
def setup_method(self):
    """
    Initializes Playwright browser and page before each test method.

    If you need to customize browser options or use a different browser,
    override this method in your test class.
    """
    super().setup_method()

    from playwright.sync_api import sync_playwright
    self._playwright = sync_playwright().start()
    self._browser = self._playwright.chromium.launch(
        args=["--disable-gpu"],
        headless=False)
    self._page = self._browser.new_page(viewport=None)

teardown_method()

Closes the Playwright page, browser, and context after each test method.

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_tests.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
@override
def teardown_method(self):
    """
    Closes the Playwright page, browser, and context after each test method.
    """
    try:
        try:
            self._page.close()
        finally:
            try:
                self._browser.close()
            finally:
                self._playwright.stop()
    finally:
        super().teardown_method()

PlaywrightUiContext

Bases: UiContext[PlaywrightUiElement]

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
class PlaywrightUiContext(UiContext[PlaywrightUiElement]):
    _page: Page

    def __init__(self, page: Page) -> None:
        self._page = page

    def find_element(
            self, by: str, value: Optional[str]) -> PlaywrightUiElement:
        selector_str = self._build_playwright_selector(
            Selector(by, value or ""))
        return PlaywrightUiElement(self._page.locator(selector_str).first)

    def find_elements(
            self, by: str, value: Optional[str]) -> Iterator[PlaywrightUiElement]:
        selector_str = self._build_playwright_selector(
            Selector(by, value or ""))
        return (PlaywrightUiElement(e) for e in self._page.locator(selector_str).all())

    def get(self, url: str) -> None:
        self._page.goto(url)

    def execute_script(self, script: str, *args: object) -> Any:
        return self._page.evaluate(script, *args)

    @staticmethod
    def _build_playwright_selector(selector: Selector) -> str:
        """
        Converts a Selector object to a Playwright selector string.
        """
        if selector.by == "id":
            return f"#{selector.value}"
        elif selector.by == "xpath":
            return selector.value
        elif selector.by == "css selector":
            return selector.value
        elif selector.by == "link text":
            return f"text={selector.value}"
        elif selector.by == "partial link text":
            return f"text={selector.value}"
        elif selector.by == "name":
            return f"[name='{selector.value}']"
        elif selector.by == "tag name":
            return selector.value
        elif selector.by == "class name":
            return f".{selector.value}"
        else:
            return selector.value

__init__(page)

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
70
71
def __init__(self, page: Page) -> None:
    self._page = page

execute_script(script, *args)

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
88
89
def execute_script(self, script: str, *args: object) -> Any:
    return self._page.evaluate(script, *args)

find_element(by, value)

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
73
74
75
76
77
def find_element(
        self, by: str, value: Optional[str]) -> PlaywrightUiElement:
    selector_str = self._build_playwright_selector(
        Selector(by, value or ""))
    return PlaywrightUiElement(self._page.locator(selector_str).first)

find_elements(by, value)

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
79
80
81
82
83
def find_elements(
        self, by: str, value: Optional[str]) -> Iterator[PlaywrightUiElement]:
    selector_str = self._build_playwright_selector(
        Selector(by, value or ""))
    return (PlaywrightUiElement(e) for e in self._page.locator(selector_str).all())

get(url)

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
85
86
def get(self, url: str) -> None:
    self._page.goto(url)

PlaywrightUiElement

Bases: UiElement

Playwright adapter for UiElement protocol. Wraps a Playwright Locator and implements the backend-agnostic UiElement interface.

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class PlaywrightUiElement(UiElement):
    """
    Playwright adapter for UiElement protocol.
    Wraps a Playwright Locator and implements the backend-agnostic UiElement interface.
    """

    _locator: PlaywrightLocator

    def __init__(self, locator: PlaywrightLocator) -> None:
        object.__setattr__(self, '_locator', locator)

    def click(self) -> None:
        self._locator.click()

    def type(self, text: str) -> None:
        self._locator.clear()
        self._locator.fill(text)

    def clear(self) -> None:
        self._locator.clear()

    def scroll_into_view(self) -> None:
        self._locator.scroll_into_view_if_needed()

    def execute_script(self, script: str, *args: object) -> object:
        """Execute script on this element using the page's evaluate method."""
        return self._locator.evaluate(script, *args)

    @property
    def text(self) -> str:
        """Get element text content, returning empty string if None."""
        return self._locator.text_content() or ""

    def __getattr__(self, name: str) -> Any:
        """Delegate all other attribute access to the wrapped Playwright Locator."""
        return getattr(self._locator, name)

    def __setattr__(self, name: str, value: Any) -> None:
        """Delegate attribute setting to the wrapped Playwright Locator."""
        if name == '_locator':
            object.__setattr__(self, name, value)
        else:
            setattr(self._locator, name, value)

    def __repr__(self) -> str:
        return repr(self._locator)

    def __str__(self) -> str:
        return str(self._locator)

text property

Get element text content, returning empty string if None.

__getattr__(name)

Delegate all other attribute access to the wrapped Playwright Locator.

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
49
50
51
def __getattr__(self, name: str) -> Any:
    """Delegate all other attribute access to the wrapped Playwright Locator."""
    return getattr(self._locator, name)

__init__(locator)

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
24
25
def __init__(self, locator: PlaywrightLocator) -> None:
    object.__setattr__(self, '_locator', locator)

__repr__()

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
60
61
def __repr__(self) -> str:
    return repr(self._locator)

__setattr__(name, value)

Delegate attribute setting to the wrapped Playwright Locator.

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
53
54
55
56
57
58
def __setattr__(self, name: str, value: Any) -> None:
    """Delegate attribute setting to the wrapped Playwright Locator."""
    if name == '_locator':
        object.__setattr__(self, name, value)
    else:
        setattr(self._locator, name, value)

__str__()

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
63
64
def __str__(self) -> str:
    return str(self._locator)

clear()

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
34
35
def clear(self) -> None:
    self._locator.clear()

click()

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
27
28
def click(self) -> None:
    self._locator.click()

execute_script(script, *args)

Execute script on this element using the page's evaluate method.

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
40
41
42
def execute_script(self, script: str, *args: object) -> object:
    """Execute script on this element using the page's evaluate method."""
    return self._locator.evaluate(script, *args)

scroll_into_view()

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
37
38
def scroll_into_view(self) -> None:
    self._locator.scroll_into_view_if_needed()

type(text)

Source code in qa-pytest-playwright/src/qa_pytest_playwright/playwright_ui_adapter.py
30
31
32
def type(self, text: str) -> None:
    self._locator.clear()
    self._locator.fill(text)