|
|
import assert from 'assert'; |
|
|
import { Locator, Page } from 'playwright'; |
|
|
import { |
|
|
BlockInserter, |
|
|
EditorSidebarBlockInserterComponent, |
|
|
EditorToolbarComponent, |
|
|
EditorWelcomeTourComponent, |
|
|
EditorPopoverMenuComponent, |
|
|
EditorSiteStylesComponent, |
|
|
ColorSettings, |
|
|
TypographySettings, |
|
|
ColorLocation, |
|
|
FullSiteEditorSavePanelComponent, |
|
|
EditorBlockToolbarComponent, |
|
|
BlockToolbarButtonIdentifier, |
|
|
TemplatePartListComponent, |
|
|
FullSiteEditorNavSidebarComponent, |
|
|
FullSiteEditorDataViewsComponent, |
|
|
TemplatePartModalComponent, |
|
|
OpenInlineInserter, |
|
|
EditorInlineBlockInserterComponent, |
|
|
DimensionsSettings, |
|
|
CookieBannerComponent, |
|
|
EditorComponent, |
|
|
} from '..'; |
|
|
import { getIdFromBlock } from '../../element-helper'; |
|
|
import envVariables from '../../env-variables'; |
|
|
|
|
|
const selectors = { |
|
|
editorRoot: 'body.block-editor-page', |
|
|
editorCanvasIframe: 'iframe[name="editor-canvas"]', |
|
|
editorCanvasRoot: 'body.block-editor-iframe__body', |
|
|
templateLoadingSpinner: '[aria-label="Block: Template Part"] .components-spinner', |
|
|
closeStylesWelcomeGuideButton: |
|
|
'[aria-label="Welcome to styles"] button[aria-label="Close dialog"]', |
|
|
limitedGlobalStylesModalTryButton: '.wpcom-global-styles-modal__actions button:first-child', |
|
|
|
|
|
confirmationToast: ( text: string ) => `.components-snackbar:has-text('${ text }')`, |
|
|
focusedBlock: ( blockSelector: string ) => `${ blockSelector }.is-selected`, |
|
|
parentOfFocusedBlock: ( blockSelector: string ) => `${ blockSelector }.has-child-selected`, |
|
|
limitedGlobalStylesNotice: '.wpcom-global-styles-notice', |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class FullSiteEditorPage { |
|
|
private page: Page; |
|
|
private editor: EditorComponent; |
|
|
private editorToolbarComponent: EditorToolbarComponent; |
|
|
private editorSidebarBlockInserterComponent: EditorSidebarBlockInserterComponent; |
|
|
private editorInlineBlockInserterComponent: EditorInlineBlockInserterComponent; |
|
|
private editorWelcomeTourComponent: EditorWelcomeTourComponent; |
|
|
private editorPopoverMenuComponent: EditorPopoverMenuComponent; |
|
|
private editorSiteStylesComponent: EditorSiteStylesComponent; |
|
|
private editorBlockToolbarComponent: EditorBlockToolbarComponent; |
|
|
private fullSiteEditorSavePanelComponent: FullSiteEditorSavePanelComponent; |
|
|
private fullSiteEditorNavSidebarComponent: FullSiteEditorNavSidebarComponent; |
|
|
private fullSiteEditorDataViewsComponent: FullSiteEditorDataViewsComponent; |
|
|
private templatePartModalComponent: TemplatePartModalComponent; |
|
|
private templatePartListComponent: TemplatePartListComponent; |
|
|
private cookieBannerComponent: CookieBannerComponent; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor( page: Page ) { |
|
|
this.page = page; |
|
|
|
|
|
this.editor = new EditorComponent( page ); |
|
|
|
|
|
this.editorToolbarComponent = new EditorToolbarComponent( page, this.editor ); |
|
|
this.editorWelcomeTourComponent = new EditorWelcomeTourComponent( page, this.editor ); |
|
|
this.editorPopoverMenuComponent = new EditorPopoverMenuComponent( page, this.editor ); |
|
|
this.editorSiteStylesComponent = new EditorSiteStylesComponent( page, this.editor ); |
|
|
this.editorBlockToolbarComponent = new EditorBlockToolbarComponent( page, this.editor ); |
|
|
this.fullSiteEditorNavSidebarComponent = new FullSiteEditorNavSidebarComponent( |
|
|
page, |
|
|
this.editor |
|
|
); |
|
|
this.fullSiteEditorDataViewsComponent = new FullSiteEditorDataViewsComponent( |
|
|
page, |
|
|
this.editor |
|
|
); |
|
|
this.editorSidebarBlockInserterComponent = new EditorSidebarBlockInserterComponent( |
|
|
page, |
|
|
this.editor |
|
|
); |
|
|
this.editorInlineBlockInserterComponent = new EditorInlineBlockInserterComponent( |
|
|
page, |
|
|
this.editor |
|
|
); |
|
|
this.fullSiteEditorSavePanelComponent = new FullSiteEditorSavePanelComponent( |
|
|
page, |
|
|
this.editor |
|
|
); |
|
|
this.templatePartModalComponent = new TemplatePartModalComponent( page, this.editor ); |
|
|
this.templatePartListComponent = new TemplatePartListComponent( page, this.editor ); |
|
|
this.cookieBannerComponent = new CookieBannerComponent( page, this.editor ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async visit( siteHostWithProtocol: string ): Promise< void > { |
|
|
let parsedUrl: URL; |
|
|
try { |
|
|
parsedUrl = new URL( siteHostWithProtocol ); |
|
|
} catch ( error ) { |
|
|
throw new Error( |
|
|
`Invalid site host URL provided: "${ siteHostWithProtocol }". Did you remember to include the protocol?` |
|
|
); |
|
|
} |
|
|
|
|
|
parsedUrl.pathname = '/wp-admin/site-editor.php'; |
|
|
parsedUrl.searchParams.set( 'calypso_origin', envVariables.CALYPSO_BASE_URL ); |
|
|
|
|
|
await this.page.goto( parsedUrl.href, { timeout: 60 * 1000 } ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async waitUntilLoaded(): Promise< void > { |
|
|
const editorCanvas = await this.editor.canvas(); |
|
|
|
|
|
const spinnerLocator = editorCanvas.locator( selectors.templateLoadingSpinner ); |
|
|
|
|
|
await spinnerLocator.first().waitFor( { state: 'detached' } ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async prepareForInteraction( |
|
|
{ |
|
|
leaveWithoutSaving, |
|
|
}: { |
|
|
leaveWithoutSaving?: boolean; |
|
|
} = { leaveWithoutSaving: true } |
|
|
): Promise< void > { |
|
|
|
|
|
if ( envVariables.VIEWPORT_NAME === 'desktop' ) { |
|
|
await this.waitUntilLoaded(); |
|
|
} |
|
|
|
|
|
await this.editorWelcomeTourComponent.forceDismissWelcomeTour(); |
|
|
await this.cookieBannerComponent.acceptCookie(); |
|
|
|
|
|
if ( leaveWithoutSaving ) { |
|
|
this.page.on( 'dialog', async ( dialog ) => { |
|
|
if ( dialog.type() === 'beforeunload' ) { |
|
|
await dialog.accept(); |
|
|
} |
|
|
} ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async clickFullSiteNavigatorButton( text: string ): Promise< void > { |
|
|
await this.fullSiteEditorNavSidebarComponent.clickNavButtonByExactText( text ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async openTemplateEditor( text: string ): Promise< void > { |
|
|
await this.fullSiteEditorDataViewsComponent.clickPrimaryFieldByExactText( text ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async ensureNavigationTopLevel(): Promise< void > { |
|
|
await this.fullSiteEditorNavSidebarComponent.ensureNavigationTopLevel(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async addBlockFromSidebar( blockName: string, blockEditorSelector: string ): Promise< Locator > { |
|
|
await this.editorToolbarComponent.openBlockInserter(); |
|
|
await this.addBlockFromInserter( blockName, this.editorSidebarBlockInserterComponent ); |
|
|
const addedBlockId = await this.getIdOfAddedBlock( blockEditorSelector ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( envVariables.VIEWPORT_NAME !== 'mobile' ) { |
|
|
await this.editorToolbarComponent.closeBlockInserter(); |
|
|
} |
|
|
|
|
|
const editorCanvas = await this.editor.canvas(); |
|
|
return editorCanvas.locator( `#${ addedBlockId }` ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async addBlockInline( |
|
|
blockName: string, |
|
|
blockEditorSelector: string, |
|
|
openInlineInserter: OpenInlineInserter |
|
|
): Promise< Locator > { |
|
|
|
|
|
await openInlineInserter( await this.editor.canvas() ); |
|
|
await this.addBlockFromInserter( blockName, this.editorInlineBlockInserterComponent ); |
|
|
const addedBlockId = await this.getIdOfAddedBlock( blockEditorSelector ); |
|
|
const editorCanvas = await this.editor.canvas(); |
|
|
return editorCanvas.locator( `#${ addedBlockId }` ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async addBlockFromInserter( |
|
|
blockName: string, |
|
|
inserter: BlockInserter |
|
|
): Promise< void > { |
|
|
await inserter.searchBlockInserter( blockName ); |
|
|
await inserter.selectBlockInserterResult( blockName ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async getIdOfAddedBlock( blockEditorSelector: string ): Promise< string > { |
|
|
const editorCanvas = await this.editor.canvas(); |
|
|
|
|
|
const addedBlockLocator = editorCanvas.locator( |
|
|
`${ selectors.focusedBlock( blockEditorSelector ) },${ selectors.parentOfFocusedBlock( |
|
|
blockEditorSelector |
|
|
) }` |
|
|
); |
|
|
await addedBlockLocator.waitFor(); |
|
|
|
|
|
return await getIdFromBlock( addedBlockLocator ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async focusBlock( blockSelector: string ): Promise< void >; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async focusBlock( blockLocator: Locator ): Promise< void >; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async focusBlock( block: string | Locator ): Promise< void > { |
|
|
const editorCanvas = await this.editor.canvas(); |
|
|
let originalBlockLocator: Locator; |
|
|
let focusedBlockLocator: Locator; |
|
|
if ( typeof block === 'string' ) { |
|
|
|
|
|
originalBlockLocator = editorCanvas.locator( block ); |
|
|
focusedBlockLocator = editorCanvas.locator( selectors.focusedBlock( block ) ); |
|
|
} else { |
|
|
|
|
|
originalBlockLocator = block; |
|
|
|
|
|
|
|
|
const blockId = await getIdFromBlock( block ); |
|
|
focusedBlockLocator = editorCanvas.locator( selectors.focusedBlock( `#${ blockId }` ) ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for ( let clickAttempt = 1; clickAttempt <= 3; clickAttempt++ ) { |
|
|
if ( ( await focusedBlockLocator.count() ) > 0 ) { |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
await originalBlockLocator.click(); |
|
|
} |
|
|
|
|
|
await focusedBlockLocator.waitFor(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async clickBlockToolbarPrimaryButton( |
|
|
indentifier: BlockToolbarButtonIdentifier |
|
|
): Promise< void > { |
|
|
await this.editorBlockToolbarComponent.clickPrimaryButton( indentifier ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async clickBlockToolbarOption( optionName: string ): Promise< void > { |
|
|
await this.editorBlockToolbarComponent.clickOptionsButton(); |
|
|
await this.editorPopoverMenuComponent.clickMenuButton( optionName ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async undo(): Promise< void > { |
|
|
await this.editorToolbarComponent.undo(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async redo(): Promise< void > { |
|
|
await this.editorToolbarComponent.redo(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async save(): Promise< void > { |
|
|
await this.clearExistingSaveConfirmationToast(); |
|
|
await this.editorToolbarComponent.saveSiteEditor(); |
|
|
await this.fullSiteEditorSavePanelComponent.confirmSave(); |
|
|
await this.waitForConfirmationToast( 'Site updated.' ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async openNavSidebar(): Promise< void > { |
|
|
const editorParent = await this.editor.parent(); |
|
|
const openButton = editorParent.locator( 'a[aria-label="Open Navigation"]' ); |
|
|
|
|
|
await openButton.click(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async closeNavSidebar(): Promise< void > { |
|
|
if ( envVariables.VIEWPORT_NAME === 'mobile' ) { |
|
|
throw new Error( |
|
|
'There is no standardized way to close the site editor navigation sidebar on mobile. Navigate to a template or template part instead.' |
|
|
); |
|
|
} |
|
|
const editorParent = await this.editor.parent(); |
|
|
const editorCanvas = await this.editor.canvas(); |
|
|
const openButton = editorParent.locator( 'a[aria-label="Open Navigation"]' ); |
|
|
|
|
|
await Promise.race( [ openButton.waitFor(), editorCanvas.locator( 'body' ).click() ] ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async openDocumentActionsDropdown(): Promise< void > { |
|
|
await this.editorToolbarComponent.clickDocumentActionsIcon(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async clickDocumentActionsDropdownItem( itemSelector: string ): Promise< void > { |
|
|
await this.editorToolbarComponent.clickDocumentActionsDropdownItem( itemSelector ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async openSiteStyles( |
|
|
{ closeWelcomeGuide }: { closeWelcomeGuide: boolean } = { closeWelcomeGuide: true } |
|
|
): Promise< void > { |
|
|
if ( ! ( await this.editorSiteStylesComponent.siteStylesIsOpen() ) ) { |
|
|
await this.editorToolbarComponent.openMoreOptionsMenu(); |
|
|
|
|
|
if ( closeWelcomeGuide ) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const safelyWatchForWelcomeGuide = () => |
|
|
this.closeStylesWelcomeGuide().catch( () => { |
|
|
|
|
|
} ); |
|
|
safelyWatchForWelcomeGuide(); |
|
|
} |
|
|
await this.editorPopoverMenuComponent.clickMenuButton( 'Styles' ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async closeStylesWelcomeGuide(): Promise< void > { |
|
|
const editorParent = await this.editor.parent(); |
|
|
const locator = editorParent.locator( selectors.closeStylesWelcomeGuideButton ); |
|
|
await locator.click( { timeout: 5 * 1000 } ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async closeSiteStyles(): Promise< void > { |
|
|
await this.editorSiteStylesComponent.closeSiteStyles(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async clickStylesMenuButton( buttonName: string ): Promise< void > { |
|
|
await this.editorSiteStylesComponent.clickMenuButton( buttonName ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async returnToStylesTopMenu(): Promise< void > { |
|
|
await this.editorSiteStylesComponent.returnToTopMenu(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async setGlobalColorStyle( |
|
|
colorLocation: ColorLocation, |
|
|
colorSettings: ColorSettings |
|
|
): Promise< void > { |
|
|
await this.editorSiteStylesComponent.setGlobalColor( colorLocation, colorSettings ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async setBlockTypographyStyle( |
|
|
blockName: string, |
|
|
typographySettings: TypographySettings |
|
|
): Promise< void > { |
|
|
await this.editorSiteStylesComponent.setBlockTypography( blockName, typographySettings ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async setGlobalLayoutStyle( dimensionsSettings: DimensionsSettings ): Promise< void > { |
|
|
await this.editorSiteStylesComponent.setGlobalLayout( dimensionsSettings ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async resetGlobalLayoutStyle(): Promise< void > { |
|
|
await this.editorSiteStylesComponent.resetGlobalLayout(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async resetStylesToDefaults(): Promise< void > { |
|
|
await this.editorSiteStylesComponent.openMoreActionsMenu(); |
|
|
await this.editorPopoverMenuComponent.clickMenuButton( 'Reset to defaults' ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async tryGlobalStyles(): Promise< void > { |
|
|
const editorParent = await this.editor.parent(); |
|
|
const locator = editorParent.locator( selectors.limitedGlobalStylesModalTryButton ); |
|
|
await locator.click(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async setStyleVariation( styleVariationName: string ): Promise< void > { |
|
|
await this.fullSiteEditorNavSidebarComponent.setStyleVariation( styleVariationName ); |
|
|
const hasCustomStyles = styleVariationName !== 'Default'; |
|
|
const editorParent = await this.editor.parent(); |
|
|
const limitedGlobalStylesNotice = editorParent.locator( selectors.limitedGlobalStylesNotice ); |
|
|
if ( hasCustomStyles ) { |
|
|
await limitedGlobalStylesNotice.waitFor(); |
|
|
} else { |
|
|
const count = await limitedGlobalStylesNotice.count(); |
|
|
assert.equal( count, 0 ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async nameAndFinalizeTemplatePart( name: string ): Promise< void > { |
|
|
await this.templatePartModalComponent.enterTemplateName( name ); |
|
|
await this.templatePartModalComponent.clickCreate(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async selectExistingTemplatePartFromModal( name: string ): Promise< void > { |
|
|
await this.templatePartModalComponent.selectExistingTemplatePart( name ); |
|
|
|
|
|
await this.waitForConfirmationToast( `Template Part "${ name }" inserted.` ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async deleteTemplateParts( names: string[] ): Promise< void > { |
|
|
await this.openNavSidebar(); |
|
|
await this.fullSiteEditorNavSidebarComponent.navigateToTemplatePartsManager(); |
|
|
for ( const name of names ) { |
|
|
await this.templatePartListComponent.deleteTemplatePart( name ); |
|
|
await this.waitForConfirmationToast( `"${ name }" deleted.` ); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async waitForConfirmationToast( text: string ): Promise< void > { |
|
|
const editorParent = await this.editor.parent(); |
|
|
const locator = editorParent.locator( selectors.confirmationToast( text ) ); |
|
|
await locator.waitFor(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async clearExistingSaveConfirmationToast(): Promise< void > { |
|
|
const editorParent = await this.editor.parent(); |
|
|
const toastLocator = editorParent.locator( selectors.confirmationToast( 'Site updated.' ) ); |
|
|
if ( ( await toastLocator.count() ) > 0 ) { |
|
|
await toastLocator.click(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|