|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import { E2ESession } from './e2e-helpers'; |
| 3 | +import AddSiteModal from './page-objects/add-site-modal'; |
| 4 | +import MainSidebar from './page-objects/main-sidebar'; |
| 5 | +import Onboarding from './page-objects/onboarding'; |
| 6 | +import SiteContent from './page-objects/site-content'; |
| 7 | +import WhatsNewModal from './page-objects/whats-new-modal'; |
| 8 | +import { getUrlWithAutoLogin } from './utils'; |
| 9 | + |
| 10 | +test.describe( 'Localization', () => { |
| 11 | + const session = new E2ESession(); |
| 12 | + |
| 13 | + // Helper function to open settings using data-testid |
| 14 | + const openSettings = async ( page: typeof session.mainWindow ) => { |
| 15 | + const settingsButton = page.getByTestId( 'settings-button' ); |
| 16 | + await expect( settingsButton ).toBeVisible(); |
| 17 | + await settingsButton.click(); |
| 18 | + }; |
| 19 | + |
| 20 | + // Helper to change language |
| 21 | + const changeLanguage = async ( page: typeof session.mainWindow, localeCode: string ) => { |
| 22 | + await openSettings( page ); |
| 23 | + |
| 24 | + // Wait for Settings modal |
| 25 | + const settingsModal = page.getByRole( 'dialog' ); |
| 26 | + await expect( settingsModal ).toBeVisible( { timeout: 10_000 } ); |
| 27 | + |
| 28 | + // Select language using data-testid |
| 29 | + const languageSelect = page.getByTestId( 'language-select' ); |
| 30 | + await expect( languageSelect ).toBeVisible( { timeout: 10_000 } ); |
| 31 | + await languageSelect.selectOption( localeCode ); |
| 32 | + |
| 33 | + // Click Save button using data-testid |
| 34 | + const saveButton = page.getByTestId( 'preferences-save-button' ); |
| 35 | + await expect( saveButton ).toBeVisible(); |
| 36 | + await expect( saveButton ).toBeEnabled(); // Wait for it to be enabled |
| 37 | + await saveButton.click(); |
| 38 | + |
| 39 | + // Wait for modal to close |
| 40 | + await expect( settingsModal ).not.toBeVisible( { timeout: 5_000 } ); |
| 41 | + }; |
| 42 | + |
| 43 | + test.beforeAll( async () => { |
| 44 | + await session.launch(); |
| 45 | + |
| 46 | + // Complete onboarding before tests |
| 47 | + const onboarding = new Onboarding( session.mainWindow ); |
| 48 | + await expect( onboarding.heading ).toBeVisible(); |
| 49 | + await onboarding.continueButton.click(); |
| 50 | + |
| 51 | + const whatsNewModal = new WhatsNewModal( session.mainWindow ); |
| 52 | + if ( await whatsNewModal.locator.isVisible( { timeout: 5000 } ) ) { |
| 53 | + await whatsNewModal.closeButton.click(); |
| 54 | + } |
| 55 | + |
| 56 | + const siteContent = new SiteContent( session.mainWindow, 'My WordPress Website' ); |
| 57 | + await expect( siteContent.siteNameHeading ).toBeVisible( { timeout: 120_000 } ); |
| 58 | + } ); |
| 59 | + |
| 60 | + test.afterAll( async () => { |
| 61 | + await session.cleanup(); |
| 62 | + } ); |
| 63 | + |
| 64 | + test( 'changes language from settings', async () => { |
| 65 | + const sidebar = new MainSidebar( session.mainWindow ); |
| 66 | + |
| 67 | + await changeLanguage( session.mainWindow, 'fr' ); |
| 68 | + await expect( sidebar.addSiteButton ).toHaveText( 'Ajouter un site' ); |
| 69 | + await changeLanguage( session.mainWindow, 'en' ); |
| 70 | + await expect( sidebar.addSiteButton ).toHaveText( 'Add site' ); |
| 71 | + } ); |
| 72 | + |
| 73 | + test( 'supports RTL languages', async () => { |
| 74 | + const sidebar = new MainSidebar( session.mainWindow ); |
| 75 | + |
| 76 | + await changeLanguage( session.mainWindow, 'ar' ); |
| 77 | + const htmlDir = await session.mainWindow.evaluate( () => document.documentElement.dir ); |
| 78 | + expect( htmlDir ).toBe( 'rtl' ); |
| 79 | + await expect( sidebar.addSiteButton ).toHaveText( 'إضافة موقع' ); |
| 80 | + await changeLanguage( session.mainWindow, 'en' ); |
| 81 | + |
| 82 | + const htmlDirEnglish = await session.mainWindow.evaluate( () => document.documentElement.dir ); |
| 83 | + expect( htmlDirEnglish ).toBe( 'ltr' ); |
| 84 | + await expect( sidebar.addSiteButton ).toHaveText( 'Add site' ); |
| 85 | + } ); |
| 86 | + |
| 87 | + test( 'persists selected language when re-opening app', async () => { |
| 88 | + const sidebar = new MainSidebar( session.mainWindow ); |
| 89 | + |
| 90 | + await changeLanguage( session.mainWindow, 'de' ); |
| 91 | + await expect( sidebar.addSiteButton ).toHaveText( 'Website hinzufügen' ); |
| 92 | + await session.restart(); |
| 93 | + await session.mainWindow.waitForLoadState( 'domcontentloaded' ); |
| 94 | + |
| 95 | + const onboarding = new Onboarding( session.mainWindow ); |
| 96 | + try { |
| 97 | + const visible = await onboarding.heading.isVisible( { timeout: 2000 } ); |
| 98 | + if ( visible ) { |
| 99 | + await onboarding.continueButton.click(); |
| 100 | + } |
| 101 | + } catch ( error ) { |
| 102 | + // Onboarding not visible, continue with test |
| 103 | + } |
| 104 | + |
| 105 | + const whatsNewModal = new WhatsNewModal( session.mainWindow ); |
| 106 | + try { |
| 107 | + const visible = await whatsNewModal.locator.isVisible( { timeout: 2000 } ); |
| 108 | + if ( visible ) { |
| 109 | + await whatsNewModal.closeButton.click(); |
| 110 | + } |
| 111 | + } catch ( error ) { |
| 112 | + // What's New modal not visible, continue with test |
| 113 | + } |
| 114 | + |
| 115 | + const siteContent = new SiteContent( session.mainWindow, 'My WordPress Website' ); |
| 116 | + await expect( siteContent.siteNameHeading ).toBeVisible( { timeout: 120_000 } ); |
| 117 | + |
| 118 | + const sidebarAfterRestart = new MainSidebar( session.mainWindow ); |
| 119 | + await expect( sidebarAfterRestart.addSiteButton ).toHaveText( 'Website hinzufügen' ); |
| 120 | + |
| 121 | + await changeLanguage( session.mainWindow, 'en' ); |
| 122 | + } ); |
| 123 | + |
| 124 | + test( 'created site language matches Studio language', async ( { page } ) => { |
| 125 | + const siteName = 'Localized-Site-Test'; |
| 126 | + const sidebar = new MainSidebar( session.mainWindow ); |
| 127 | + const addSiteModal = new AddSiteModal( session.mainWindow ); |
| 128 | + |
| 129 | + // Change to Japanese |
| 130 | + await changeLanguage( session.mainWindow, 'ja' ); |
| 131 | + |
| 132 | + // Verify language changed |
| 133 | + await expect( sidebar.addSiteButton ).toHaveText( 'サイトを追加' ); |
| 134 | + |
| 135 | + // Open add site modal |
| 136 | + await sidebar.addSiteButton.click(); |
| 137 | + await expect( addSiteModal.locator ).toBeVisible( { timeout: 5000 } ); |
| 138 | + |
| 139 | + // Click "Create a site" option using data-testid |
| 140 | + await expect( addSiteModal.createSiteButton ).toBeVisible(); |
| 141 | + // Button contains both title and description, so we check it contains the title |
| 142 | + await expect( addSiteModal.createSiteButton ).toContainText( 'サイトを作成' ); |
| 143 | + await addSiteModal.createSiteButton.click(); |
| 144 | + |
| 145 | + // Fill in site name using data-testid |
| 146 | + await expect( addSiteModal.siteNameInput ).toBeVisible( { timeout: 5000 } ); |
| 147 | + await addSiteModal.siteNameInput.fill( siteName ); |
| 148 | + |
| 149 | + // Click "Add site" button using data-testid (wait for it to be enabled) |
| 150 | + await expect( addSiteModal.addSiteButton ).toBeVisible(); |
| 151 | + await expect( addSiteModal.addSiteButton ).toContainText( 'サイトを追加' ); |
| 152 | + await expect( addSiteModal.addSiteButton ).toBeEnabled(); |
| 153 | + await addSiteModal.addSiteButton.click(); |
| 154 | + |
| 155 | + // Wait for modal to close |
| 156 | + await expect( addSiteModal.locator ).not.toBeVisible( { timeout: 10_000 } ); |
| 157 | + |
| 158 | + // Wait for site to be created |
| 159 | + const siteContent = new SiteContent( session.mainWindow, siteName ); |
| 160 | + await expect( siteContent.runningButton ).toBeAttached( { timeout: 120_000 } ); |
| 161 | + |
| 162 | + const settingsTabButton = session.mainWindow.getByRole( 'tab', { name: /Settings|設定/i } ); |
| 163 | + await settingsTabButton.click(); |
| 164 | + const copyWpAdminButton = session.mainWindow.getByTestId( 'copy-wp-admin-url' ); |
| 165 | + await expect( copyWpAdminButton ).toBeVisible(); |
| 166 | + await copyWpAdminButton.click(); |
| 167 | + const wpAdminUrl = await session.electronApp.evaluate( ( app ) => app.clipboard.readText() ); |
| 168 | + |
| 169 | + // Check WordPress site language |
| 170 | + // Use getUrlWithAutoLogin to automatically authenticate with WordPress admin |
| 171 | + // This works cross-platform by appending authentication parameters to the URL |
| 172 | + const optionsGeneralUrl = wpAdminUrl + '/options-general.php'; |
| 173 | + await page.goto( getUrlWithAutoLogin( optionsGeneralUrl ) ); |
| 174 | + |
| 175 | + const languageSelect = page.locator( '#WPLANG' ); |
| 176 | + const selectedLanguage = await languageSelect.inputValue(); |
| 177 | + |
| 178 | + expect( selectedLanguage ).toBe( 'ja' ); |
| 179 | + |
| 180 | + await changeLanguage( session.mainWindow, 'en' ); |
| 181 | + await session.electronApp.evaluate( ( { dialog } ) => { |
| 182 | + dialog.showMessageBox = async () => { |
| 183 | + return { response: 0, checkboxChecked: true }; |
| 184 | + }; |
| 185 | + } ); |
| 186 | + |
| 187 | + const siteContentEnglish = new SiteContent( session.mainWindow, siteName ); |
| 188 | + const settingsTab = await siteContentEnglish.navigateToTab( 'Settings' ); |
| 189 | + await settingsTab.openDeleteSiteModal(); |
| 190 | + |
| 191 | + // Wait for the confirmation dialog to be auto-confirmed and deletion to complete |
| 192 | + // Verify site was deleted by checking it no longer appears in sidebar |
| 193 | + const deletedSite = session.mainWindow.getByRole( 'button', { name: siteName, exact: true } ); |
| 194 | + await expect( deletedSite ).not.toBeVisible( { timeout: 30_000 } ); |
| 195 | + } ); |
| 196 | +} ); |
0 commit comments