Skip to content

Commit

Permalink
Testing: Add pressWithModifier E2E test utility
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Apr 25, 2018
1 parent 5a66935 commit e7f6b1c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
6 changes: 2 additions & 4 deletions test/e2e/specs/a11y.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import '../support/bootstrap';
import { newPost, newDesktopBrowserPage } from '../support/utils';
import { newPost, newDesktopBrowserPage, pressWithModifier } from '../support/utils';

describe( 'a11y', () => {
beforeAll( async () => {
Expand All @@ -11,9 +11,7 @@ describe( 'a11y', () => {
} );

it( 'tabs header bar', async () => {
await page.keyboard.down( 'Control' );
await page.keyboard.press( '~' );
await page.keyboard.up( 'Control' );
await pressWithModifier( 'Control', '~' );

await page.keyboard.press( 'Tab' );

Expand Down
6 changes: 2 additions & 4 deletions test/e2e/specs/multi-block-selection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import '../support/bootstrap';
import { newPost, newDesktopBrowserPage } from '../support/utils';
import { newPost, newDesktopBrowserPage, pressWithModifier } from '../support/utils';

describe( 'Multi-block selection', () => {
beforeAll( async () => {
Expand Down Expand Up @@ -62,9 +62,7 @@ describe( 'Multi-block selection', () => {

// Multiselect via keyboard
await page.click( 'body' );
await page.keyboard.down( 'Meta' );
await page.keyboard.press( 'a' );
await page.keyboard.up( 'Meta' );
await pressWithModifier( 'Mod', 'a' );

// Verify selection
expectMultiSelected( blocks, true );
Expand Down
28 changes: 28 additions & 0 deletions test/e2e/support/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ const {
WP_PASSWORD = 'password',
} = process.env;

/**
* Platform-specific modifier key.
*
* @see pressWithModifier
*
* @type {string}
*/
const MOD_KEY = process.platform === 'darwin' ? 'Meta' : 'Control';

function getUrl( WPPath, query = '' ) {
const url = new URL( WP_BASE_URL );

Expand Down Expand Up @@ -71,3 +80,22 @@ export async function getHTMLFromCodeEditor() {
await switchToEditor( 'Visual' );
return textEditorContent;
}

/**
* Performs a key press with modifier (Shift, Control, Meta, Mod), where "Mod"
* is normalized to platform-specific modifier (Meta in MacOS, else Control).
*
* @param {string} modifier Modifier key.
* @param {string} key Key to press while modifier held.
*
* @return {Promise} Promise resolving when key combination pressed.
*/
export async function pressWithModifier( modifier, key ) {
if ( modifier.toLowerCase() === 'mod' ) {
modifier = MOD_KEY;
}

await page.keyboard.down( modifier );
await page.keyboard.press( key );
return page.keyboard.up( modifier );
}

0 comments on commit e7f6b1c

Please sign in to comment.