-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨ Serialize current video frame into a `poster` on the `<video>` Capturing videos via request interception can be tricky and highly depends on the customers server. Percy's infrastructure will also not play videos for screenshots so it doesn't make too much sense to capture large resources like vidoes. This feature will capture the current frame of the videos on page and turn them into a poster image for the video. This is so there's something to display on the `<video>` element when capturing a screenshot in Percy's browsers. It's possible for this to introduce flakes since this will capture the current frame of the video. If the video is playing when `percySnapshot` is called, we're going to capture different states of the video. In these cases the custoemr should provide their own `poster`, which will result in the most stable screenshots anyways. * 🏗 Fix karma warning for serving non-js files * 💚 Wait for video ready state & use webm video Turns out firefox on ubuntu can't play mpeg videos due to licensing. Swapping to a webm video makes the tests pass in CI > Note: Firefox doesn't include MPEG decoding (it's patented), so if that isn't already in your distribution, you might need to install additional software. Not sure if there are useful links here or whether a Linux user will need to jump in: Fix common audio and video issues. https://support.mozilla.org/en-US/questions/1227605#answer-1138231 * ♻️ Peer review feedback refactoring * 📝 Update README
- Loading branch information
Showing
7 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Captures the current frame of videos and sets the poster image | ||
export function serializeVideos(dom, clone) { | ||
for (let video of dom.querySelectorAll('video')) { | ||
// If the video already has a poster image, no work for us to do | ||
if (video.getAttribute('poster')) continue; | ||
|
||
let videoId = video.getAttribute('data-percy-element-id'); | ||
let cloneEl = clone.querySelector(`[data-percy-element-id="${videoId}"]`); | ||
let canvas = document.createElement('canvas'); | ||
let width = canvas.width = video.videoWidth; | ||
let height = canvas.height = video.videoHeight; | ||
|
||
canvas.getContext('2d').drawImage(video, 0, 0, width, height); | ||
|
||
let dataUrl = canvas.toDataURL(); | ||
// If the canvas produces a blank image, skip | ||
if (!dataUrl || dataUrl === 'data:,') continue; | ||
|
||
cloneEl.setAttribute('poster', dataUrl); | ||
} | ||
} | ||
|
||
export default serializeVideos; |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { withExample, parseDOM } from './helpers'; | ||
import serializeDOM from '@percy/dom'; | ||
|
||
let canPlay = $video => new Promise(resolve => { | ||
if ($video.readyState > 2) resolve(); | ||
else $video.addEventListener('canplay', resolve); | ||
}); | ||
|
||
describe('serializeVideos', () => { | ||
let $; | ||
|
||
it('serializes video elements', async () => { | ||
withExample(` | ||
<video src="base/test/assets/example.webm" id="video" controls /> | ||
`); | ||
|
||
await canPlay(window.video); | ||
$ = parseDOM(serializeDOM()); | ||
expect($('#video')[0].getAttribute('poster').length > 25).toBe(true); | ||
}); | ||
|
||
it('does not serialize videos with an existing poster', async () => { | ||
withExample(` | ||
<video src="base/test/assets/example.webm" id="video" poster="//:0" /> | ||
`); | ||
|
||
await canPlay(window.video); | ||
$ = parseDOM(serializeDOM()); | ||
expect($('#video')[0].getAttribute('poster')).toBe('//:0'); | ||
}); | ||
|
||
it('does not apply blank poster images', () => { | ||
withExample(` | ||
<video src="//:0" id="video" /> | ||
`); | ||
|
||
$ = parseDOM(serializeDOM()); | ||
expect($('#video')[0].getAttribute('poster')).toBe(null); | ||
}); | ||
}); |