| 1 |
import { Locator, type Page } from '@playwright/test'; |
| 2 |
import EditorPage from '../pages/editor-page'; |
| 3 |
import { BreakpointEditableDevice, Device } from '../types/types'; |
| 4 |
import EditorSelectors from '../selectors/editor-selectors'; |
| 5 |
|
| 6 |
export default class { |
| 7 |
readonly page: Page; |
| 8 |
constructor( page: Page ) { |
| 9 |
this.page = page; |
| 10 |
} |
| 11 |
|
| 12 |
static getDeviceLocator( page: Page, device: Device ): Locator { |
| 13 |
const baseLocator = page.locator( '[aria-label="Switch Device"]' ); |
| 14 |
return baseLocator.locator( `[data-testid="switch-device-to-${ device }"]` ); |
| 15 |
} |
| 16 |
|
| 17 |
static getAll(): Device[] { |
| 18 |
return [ 'mobile', 'mobile_extra', 'tablet', 'tablet_extra', 'laptop', 'desktop', 'widescreen' ]; |
| 19 |
} |
| 20 |
|
| 21 |
static getBasic(): Device[] { |
| 22 |
return [ 'mobile', 'tablet', 'desktop' ]; |
| 23 |
} |
| 24 |
|
| 25 |
async saveOrUpdate( editor: EditorPage, toReload = false ) { |
| 26 |
const hasTopBar: boolean = await editor.hasTopBar(); |
| 27 |
if ( hasTopBar ) { |
| 28 |
await editor.saveSiteSettingsWithTopBar( toReload ); |
| 29 |
} else { |
| 30 |
await editor.saveSiteSettingsNoTopBar(); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
async addAllBreakpoints( editor: EditorPage, experimentPostId?: string ) { |
| 35 |
await editor.openSiteSettings( 'settings-layout' ); |
| 36 |
await editor.openSection( 'section_breakpoints' ); |
| 37 |
await this.page.waitForSelector( 'text=Active Breakpoints' ); |
| 38 |
|
| 39 |
const devices = [ 'Mobile Landscape', 'Tablet Landscape', 'Laptop', 'Widescreen' ]; |
| 40 |
|
| 41 |
for ( const device of devices ) { |
| 42 |
if ( await this.page.$( '.select2-selection__e-plus-button' ) ) { |
| 43 |
await this.page.click( '.select2-selection__e-plus-button' ); |
| 44 |
await this.page.click( `li:has-text("${ device }")` ); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
await this.saveOrUpdate( editor, true ); |
| 49 |
|
| 50 |
if ( experimentPostId ) { |
| 51 |
await this.page.goto( `/wp-admin/post.php?post=${ experimentPostId }&action=elementor` ); |
| 52 |
} else { |
| 53 |
await this.page.reload(); |
| 54 |
|
| 55 |
if ( await this.page.$( '#elementor-panel-header-kit-close' ) ) { |
| 56 |
await this.page.locator( '#elementor-panel-header-kit-close' ).click( { timeout: 30000 } ); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
await this.page.waitForSelector( '#elementor-editor-wrapper' ); |
| 61 |
} |
| 62 |
|
| 63 |
async resetBreakpoints( editor: EditorPage ) { |
| 64 |
await editor.openSiteSettings( 'settings-layout' ); |
| 65 |
await editor.openSection( 'section_breakpoints' ); |
| 66 |
await this.page.waitForSelector( 'text=Active Breakpoints' ); |
| 67 |
|
| 68 |
const removeBreakpointButton = EditorSelectors.panels.siteSettings.layout.breakpoints.removeBreakpointButton; |
| 69 |
while ( await this.page.locator( removeBreakpointButton ).count() > 0 ) { |
| 70 |
await this.page.click( removeBreakpointButton ); |
| 71 |
} |
| 72 |
await this.saveOrUpdate( editor, true ); |
| 73 |
} |
| 74 |
|
| 75 |
getBreakpointInputLocator( page: Page, device: BreakpointEditableDevice ): Locator { |
| 76 |
return page.locator( `input[data-setting="viewport_${ device }"]` ); |
| 77 |
} |
| 78 |
|
| 79 |
async setBreakpoint( editor: EditorPage, device: BreakpointEditableDevice, value: number ) { |
| 80 |
await editor.openSiteSettings( 'settings-layout' ); |
| 81 |
await editor.openSection( 'section_breakpoints' ); |
| 82 |
await this.page.waitForSelector( 'text=Active Breakpoints' ); |
| 83 |
|
| 84 |
const locator = this.getBreakpointInputLocator( this.page, device ); |
| 85 |
await locator.fill( String( value ) ); |
| 86 |
await this.saveOrUpdate( editor ); |
| 87 |
await this.page.locator( EditorSelectors.toast ).waitFor(); |
| 88 |
} |
| 89 |
} |
| 90 |
|