|
| 1 | +import { APIRequestContext, type Page, type TestInfo, expect } from '@playwright/test'; |
| 2 | +import WpAdminPage from './wp-admin-page.ts'; |
| 3 | + |
| 4 | +export default class SettingsPage extends WpAdminPage { |
| 5 | + async gotoSettingsPage(): Promise<void> { |
| 6 | + await this.page.goto( '/wp-admin/admin.php?page=hello-elementor-settings' ); |
| 7 | + await this.page.waitForSelector( 'h4:has-text("Advanced theme settings")', { timeout: 10000 } ); |
| 8 | + } |
| 9 | + |
| 10 | + getTab( tabName: string ) { |
| 11 | + return this.page.getByRole( 'tab', { name: tabName } ); |
| 12 | + } |
| 13 | + |
| 14 | + getTabPanel( tabName: string ) { |
| 15 | + return this.page.getByRole( 'tabpanel', { name: tabName } ); |
| 16 | + } |
| 17 | + |
| 18 | + async createNewBasicPost(): Promise<string> { |
| 19 | + const request: APIRequestContext = this.page.context().request, |
| 20 | + postDataInitial = { |
| 21 | + title: 'Playwright Test Page - Uninitialized', |
| 22 | + content: 'This is a test content for the post.', |
| 23 | + status: 'publish' as const, |
| 24 | + excerpt: 'This is a test excerpt that should appear in meta description.', |
| 25 | + }, |
| 26 | + postId = await this.apiRequests.create( request, 'pages', postDataInitial ), |
| 27 | + postDataUpdated = { |
| 28 | + title: `Playwright Test Page #${ postId }`, |
| 29 | + }; |
| 30 | + |
| 31 | + await this.apiRequests.create( request, `pages/${ postId }`, postDataUpdated ); |
| 32 | + |
| 33 | + return postId; |
| 34 | + } |
| 35 | + |
| 36 | + async clickTab( tabName: string ): Promise<void> { |
| 37 | + await this.getTab( tabName ).click(); |
| 38 | + await this.page.waitForSelector( `[role="tabpanel"]:visible`, { timeout: 5000 } ); |
| 39 | + } |
| 40 | + |
| 41 | + getCheckboxByIndex( index: number ) { |
| 42 | + return this.page.locator( 'input[type="checkbox"]' ).nth( index ); |
| 43 | + } |
| 44 | + |
| 45 | + getCheckboxBySetting( settingName: string ) { |
| 46 | + const settingMap: { [key: string]: number } = { |
| 47 | + 'Disable description meta tag': 0, |
| 48 | + 'Disable skip links': 1, |
| 49 | + 'Disable theme header and footer': 0, |
| 50 | + 'Hide page title': 1, |
| 51 | + 'Deregister Hello reset.css': 0, |
| 52 | + 'Deregister Hello theme.css': 1, |
| 53 | + }; |
| 54 | + return this.page.locator( `input[type="checkbox"]` ).nth( settingMap[ settingName ] || 0 ); |
| 55 | + } |
| 56 | + |
| 57 | + async toggleSetting( settingName: string ): Promise<boolean> { |
| 58 | + const checkbox = this.getCheckboxBySetting( settingName ); |
| 59 | + await checkbox.click(); |
| 60 | + await this.waitForSaveNotification(); |
| 61 | + return await checkbox.isChecked(); |
| 62 | + } |
| 63 | + |
| 64 | + async toggleCheckboxByIndex( index: number ): Promise<boolean> { |
| 65 | + const checkbox = this.getCheckboxByIndex( index ); |
| 66 | + await checkbox.click(); |
| 67 | + await this.waitForSaveNotification(); |
| 68 | + return await checkbox.isChecked(); |
| 69 | + } |
| 70 | + |
| 71 | + async waitForSaveNotification(): Promise<void> { |
| 72 | + await expect( this.page.locator( '[role="alert"]:has-text("Settings Saved")' ).first() ).toBeVisible( { timeout: 5000 } ); |
| 73 | + } |
| 74 | + |
| 75 | + async closeSaveNotification(): Promise<void> { |
| 76 | + const notification = this.page.locator( '[role="alert"]:has-text("Settings Saved")' ); |
| 77 | + if ( await notification.isVisible() ) { |
| 78 | + await notification.getByRole( 'button', { name: 'Close' } ).click(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + async openChangelog(): Promise<void> { |
| 83 | + await this.page.getByRole( 'link', { name: 'Changelog' } ).click(); |
| 84 | + await expect( this.page.locator( 'h4:has-text("Changelog")' ) ).toBeVisible( { timeout: 5000 } ); |
| 85 | + } |
| 86 | + |
| 87 | + async closeChangelogWithEscape(): Promise<void> { |
| 88 | + await this.page.keyboard.press( 'Escape' ); |
| 89 | + await expect( this.page.locator( '[role="dialog"]' ) ).not.toBeVisible( { timeout: 3000 } ); |
| 90 | + } |
| 91 | + |
| 92 | + async closeChangelogByClickingOutside(): Promise<void> { |
| 93 | + await this.page.locator( '[role="dialog"]' ).click( { position: { x: 5, y: 5 } } ); |
| 94 | + await expect( this.page.locator( '[role="dialog"]' ) ).not.toBeVisible( { timeout: 3000 } ); |
| 95 | + } |
| 96 | + |
| 97 | + async getChangelogVersions(): Promise<string[]> { |
| 98 | + const versionElements = this.page.locator( 'h6[class*="MuiTypography"]:regex("\\d+\\.\\d+\\.\\d+ - \\d{4}-\\d{2}-\\d{2}")' ); |
| 99 | + return await versionElements.allTextContents(); |
| 100 | + } |
| 101 | + |
| 102 | + async hasWarningAlert(): Promise<boolean> { |
| 103 | + return await this.page.locator( '[role="alert"]:has-text("Be Careful")' ).isVisible(); |
| 104 | + } |
| 105 | + |
| 106 | + async getSettingDescription( settingName: string ): Promise<string> { |
| 107 | + const descriptionElement = this.page.locator( `h6:has-text("${ settingName }") ~ * p:has-text("What it does:")` ); |
| 108 | + return await descriptionElement.textContent() || ''; |
| 109 | + } |
| 110 | + |
| 111 | + async getSettingTip( settingName: string ): Promise<string> { |
| 112 | + const tipElement = this.page.locator( `h6:has-text("${ settingName }") ~ * p:has-text("Tip:")` ); |
| 113 | + return await tipElement.textContent() || ''; |
| 114 | + } |
| 115 | + |
| 116 | + async getSettingCode( settingName: string ): Promise<string> { |
| 117 | + const codeElement = this.page.locator( `h6:has-text("${ settingName }") ~ * code` ); |
| 118 | + return await codeElement.textContent() || ''; |
| 119 | + } |
| 120 | + |
| 121 | + async waitForPageLoad(): Promise<void> { |
| 122 | + await this.page.waitForSelector( 'h4:has-text("Advanced theme settings")', { timeout: 10000 } ); |
| 123 | + await this.page.waitForSelector( '[role="tablist"]', { timeout: 5000 } ); |
| 124 | + await this.page.waitForSelector( '[role="tabpanel"]:visible', { timeout: 5000 } ); |
| 125 | + await this.page.waitForTimeout( 1000 ); |
| 126 | + } |
| 127 | + |
| 128 | + async resetToDefaults(): Promise<void> { |
| 129 | + const tabs = [ |
| 130 | + 'SEO and accessibility', |
| 131 | + 'Structure and layout', |
| 132 | + 'CSS and styling control', |
| 133 | + ]; |
| 134 | + |
| 135 | + for ( const tabName of tabs ) { |
| 136 | + await this.clickTab( tabName ); |
| 137 | + |
| 138 | + const checkboxes = this.page.locator( 'input[type="checkbox"]' ); |
| 139 | + const count = await checkboxes.count(); |
| 140 | + |
| 141 | + for ( let i = 0; i < count; i++ ) { |
| 142 | + const checkbox = checkboxes.nth( i ); |
| 143 | + const isChecked = await checkbox.isChecked(); |
| 144 | + |
| 145 | + let shouldBeChecked = false; |
| 146 | + if ( 'Structure and layout' === tabName && 1 === i ) { |
| 147 | + shouldBeChecked = true; |
| 148 | + } |
| 149 | + |
| 150 | + if ( isChecked !== shouldBeChecked ) { |
| 151 | + await checkbox.click(); |
| 152 | + await this.waitForSaveNotification(); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments