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

Add visual tests support #5456

Merged
merged 4 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ commands:
- run:
name: Create reports directory
command: mkdir reports && mkdir reports/test-results
add-jest-snapshot-dir:
steps:
- run:
name: Create snapshots directory
command: mkdir __device-tests__/image-snapshots/diff
install-node-version:
steps:
- run:
Expand Down Expand Up @@ -248,6 +253,7 @@ jobs:
- install-node-version
- run: node -v
- npm-install-e2e-tests
- add-jest-snapshot-dir
- run: mkdir /home/circleci/test-results
- run:
name: Run Device Tests
Expand Down Expand Up @@ -356,6 +362,7 @@ jobs:
- install-node-version
- npm-install-e2e-tests
- add-jest-reporter-dir
- add-jest-snapshot-dir
- run:
name: Set Environment Variables
command: |
Expand Down
35 changes: 35 additions & 0 deletions __device-tests__/gutenberg-editor-gallery-visual.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Internal dependencies
*/
const { blockNames } = editorPage;
import { takeScreenshot } from './utils';

describe( 'Gutenberg Editor Visual test for Gallery Block', () => {
it( 'should be able to render the placeholder correctly', async () => {
await editorPage.addNewBlock( blockNames.gallery );
await editorPage.closePicker();

// Visual test check
const screenshot = await takeScreenshot();
expect( screenshot ).toMatchImageSnapshot();

await editorPage.removeBlockAtPosition( blockNames.gallery );
} );

it( 'should be able to render a gallery correctly', async () => {
await editorPage.setHtmlContent(
[ e2eTestData.galleryBlock ].join( '\n\n' )
);
const galleryBlock = await editorPage.getBlockAtPosition(
blockNames.gallery
);
expect( galleryBlock ).toBeTruthy();

// Wait for images to load
await editorPage.driver.sleep( 5000 );

// Visual test check
const screenshot = await takeScreenshot();
expect( screenshot ).toMatchImageSnapshot();
Copy link
Member

Choose a reason for hiding this comment

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

I noticed the previous test has what appears to be clean up code removing the block. Should all tests including this one have that as well? This question applies to __device-tests__/gutenberg-editor-group-visual.test.js as well.

Suggested change
expect( screenshot ).toMatchImageSnapshot();
expect( screenshot ).toMatchImageSnapshot();
await editorPage.removeBlockAtPosition( blockNames.gallery );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will depend on the test, for this case, to start with a "blank" canvas in the second test you need to remove the blocks you created before, as the other tests in the file will run in the same session.

} );
} );
23 changes: 23 additions & 0 deletions __device-tests__/gutenberg-editor-group-visual.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Internal dependencies
*/
const { blockNames } = editorPage;
import { takeScreenshot } from './utils';

describe( 'Gutenberg Editor Visual test for Group Block', () => {
it( 'should show the empty placeholder for the unselected state', async () => {
await editorPage.addNewBlock( blockNames.group );

// Select title to unfocus the block
const titleElement = await editorPage.getTitleElement();
titleElement.click();

await editorPage.dismissKeyboard();
// Wait for the keyboard to be hidden
await editorPage.driver.sleep( 3000 );

// Visual test check
const screenshot = await takeScreenshot();
expect( screenshot ).toMatchImageSnapshot();
} );
} );
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions __device-tests__/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* External dependencies
*/
import jimp from 'jimp';

const { isAndroid } = e2eUtils;

export async function takeScreenshot() {
const statusBarHeight = isAndroid() ? 100 : 94;
const screenshot = await editorPage.driver.takeScreenshot();

const base64Image = Buffer.from( screenshot, 'base64' );
const image = await jimp.read( base64Image );
image.crop(
0,
statusBarHeight,
image.getWidth(),
image.getHeight() - statusBarHeight
);
const resizedImage = await image.resize( 320, jimp.AUTO );
return resizedImage.getBufferAsync( jimp.MIME_PNG );
}
2 changes: 1 addition & 1 deletion gutenberg
Submodule gutenberg updated 248 files
1 change: 1 addition & 0 deletions jest_ui.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const gutenbergJestUIConfig = require( './gutenberg/packages/react-native-editor
module.exports = {
...gutenbergJestUIConfig,
setupFilesAfterEnv: [
'./jest_ui_setup_after_env.js',
'./gutenberg/packages/react-native-editor/jest_ui_setup_after_env.js',
],
testEnvironment:
Expand Down
24 changes: 24 additions & 0 deletions jest_ui_setup_after_env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Internal dependencies
*/
const {
isAndroid,
} = require( './gutenberg/packages/react-native-editor/__device-tests__/helpers/utils' );

// eslint-disable-next-line import/no-extraneous-dependencies
const { configureToMatchImageSnapshot } = require( 'jest-image-snapshot' );

const toMatchImageSnapshot = configureToMatchImageSnapshot( {
failureThreshold: 0.01, // 1% threshold.
failureThresholdType: 'percent',
dumpInlineDiffToConsole: true,
runInProcess: true,
customSnapshotIdentifier( data ) {
return `${ data.defaultIdentifier }-${
isAndroid() ? 'android' : 'ios'
}`;
},
customSnapshotsDir: '__device-tests__/image-snapshots',
customDiffDir: '__device-tests__/image-snapshots/diff',
} );
expect.extend( { toMatchImageSnapshot } );
Loading