|
|
import { Page, ElementHandle } from 'playwright'; |
|
|
import { EditorComponent } from '../..'; |
|
|
|
|
|
type Sources = 'Media Library' | 'Google Photos' | 'Pexels'; |
|
|
|
|
|
const selectors = { |
|
|
block: '.wp-block-image', |
|
|
|
|
|
|
|
|
fileInput: '.components-form-file-upload input[type="file"]', |
|
|
selectImageSourceButton: 'button.jetpack-external-media-button-menu', |
|
|
imageSource: ( source: Sources ) => `button :text("${ source }")`, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
image: ( filename: string ) => `${ selectors.block } img[data-image-title*="${ filename }" i]`, |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class ImageBlock { |
|
|
static blockName = 'Image'; |
|
|
static blockEditorSelector = '[aria-label="Block: Image"]'; |
|
|
private editor: EditorComponent; |
|
|
block: ElementHandle; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor( page: Page, block: ElementHandle ) { |
|
|
this.block = block; |
|
|
this.editor = new EditorComponent( page ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async getImage(): Promise< ElementHandle > { |
|
|
return await this.block.waitForSelector( 'img' ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async waitUntilUploaded(): Promise< void > { |
|
|
await Promise.all( [ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.block.waitForSelector( 'img:not([src^="blob:"])' ), |
|
|
this.block.waitForElementState( 'stable' ), |
|
|
] ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async upload( path: string ): Promise< ElementHandle > { |
|
|
const input = await this.block.waitForSelector( 'input[type="file"]', { state: 'attached' } ); |
|
|
await input.setInputFiles( path ); |
|
|
await this.waitUntilUploaded(); |
|
|
|
|
|
return await this.getImage(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async uploadThroughMediaLibrary( path: string ): Promise< ElementHandle > { |
|
|
await this.selectImageSource( 'Media Library' ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const page = ( await this.block.ownerFrame() )?.page() as Page; |
|
|
await page.locator( 'input[type="file"][multiple]' ).setInputFiles( path ); |
|
|
|
|
|
await page.locator( 'button:text-is("Select")' ).click(); |
|
|
|
|
|
return await this.getImage(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async selectImageSource( source: Sources ): Promise< void > { |
|
|
const buttonHandle = await this.block.waitForSelector( selectors.selectImageSourceButton ); |
|
|
await buttonHandle.click(); |
|
|
|
|
|
const editorParent = await this.editor.parent(); |
|
|
await editorParent.locator( selectors.imageSource( source ) ).click(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static async validatePublishedContent( page: Page, contents: string[] ): Promise< void > { |
|
|
for await ( const content of contents ) { |
|
|
await page.waitForSelector( selectors.image( content ) ); |
|
|
} |
|
|
} |
|
|
} |
|
|
|