|
|
import { ElementHandle, Page } from 'playwright'; |
|
|
import { getCalypsoURL } from '../../data-helper'; |
|
|
import { waitForElementEnabled, clickNavTab } from '../../element-helper'; |
|
|
|
|
|
const selectors = { |
|
|
|
|
|
gallery: '.media-library__content', |
|
|
items: ( selected: boolean ) => { |
|
|
if ( selected ) { |
|
|
return '.media-library__list-item.is-selected'; |
|
|
} |
|
|
return '.media-library__list-item'; |
|
|
}, |
|
|
placeholder: '.is-placeholder', |
|
|
uploadSpinner: '.media-library__list-item-spinner', |
|
|
notReadyOverlay: '.is-transient', |
|
|
editButton: 'button[data-e2e-button="edit"]', |
|
|
deleteButton: '.media-library__header button[data-e2e-button="delete"]', |
|
|
fileInput: 'input.media-library__upload-button-input', |
|
|
uploadRejectionNotice: 'text=/could not be uploaded/i', |
|
|
|
|
|
|
|
|
editFileModal: '.editor-media-modal__content', |
|
|
editImageButton: 'button:has-text("Edit Image"):visible', |
|
|
editModalDeleteButton: '.editor-media-modal button:has-text("Delete"):visible', |
|
|
|
|
|
|
|
|
confirmationDeleteButton: '.dialog:has-text("Are you sure") button:has-text("Delete")', |
|
|
|
|
|
|
|
|
imageEditorCanvas: '.image-editor__canvas-container', |
|
|
imageEditorToolbarButton: ( text: string ) => |
|
|
`.image-editor__toolbar-button span:text("${ text }")`, |
|
|
imageEditorResetButton: 'button[data-e2e-button="reset"]', |
|
|
imageEditorCancelButton: 'button[data-e2e-button="cancel"]', |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class MediaPage { |
|
|
private page: Page; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor( page: Page ) { |
|
|
this.page = page; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async visit( siteSlug: string ): Promise< void > { |
|
|
await this.page.goto( getCalypsoURL( `media/${ siteSlug }` ) ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async waitUntilLoaded(): Promise< ElementHandle > { |
|
|
|
|
|
|
|
|
|
|
|
await this.page.waitForSelector( selectors.placeholder, { state: 'hidden' } ); |
|
|
|
|
|
const gallery = await this.page.waitForSelector( selectors.gallery ); |
|
|
await gallery.waitForElementState( 'stable' ); |
|
|
return gallery; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async hasStorageCapacity( capacity: number ): Promise< boolean > { |
|
|
const locator = this.page.locator( |
|
|
`.plan-storage__storage-label:has-text("${ capacity.toString() }")` |
|
|
); |
|
|
try { |
|
|
await locator.waitFor( { timeout: 15 * 1000 } ); |
|
|
return true; |
|
|
} catch { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async selectItem( { index, name }: { index?: number; name?: string } = {} ): Promise< void > { |
|
|
if ( ! index && ! name ) { |
|
|
throw new Error( 'Specify either index or name.' ); |
|
|
} |
|
|
|
|
|
if ( index ) { |
|
|
const elementHandle = await this.page.waitForSelector( |
|
|
`:nth-match(${ selectors.items }, ${ index })` |
|
|
); |
|
|
await elementHandle.click(); |
|
|
await this.page.waitForFunction( |
|
|
( element: SVGElement | HTMLElement ) => element.classList.contains( 'is-selected' ), |
|
|
elementHandle |
|
|
); |
|
|
} |
|
|
if ( name ) { |
|
|
const locator = this.page.locator( selectors.items( false ), { |
|
|
has: this.page.locator( `figure[title="${ name }"]` ), |
|
|
} ); |
|
|
await locator.click(); |
|
|
|
|
|
const selectedLocator = this.page.locator( selectors.items( true ), { |
|
|
has: this.page.locator( `figure[title="${ name }"]` ), |
|
|
} ); |
|
|
await selectedLocator.waitFor(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async clickTab( name: 'All' | 'Images' | 'Documents' | 'Videos' | 'Audio' ): Promise< void > { |
|
|
await this.waitUntilLoaded(); |
|
|
await clickNavTab( this.page, name ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async editSelectedItem(): Promise< void > { |
|
|
|
|
|
try { |
|
|
await this.page.waitForSelector( selectors.items( true ) ); |
|
|
} catch ( error ) { |
|
|
throw new Error( 'Unable to edit files: no item(s) were selected.' ); |
|
|
} |
|
|
|
|
|
await this.page.click( selectors.editButton ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async launchImageEditor(): Promise< void > { |
|
|
await this.page.waitForSelector( selectors.editFileModal ); |
|
|
await this.page.click( selectors.editImageButton ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async rotateImage(): Promise< void > { |
|
|
await this.page.waitForSelector( selectors.imageEditorCanvas ); |
|
|
await this.page.click( selectors.imageEditorToolbarButton( 'Rotate' ) ); |
|
|
await waitForElementEnabled( this.page, selectors.imageEditorResetButton ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async cancelImageEdit(): Promise< void > { |
|
|
await this.page.click( selectors.imageEditorCancelButton ); |
|
|
await this.page.waitForSelector( selectors.editFileModal ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async deleteMediaFromModal(): Promise< void > { |
|
|
const modalDeleteButtonLocator = this.page.locator( selectors.editModalDeleteButton ); |
|
|
await modalDeleteButtonLocator.click(); |
|
|
|
|
|
const confirmationDeleteButtonLocator = this.page.locator( selectors.confirmationDeleteButton ); |
|
|
await confirmationDeleteButtonLocator.click(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async deleteSelectedMediaFromLibrary(): Promise< void > { |
|
|
const deleteButtonLocator = this.page.locator( selectors.deleteButton ); |
|
|
await deleteButtonLocator.click(); |
|
|
|
|
|
const confirmationDeleteButtonLocator = this.page.locator( selectors.confirmationDeleteButton ); |
|
|
await confirmationDeleteButtonLocator.click(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async upload( fullPath: string ): Promise< void > { |
|
|
await this.waitUntilLoaded(); |
|
|
|
|
|
|
|
|
await this.page.setInputFiles( selectors.fileInput, fullPath ); |
|
|
|
|
|
await Promise.all( [ |
|
|
|
|
|
this.page.waitForSelector( `${ selectors.deleteButton }[disabled]`, { |
|
|
state: 'hidden', |
|
|
} ), |
|
|
this.page.waitForSelector( selectors.uploadSpinner, { state: 'hidden' } ), |
|
|
this.page.waitForSelector( selectors.notReadyOverlay, { state: 'hidden' } ), |
|
|
] ); |
|
|
|
|
|
|
|
|
|
|
|
const rejected = await this.page.isVisible( selectors.uploadRejectionNotice ); |
|
|
|
|
|
if ( rejected ) { |
|
|
throw new Error( |
|
|
await this.page |
|
|
.waitForSelector( selectors.uploadRejectionNotice ) |
|
|
.then( ( element ) => element.innerText() ) |
|
|
); |
|
|
} |
|
|
} |
|
|
} |
|
|
|