Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple Actors. #12

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions screenpy_playwright/abilities/browse_the_web_synchronously.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@
from playwright.sync_api import sync_playwright

if TYPE_CHECKING:
from typing import TypeVar

from playwright.sync_api import Browser, BrowserContext, Page, Playwright

SelfBrowseTheWebSynchronously = TypeVar(
"SelfBrowseTheWebSynchronously", bound="BrowseTheWebSynchronously"
)
from typing_extensions import Self


class BrowseTheWebSynchronously:
Expand All @@ -38,50 +33,47 @@ class BrowseTheWebSynchronously:

@classmethod
def using(
cls: type[SelfBrowseTheWebSynchronously],
cls,
playwright: Playwright,
browser: Browser | BrowserContext,
) -> SelfBrowseTheWebSynchronously:
) -> Self:
"""Supply a pre-defined Playwright browser to use."""
cls.playwright = playwright
return cls(browser)

@classmethod
def using_firefox(
cls: type[SelfBrowseTheWebSynchronously],
) -> SelfBrowseTheWebSynchronously:
cls,
) -> Self:
"""Use a synchronous Firefox browser."""
if cls.playwright is None:
cls.playwright = sync_playwright().start()
return cls(cls.playwright.firefox.launch())

@classmethod
def using_chromium(
cls: type[SelfBrowseTheWebSynchronously],
) -> SelfBrowseTheWebSynchronously:
cls,
) -> Self:
"""Use a synchronous Chromium (i.e. Chrome, Edge, Opera, etc.) browser."""
if cls.playwright is None:
cls.playwright = sync_playwright().start()
return cls(cls.playwright.chromium.launch())

@classmethod
def using_webkit(
cls: type[SelfBrowseTheWebSynchronously],
cls,
) -> BrowseTheWebSynchronously:
"""Use a synchronous WebKit (i.e. Safari, etc.) browser."""
if cls.playwright is None:
cls.playwright = sync_playwright().start()
return cls(cls.playwright.webkit.launch())

def forget(self: SelfBrowseTheWebSynchronously) -> None:
def forget(self: Self) -> None:
"""Forget everything you knew about being a playwright."""
self.browser.close()
if self.playwright:
self.playwright.stop()
self.__class__.playwright = None

def __init__(
self: SelfBrowseTheWebSynchronously,
self,
browser: Browser | BrowserContext,
) -> None:
self.browser = browser
Expand Down
9 changes: 9 additions & 0 deletions tests/test_abilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,12 @@ def test_can_have_separate_instance_attribute(self) -> None:
assert mock_playwright.chromium.launch.call_count == 1
assert mock_playwright.firefox.launch.call_count == 1
assert mock_playwright.webkit.launch.call_count == 1

def test_forget_does_not_clear_playwright(self) -> None:
mock_playwright, mock_browser = get_mocked_playwright_and_browser()
btws = BrowseTheWebSynchronously.using(mock_playwright, mock_browser)

btws.forget()

assert BrowseTheWebSynchronously.playwright is mock_playwright
assert mock_browser.close.call_count == 1