Skip to content

Save screenshot to tempfile in text mode #133

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

Closed
xmo-odoo opened this issue Sep 13, 2017 · 15 comments · Fixed by #226
Closed

Save screenshot to tempfile in text mode #133

xmo-odoo opened this issue Sep 13, 2017 · 15 comments · Fixed by #226

Comments

@xmo-odoo
Copy link

Screenshots are really valuable tools for debugging selenium test (even more so when running in headless Firefox or Chrome). Currently, pytest-selenium injects screenshots into HTML reports, but maybe it could also save those screenshots to the disk and give the file path in the text summary, possibly optionally? At the moment it generates a (base64) screenshot in every case and just drops it when html is not enabled.

@ramonasuciu
Copy link

I was able to save the screenshots to disk, using Selenium's save_screenshot(file_path).
You can rename the files as you wish and move them to a separate folder. I left them in assets, for convenience purposes, but the idea is that, using save_screenshot(file_path), you are able to keep the screenshots locally. Also, you can inject them still in pytest's html report.

Hope it helps.

@xmo-odoo
Copy link
Author

@ramonasuciu yeah you can take them manually without issues, but it's less convenient than pytest-selenium doing so on only failures, which as far as I can see it already does but it drops the screenshot if not using HTML reports.

@ramonasuciu
Copy link

@xmo-odoo what I meant is that pytest-html still picks the screenshots which are now created as png files, not as base64, and attaches them to the corresponding failing scenario in the html report.
The only difference is that I am now saving the screenshots as files per-say, in any location, and instruct pytest to pick them up from that new location, in order to properly attach them to the html report.

Please let me know if you'd need more details on how I've done this.

Thanks,
Ramona

@BeyondEvil
Copy link
Contributor

@davehunt What if were to, in the case of pytest-html not being enabled, drop the files to disk and put the file path in the summary? Either per default or per the command line/ini-setting.

ping @xmo-odoo

@xmo-odoo
Copy link
Author

xmo-odoo commented Sep 10, 2018

@BeyondEvil that's pretty much my original suggestion: if not generating an HTML report (or possibly even then), save failure screenshots to a temporary file and print the file path.

@BeyondEvil
Copy link
Contributor

@xmo-odoo Yes :)

The problem I can see with dumping the file being the default, is littering the filesystem with screenshots.

@davehunt
Copy link
Contributor

It would be fairly simple for you to do this yourself in a conftest.py by implementing the pytest_runtest_makereport hook, however I do see your point that pytest-selenium already gathers the screenshots, and just discards them if pytest-html is not available. I'd support a pull request that only grabs a screenshot when pytest-html is available, or a pull request that saves the screenshots to a temporary path. Doing the latter will need to have some way to associate the tests with the screenshots, perhaps using the test identifier to create a subdirectory in the temporary location. Rather than list all of these in the summary, I would suggest only mentioning the parent directory. Something like /path/to/temp/screenshots/test_foo/xxx.png

@nicoddemus
Copy link
Member

Doing the latter will need to have some way to associate the tests with the screenshots, perhaps using the test identifier to create a subdirectory in the temporary location.

That's a good solution, but perhaps using the tmpdir fixture to do the heavy lifting?

@BeyondEvil
Copy link
Contributor

@nicoddemus But doesn't the tmpdir "system" do some form of cleanup right before pytest is finished? I was trying to find out, but got lost in the code...

@nicoddemus
Copy link
Member

Yes it does, but only after 3 attempts it seems

@davehunt
Copy link
Contributor

Agreed, I'd use tmpdir if we can (from the context of a hook).

@BeyondEvil
Copy link
Contributor

@davehunt But.. but.. wouldn't pytest delete the file then when it's done?

Or did I misinterpret @nicoddemus ?

@davehunt
Copy link
Contributor

davehunt commented Sep 10, 2018 via email

@BeyondEvil
Copy link
Contributor

@davehunt
Copy link
Contributor

I'm not entirely sure that you can, but perhaps @nicoddemus has a suggestion..?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants