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

Add API to capture screenshots of windows. #2062

Closed
freakboy3742 opened this issue Aug 4, 2023 · 2 comments · Fixed by #2103
Closed

Add API to capture screenshots of windows. #2062

freakboy3742 opened this issue Aug 4, 2023 · 2 comments · Fixed by #2103
Labels
enhancement New features, or improvements to existing features.

Comments

@freakboy3742
Copy link
Member

What is the problem or limitation you are having?

As an app developer, it is sometimes necessary to capture the current state of a GUI window as an image.

The most notable use case is in CI testing, where the screen isn't visible, and test failures are most obvious in their visual presentation; however, there may be some other uses.

Describe the solution you'd like

  1. A new cross-platform Window.as_image() -> toga.Image API.

  2. As an example of the API in action, modify the testbed test harness so that when a widget test fails, a screenshot of the screen is captured and written to the cache directory.

Describe alternatives you've considered

Do nothing. Screenshots aren't essential; they're a nice-to-have feature.

Additional context

  • The Canvas widget already has an as_image() API, demonstrating how to save a single widget as an image. The Window screenshot API should be almost identical, but applied to the root content widget.
  • The Canvas GUI tests already have tests that write the as_image() representation to disk if the canvas hasn't rendered correctly.
  • It should be possible to catch a failed test in the teardown of the widget_probe() fixture of testbed; if an AssertionError is raised during the yield, that will be a test failure; if this occurs, take a screenshot of the window/screen, write to the app cache folder, and re-raise the assertion (so that it is picked up by Pytest).
@freakboy3742 freakboy3742 added enhancement New features, or improvements to existing features. good first issue Is this your first time contributing? This could be a good place to start! labels Aug 4, 2023
@freakboy3742 freakboy3742 removed the good first issue Is this your first time contributing? This could be a good place to start! label Aug 28, 2023
@dr-gavitron
Copy link
Contributor

dr-gavitron commented Oct 14, 2023

Started implementing it. Work done ✅ for windows and Linux. Will submit a PR when completed for all platforms.

Add pillow features in this: as_image will return toga.Image when nothing will be specified. And will return Pillow Object when PIL.Image is specified as format.

@freakboy3742
Copy link
Member Author

@dr-gavitron If you read back to the tickets that are linked to this one, there's already an implementation of this on all platforms as part of #2103.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New features, or improvements to existing features.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants