File size: 4,986 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
import { Locator, Page } from 'playwright';
import { envVariables } from '../..';
import { EditorComponent } from './editor-component';
const parentSelector = '[aria-label="Block tools"]';
const selectors = {
button: ( identifier: BlockToolbarButtonIdentifier ) => {
if ( ! ( identifier.ariaLabel || identifier.text ) ) {
throw new Error( 'You must provide at least one way to identify the menu button.' );
}
let selector = `${ parentSelector } button`;
if ( identifier.ariaLabel ) {
selector = `${ selector }[aria-label="${ identifier.ariaLabel }"]`;
}
if ( identifier.text ) {
selector = `${ selector }:has-text("${ identifier.text }")`;
}
return selector;
},
};
export interface BlockToolbarButtonIdentifier {
text?: string;
ariaLabel?: string;
name?: string;
}
/**
* Represents the toolbar menu that appears for a focused block.
*/
export class EditorBlockToolbarComponent {
private page: Page;
private editor: EditorComponent;
/**
* Constructs an instance of the component.
*
* @param {Page} page The underlying page.
* @param {EditorComponent} editor The EditorComponent instance.
*/
constructor( page: Page, editor: EditorComponent ) {
this.page = page;
this.editor = editor;
}
/* General helper */
/**
* Given a Locator, determines whether the target button/toggle is
* in an expanded state.
*
* If the toggle is in the on state or otherwise in an expanded
* state, this method will return true. Otherwise, false.
*
* @param {Locator} target Target button.
* @returns {Promise<boolean>} True if target is in an expanded state. False otherwise.
*/
private async targetIsOpen( target: Locator ): Promise< boolean > {
const checked = await target.getAttribute( 'aria-checked' );
const pressed = await target.getAttribute( 'aria-pressed' );
const expanded = await target.getAttribute( 'aria-expanded' );
return checked === 'true' || pressed === 'true' || expanded === 'true';
}
/**
* Click one of the primary (not buried under a drop down) buttons in the block toolbar.
*
* @param {BlockToolbarButtonIdentifier} identifier Ways to identify the button.
*/
async clickPrimaryButton( identifier: BlockToolbarButtonIdentifier ): Promise< void > {
const editorParent = await this.editor.parent();
let locator: Locator;
if ( identifier.name ) {
// Accessible names don't need to have the selector built, but needs to be narrowed
// to the toolbar.
locator = editorParent
.getByRole( 'toolbar', { name: 'Block tools' } )
.getByRole( 'button', { name: identifier.name } );
} else {
// Other identifers need to have the selector built.
locator = editorParent.locator( selectors.button( identifier ) );
}
const handle = await locator.elementHandle();
await handle?.waitForElementState( 'stable' );
await locator.click();
}
/**
* Click on the options button (three dots).
*/
async clickOptionsButton(): Promise< void > {
const editorParent = await this.editor.parent();
const locator = editorParent.locator( selectors.button( { ariaLabel: 'Options' } ) );
await locator.click();
}
/**
* Checks if a menu button is open.
*
* @returns {boolean} True if the menu button is open, false otherwise.
*/
async isOptionsMenuOpen(): Promise< boolean > {
const editorParent = await this.editor.parent();
const optionsLocator = editorParent.locator( selectors.button( { ariaLabel: 'Options' } ) );
return this.targetIsOpen( optionsLocator );
}
/**
* Click the up arrow button to move the current block up.
*/
async moveUp(): Promise< void > {
const editorParent = await this.editor.parent();
const locator = editorParent.locator( selectors.button( { ariaLabel: 'Move up' } ) );
await locator.click();
}
/**
* Click the down arrow button to move the current block down.
*/
async moveDown(): Promise< void > {
const editorParent = await this.editor.parent();
const locator = editorParent.locator( selectors.button( { ariaLabel: 'Move down' } ) );
await locator.click();
}
/**
* Clicks the parent block button on the toolbar. Note, this only applies to desktop, as this button
* is hidden under more options on mobile.
*
* @param {string} expectedParentBlockName The expected name of the parent block.
*/
async clickParentBlockButton( expectedParentBlockName: string ): Promise< void > {
// On mobile, you select the parent block in a separate options menu item.
// That interaction should be driven by the parent method in Editor pages.
if ( envVariables.VIEWPORT_NAME === 'desktop' ) {
const editorParent = await this.editor.parent();
const locator = editorParent
.locator( parentSelector )
.getByRole( 'button', { name: `Select parent block: ${ expectedParentBlockName }` } );
await locator.click();
await locator.waitFor( { state: 'detached' } );
} else {
throw new Error( 'The separate parent block toolbar button is not available on mobile.' );
}
}
}
|