-
Notifications
You must be signed in to change notification settings - Fork 252
Add ability to add screenshot as file or link (http) #111
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
Conversation
haha, I just can't get a win. Not sure why there's two commits in there. However, I do recognize the additional commit as one of my old PRs (encoding test issue). |
content = extra.get('content') | ||
if re.match('^file|^http', content): | ||
html_div = html.img(src=content) | ||
elif self.self_contained: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm... so this means if the --self-contained-html
flag is passed, these images will not be displayed in the report at all. Whilst this is possibly acceptable (I don't think we should be downloading/reading these files) it could be confusing to users. Perhaps we could add a warning to the documentation that standalone reports may still include assets referenced by URL? We could also log a warning for each URL based asset when we're in standalone mode.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add a warning to the docs and a logging warning (using pytest warning?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davehunt I need some guidance on how to add the pytest warning. Too many options. Do I get the terminal reporter via getplugin()? Do I use one of the hooks? etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use:
import warnings
warnings.warn('blah blah blah')
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just FYI, pytest-3.1 (to be released this week) automatically shows warnings such as these in a summary at the end of the test suite, so this is the best suggestion at the moment. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wording OK with you, @davehunt ?
if content.startswith(('file', 'http')) or os.path.isfile(content):
if self.self_contained:
warnings.warn('Images added via file or link, may not '
'work as expected when using '
'--self-contained-html')
html_div = html.img(src=content)
elif self.self_contained:```
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"so this means if the --self-contained-html flag is passed, these images will not be displayed in the report at all. "
Hmm... they should be, whatever html.img() we create gets added after the if/elifs.
I tested with all types with and w/o --self-contained, and it worked.
Tested it again with the image gallery PR.
EDIT: I think I get what you mean now, the report will show all images as long as they are local to the report, sharing the report will break the images.
testing/test_pytest_html.py
Outdated
|
||
@pytest.mark.parametrize('type', ["https://", "file://"]) | ||
def test_extra_image_non_b64(self, testdir, type): | ||
content = type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type
is reserved, so we should pick another name for this parameter.
pytest_html/plugin.py
Outdated
if extra.get('format') == extras.FORMAT_IMAGE: | ||
if self.self_contained: | ||
content = extra.get('content') | ||
if re.match('^file|^http', content): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this won't cover absolute or relative paths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what you mean... What won't cover absolute or relative paths? What other types are there? While testing this I used an image with an absolute path.
Do you want to cover relative paths as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean if I passed /absolute/path/to/file
, C:\absolute\path\to\file
, or ../relative/path/to/file
then this would not work. Perhaps simply using os.path
to check if the content is a valid path, or we could leave this as a future enhancement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, I can give it a try, and if it gets to complex we'll leave it for the future.
pytest_html/plugin.py
Outdated
if extra.get('format') == extras.FORMAT_IMAGE: | ||
if self.self_contained: | ||
content = extra.get('content') | ||
if re.match('^file|^http', content): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean if I passed /absolute/path/to/file
, C:\absolute\path\to\file
, or ../relative/path/to/file
then this would not work. Perhaps simply using os.path
to check if the content is a valid path, or we could leave this as a future enhancement.
pytest_html/plugin.py
Outdated
if extra.get('format') == extras.FORMAT_IMAGE: | ||
if self.self_contained: | ||
content = extra.get('content') | ||
if re.match('^file|^http', content): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than importing re
you could just use startswith
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, I started with that but thought that re was better looking :P
However, with the abs/rel path change, I'll revert back to startswith 👍
README.rst
Outdated
Image ``extra.image('http://some_image.png')`` | ||
========== ============================================ | ||
|
||
**Note**: When using ``--self-contained-html``, images added as files or links may not work as expected. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we expand this a little more. These images are going to be linked as external resources even when --self-contained-html
is specified. The expectation is that this command line option means there's a standalone HTML file that contains all resources.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also think we should move this to the 'Creating a self-contained report' section, and reference/link to that section here.
pytest_html/plugin.py
Outdated
if self.self_contained: | ||
warnings.warn('Images added via file or link, ' | ||
'may not work as expected when ' | ||
'using --self-contained-html') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer something like "Self-contained HTML report includes link to external resource: /foo/bar"
assert os.path.exists(src) | ||
|
||
@pytest.mark.parametrize('src_type', ["https://", "file://"]) | ||
def test_extra_image_non_b64(self, testdir, src_type): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For coverage, we should also include a test that creates a file in the testdir and references it as an image path.
testing/test_pytest_html.py
Outdated
assert result.ret == 0 | ||
assert '<img src="{0}"/>'.format(content) in html | ||
|
||
def test_extra_text_encoding(self, testdir): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this test has sneaked into your patch somehow. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote this way back when you were having encoding issues. I can remove it if you don't want it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping @davehunt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've started looking at the image/slider PR @davehunt, I've got most of your requests in place, I just need to fine tune the CSS.
However, I'd prefer to get this PR out of the way first, since they hit more or less exactly the same code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I landed a change to the tests that covered this in https://github.com/pytest-dev/pytest-html/pull/108/files so this test isn't required. You can leave it here and I'll remove it before merging, if you prefer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
README.rst
Outdated
|
||
**Note**: When using ``--self-contained-html``, images added as files or links | ||
may not work as expected, see the | ||
:ref:`Creating a self-contained report <self-contained>` section for more info. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to create a reference as all titles can already be referenced. This would work as:
**Note**: When using ``--self-contained-html``, images added as files or links
may not work as expected, see section `Creating a self-contained report`_ for
more info.
README.rst
Outdated
you have this package installed, then ANSI codes will be converted to HTML in | ||
your report. | ||
|
||
.. _self-contained: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't needed, as the title can already be referenced.
README.rst
Outdated
.. _self-contained: | ||
|
||
Creating a self-contained report | ||
---------------------------------- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind trimming the underline here so it doesn't exceed the title length? 👼
extra.append(pytest_html.extras.text('some string', name='Different title')) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this came in through a merge/rebase? I can fix this when we come to merge if you like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please. I have no idea if it belongs or not.
pytest_html/plugin.py
Outdated
warnings.warn('Self-contained HTML report ' | ||
'includes link to external ' | ||
'resource: {}'.format(content)) | ||
html_div = html.img(src=content) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no link here to the image. We should set href
to the value of content
and include it in the html_div
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll fix that here, but that is taken care of in the image gallery PR. 👍
testing/test_pytest_html.py
Outdated
testdir.makefile('.png', image='pretty picture') | ||
result, html = run(testdir, 'report.html') | ||
assert result.ret == 0 | ||
assert '<img src="{0}"/>'.format(content) in html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also assert the href attribute.
Ping @davehunt New PR, hope it is to your satisfaction. :) |
This looks great. I'll do a bit of testing locally with it before merging. |
Merged in f19a86d |
Addresses issue #77 ping @davehunt