Skip to content
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

[try] Record failed e2e tests as artifacts #26959

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/workflows/end2end-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ jobs:
npm ci
FORCE_REDUCED_MOTION=true npm run build

- name: Install ffmpeg
run: sudo apt-get install ffmpeg

- name: Install WordPress
run: |
chmod -R 767 ./ # TODO: Possibly integrate in wp-env
Expand Down Expand Up @@ -89,6 +92,9 @@ jobs:
npm ci
FORCE_REDUCED_MOTION=true npm run build

- name: Install ffmpeg
run: sudo apt-get install ffmpeg

- name: Install WordPress
run: |
chmod -R 767 ./ # TODO: Possibly integrate in wp-env
Expand Down Expand Up @@ -137,6 +143,9 @@ jobs:
npm ci
FORCE_REDUCED_MOTION=true npm run build

- name: Install ffmpeg
run: sudo apt-get install ffmpeg

- name: Install WordPress
run: |
chmod -R 767 ./ # TODO: Possibly integrate in wp-env
Expand Down Expand Up @@ -185,6 +194,9 @@ jobs:
npm ci
FORCE_REDUCED_MOTION=true npm run build

- name: Install ffmpeg
run: sudo apt-get install ffmpeg

- name: Install WordPress
run: |
chmod -R 767 ./ # TODO: Possibly integrate in wp-env
Expand Down
70 changes: 68 additions & 2 deletions packages/e2e-tests/config/setup-debug-artifacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@
* External dependencies
*/
const fs = require( 'fs' );
const { spawn } = require( 'child_process' );
const util = require( 'util' );
const root = process.env.GITHUB_WORKSPACE || __dirname + '/../../../';
const ARTIFACTS_PATH = root + '/artifacts';
const TMP_PATH = ARTIFACTS_PATH + '/tmp';

const writeFile = util.promisify( fs.writeFile );

if ( ! fs.existsSync( ARTIFACTS_PATH ) ) {
fs.mkdirSync( ARTIFACTS_PATH );
for ( const path of [ ARTIFACTS_PATH, TMP_PATH ] ) {
if ( ! fs.existsSync( path ) ) {
fs.mkdirSync( path );
}
}
// afterAll( () => fs.rmdirSync( TMP_PATH ) );

/**
* Gutenberg uses the default jest-jasmine2 test runner that comes with Jest.
Expand All @@ -23,6 +28,7 @@ if ( ! fs.existsSync( ARTIFACTS_PATH ) ) {
let artifactsPromise;
// eslint-disable-next-line jest/no-jasmine-globals
jasmine.getEnv().addReporter( {
specStarted: () => startVideo(),
specDone: ( result ) => {
if ( result.status === 'failed' ) {
artifactsPromise = storeArtifacts( result.fullName );
Expand All @@ -35,6 +41,8 @@ afterAll( () => artifactsPromise );

async function storeArtifacts( testName ) {
const slug = slugify( testName );
await stopVideo();
await encodeVideo( `${ ARTIFACTS_PATH }/${ slug }-video.mp4` );
await writeFile(
`${ ARTIFACTS_PATH }/${ slug }-snapshot.html`,
await page.content()
Expand All @@ -52,3 +60,61 @@ function slugify( testName ) {
.replace( / /g, '-' );
return slug;
}

let frames = [];
let session;

const startVideo = async () => {
frames = [];
if ( ! session ) {
session = await page.target().createCDPSession();
}
await session.send( 'Page.startScreencast', {
format: 'png',
everyNthFrame: 1,
} );
session.on( 'Page.screencastFrame', ( event ) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to register a new listener for every started test? do they get cleared on stopScreencast?

frames.push( Buffer.from( event.data, 'base64' ) );
} );
};
const stopVideo = () => {
session.send( 'Page.stopScreencast' );
};
const encodeVideo = async ( outfile ) => {
if ( ! frames.length ) {
return;
}

for ( let i = 0, max = frames.length; i < max; i++ ) {
fs.writeFileSync( `${ TMP_PATH }/${ i }.png`, frames[ i ] );
}

const ffmpeg = spawn( 'ffmpeg', [
'-framerate',
'5',
'-pattern_type',
'glob',
'-i',
`${ TMP_PATH }/*.png`,
'-c:v',
'libx264',
'-r',
'30',
'-pix_fmt',
'yuv420p',
outfile,
] );

const closed = new Promise( ( resolve, reject ) => {
ffmpeg.on( 'error', reject );
ffmpeg.on( 'close', resolve );
} );

await closed;

for ( let i = 0, max = frames.length; i < max; i++ ) {
try {
fs.unlinkSync( `${ TMP_PATH }/${ i }.png` );
} catch ( e ) {}
}
};