Skip to content

Commit 8de28f6

Browse files
committed
Added support for the mp4 video format.
Support uses the raw html section in order to pass a video tag which is compatible with html5. Video size and style should be handled by the "video" class in the css file.
1 parent 23186cd commit 8de28f6

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

pytest_html/extras.py

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
FORMAT_JSON = "json"
88
FORMAT_TEXT = "text"
99
FORMAT_URL = "url"
10+
FORMAT_VIDEO = "video"
1011

1112

1213
def extra(content, format, name=None, mime_type=None, extension=None):
@@ -49,3 +50,11 @@ def text(content, name="Text"):
4950

5051
def url(content, name="URL"):
5152
return extra(content, FORMAT_URL, name)
53+
54+
55+
def video(content, name="Video", mime_type="video/mp4", extension="mp4"):
56+
return extra(content, FORMAT_VIDEO, name, mime_type, extension)
57+
58+
59+
def mp4(content, name="Video"):
60+
return video(content, name)

pytest_html/plugin.py

+34
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,40 @@ def append_extra_html(self, extra, extra_index, test_index):
235235
elif extra.get("format") == extras.FORMAT_URL:
236236
href = extra.get("content")
237237

238+
elif extra.get("format") == extras.FORMAT_VIDEO:
239+
video_base = (
240+
'<video controls>\n<source src={} type="video/mp4">\n</video>'
241+
)
242+
content = extra.get("content")
243+
try:
244+
is_uri_or_path = content.startswith(("file", "http")) or isfile(
245+
content
246+
)
247+
except ValueError:
248+
# On Windows, os.path.isfile throws this exception when
249+
# passed a b64 encoded image.
250+
is_uri_or_path = False
251+
if is_uri_or_path:
252+
if self.self_contained:
253+
warnings.warn(
254+
"Self-contained HTML report "
255+
"includes link to external "
256+
"resource: {}".format(content)
257+
)
258+
259+
html_div = html.div(raw(video_base.format(extra.get("content"))))
260+
elif self.self_contained:
261+
src = "data:{};base64,{}".format(extra.get("mime_type"), content)
262+
html_div = html.div(raw(video_base.format(src)))
263+
else:
264+
content = b64decode(content.encode("utf-8"))
265+
href = src = self.create_asset(
266+
content, extra_index, test_index, extra.get("extension"), "wb"
267+
)
268+
269+
html_div = html.a(video_base.format(src), href=href)
270+
self.additional_html.append(html.div(html_div, class_="video"))
271+
238272
if href is not None:
239273
self.links_html.append(
240274
html.a(

0 commit comments

Comments
 (0)