pytest-dev / pytest-dev/pytest-html
Cannot take screenshots, it return me an internal error
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 779
- Forks
- 260
- PR merge metrics
- No merged PRs in 30d
Description
I got this code:
`
import os
import selenium
from selenium import webdriver
import time
from datetime import datetime
import pytest
from selenium import webdriver as selenium_webdriver
from selenium.webdriver.chrome.options import Options
@pytest.fixture()
def setup():
driver = webdriver.Chrome(executable_path=r'E:\Python Projects\StackField\testCases\chromedriver.exe')
return driver
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
timestamp = datetime.now().strftime('%H-%M-%S')
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == 'call':
feature_request = item.funcargs['request']
driver = feature_request.getfuncargvalue('browser')
driver.save_screenshot('D:/report/scr'+timestamp+'.png')
extra.append(pytest_html.extras.image('D:/report/scr'+timestamp+'.png'))
# always add url to report
extra.append(pytest_html.extras.url('http://www.example.com/'))
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
# only add additional html on failure
extra.append(pytest_html.extras.image('D:/report/scr.png'))
extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
report.extra = extra
`
And after I use this command inside the Pycharm Terminal:
pytest --html=report.html --self-contained-html testCases/test_basic.py
After the test is done I received this error:
INTERNALERROR> feature_request = item.funcargs['request']
INTERNALERROR> KeyError: 'request'
I try other method:
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
pytest_html = item.config.pluginmanager.getplugin("html")
outcome = yield
report = outcome.get_result()
extra = getattr(report, "extra", [])
if report.when == "call":
# always add url to report
extra.append(pytest_html.extras.url("https://sprint.moravis.com/run/val/adchef/a0bfkgb"))
xfail = hasattr(report, "wasxfail")
if (report.skipped and xfail) or (report.failed and not xfail):
# only add additional html on failure
report_directory = os.path.dirname(item.config.option.htmlpath)
# file_name = str(int(round(time.sleep() *1000)))+".png"
file_name = report.nodeid.replace(": :", "_") + ".png"
destinationFile = os.path.join(report_directory, file_name)
driver.save_screenshot(destinationFile)
if file_name:
html = '<div><img src ="%s" alt="screenshot" style="width:300px:height=200px"' '\'onclick ="window.open(this.src)" align="right"/></div>' % file_name
extra.append(pytest_html.extras.html(html))
report.extra = extra
I tried with driver.save_screenshot(name)
and returns me key error.
I don't know what to do next and I don't know very good programming. Thank you in advance!
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the pytest_runtest_makereport hook and the request and browser fixture references shown in the issue. Reproduce the problem with pytest --html=report.html --self-contained-html testCases/test_basic.py, then inspect the fixture data available through item.funcargs. Done means the command completes without an INTERNALERROR and the expected screenshot is included in the HTML report.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100