-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,9 @@ Open | |
|
||
.. autoclass:: Open | ||
:members: | ||
|
||
SaveScreenshot | ||
-------------- | ||
|
||
.. autoclass:: SaveScreenshot | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
"""Save a screenshot of the current page.""" | ||
|
||
from __future__ import annotations | ||
|
||
import os | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING, Any | ||
|
||
from screenpy import AttachTheFile, UnableToAct, beat | ||
|
||
from ..abilities import BrowseTheWebSynchronously | ||
|
||
if TYPE_CHECKING: | ||
from screenpy import Actor | ||
from typing_extensions import Self | ||
|
||
|
||
class SaveScreenshot: | ||
"""Save a screenshot of the Actor's current page. | ||
Use the :meth:`~screenpy_playwright.actions.save_screenshot.and_attach_it` | ||
method to indicate that this screenshot should be attached to all reports | ||
through the Narrator's adapters. This method also accepts any keyword | ||
arguments those adapters might require. | ||
Abilities Required: | ||
:class:`~screenpy_playwright.abilities.BrowseTheWebSynchronously` | ||
Examples:: | ||
the_actor.attempts_to(SaveScreenshot("screenshot.png")) | ||
the_actor.attempts_to(SaveScreenshot.as_(filepath)) | ||
# attach file to the Narrator's reports (behavior depends on adapter). | ||
the_actor.attempts_to(SaveScreenshot.as_(filepath).and_attach_it()) | ||
# using screenpy_adapter_allure plugin! | ||
from allure_commons.types import AttachmentTypes | ||
the_actor.attempts_to( | ||
SaveScreenshot.as_(filepath).and_attach_it_with( | ||
attachment_type=AttachmentTypes.PNG, | ||
), | ||
) | ||
""" | ||
|
||
attach_kwargs: dict | None | ||
path: str | ||
filename: str | ||
|
||
def describe(self) -> str: | ||
"""Describe the Action in present tense.""" | ||
return f"Save screenshot as {self.filename}" | ||
|
||
@classmethod | ||
def as_(cls, path: str) -> Self: | ||
"""Supply the name and/or filepath for the screenshot. | ||
If only a name is supplied, the screenshot will appear in the current | ||
working directory. | ||
Args: | ||
path: The filepath for the screenshot, including its name. | ||
Returns: | ||
Self | ||
""" | ||
return cls(path=path) | ||
|
||
def and_attach_it(self, **kwargs: Any) -> Self: # noqa: ANN401 | ||
"""Indicate the screenshot should be attached to any reports. | ||
This method accepts any additional keywords needed by any adapters | ||
attached for :external+screenpy:ref:`Narration`. | ||
Args: | ||
**kwargs: keyword arguments for the adapters used by the narrator. | ||
Returns: | ||
Self | ||
""" | ||
self.attach_kwargs = kwargs | ||
return self | ||
|
||
and_attach_it_with = and_attach_it | ||
|
||
@beat("{} saves a screenshot as {filename}") | ||
def perform_as(self, the_actor: Actor) -> None: | ||
"""Direct the actor to save a screenshot. | ||
Args: | ||
the_actor: The actor who will perform this Action. | ||
""" | ||
current_page = the_actor.ability_to(BrowseTheWebSynchronously).current_page | ||
if current_page is None: | ||
msg = "No page has been opened! Cannot save a screenshot." | ||
raise UnableToAct(msg) | ||
|
||
screenshot = current_page.screenshot(path=self.path) | ||
Path(self.path).write_bytes(screenshot) | ||
|
||
if self.attach_kwargs is not None: | ||
the_actor.attempts_to(AttachTheFile(self.path, **self.attach_kwargs)) | ||
|
||
def __init__(self, path: str) -> None: | ||
self.path = path | ||
self.filename = path.split(os.path.sep)[-1] | ||
self.attach_kwargs = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters