Skip to content

Commit

Permalink
Merge pull request #12 from ScreenPyHQ/support-multiple-actors
Browse files Browse the repository at this point in the history
Support multiple Actors.
  • Loading branch information
perrygoy authored May 15, 2024
2 parents 04d7c50 + 0d6ac46 commit a59f20c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
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

0 comments on commit a59f20c

Please sign in to comment.