UNPKG

@wordpress/e2e-test-utils

Version:
125 lines (115 loc) 4.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.disableSiteEditorWelcomeGuide = disableSiteEditorWelcomeGuide; exports.enterEditMode = enterEditMode; exports.getCurrentSiteEditorContent = getCurrentSiteEditorContent; exports.openGlobalStylesPanel = openGlobalStylesPanel; exports.openPreviousGlobalStylesPanel = openPreviousGlobalStylesPanel; exports.toggleGlobalStyles = toggleGlobalStyles; exports.visitSiteEditor = visitSiteEditor; var _e2eTestUtils = require("@wordpress/e2e-test-utils"); var _url = require("@wordpress/url"); /** * WordPress dependencies */ /** * @typedef {import('puppeteer-core').ElementHandle} ElementHandle */ const SELECTORS = { visualEditor: '.edit-site-visual-editor iframe', canvasLoader: '.edit-site-canvas-loader' }; /** * Skips the welcome guide popping up to first time users of the site editor */ async function disableSiteEditorWelcomeGuide() { await page.evaluate(() => { window.wp.data.dispatch('core/preferences').set('core/edit-site', 'welcomeGuide', false); window.wp.data.dispatch('core/preferences').set('core/edit-site', 'welcomeGuideStyles', false); window.wp.data.dispatch('core/preferences').set('core/edit-site', 'welcomeGuidePage', false); window.wp.data.dispatch('core/preferences').set('core/edit-site', 'welcomeGuideTemplate', false); }); } /** * Returns a promise which resolves with the edited post content (HTML string). * * @return {Promise<string>} Promise resolving with post content markup. */ function getCurrentSiteEditorContent() { return page.evaluate(() => { const postId = window.wp.data.select('core/editor').getCurrentPostId(); const postType = window.wp.data.select('core/editor').getCurrentPostType(); const record = window.wp.data.select('core').getEditedEntityRecord('postType', postType, postId); if (record) { if (typeof record.content === 'function') { return record.content(record); } else if (record.blocks) { return window.wp.blocks.__unstableSerializeAndClean(record.blocks); } else if (record.content) { return record.content; } } return ''; }); } /** * Visits the Site Editor main page * * By default, it also skips the welcome guide. The option can be disabled if need be. * * @see disableSiteEditorWelcomeGuide * * @param {string} query String to be serialized as query portion of URL. * @param {boolean} [skipWelcomeGuide=true] Whether to skip the welcome guide as part of the navigation. */ async function visitSiteEditor(query, skipWelcomeGuide = true) { query = (0, _url.addQueryArgs)('', { ...query }).slice(1); await (0, _e2eTestUtils.visitAdminPage)('site-editor.php', query); await page.waitForSelector(SELECTORS.visualEditor); await page.waitForSelector(SELECTORS.canvasLoader, { hidden: true }); if (skipWelcomeGuide) { await disableSiteEditorWelcomeGuide(); } } /** * Toggles the global styles sidebar (opens it if closed and closes it if open). */ async function toggleGlobalStyles() { await page.click('.editor-header__settings button[aria-label="Styles"]'); } /** * Opens a global styles panel. * * @param {string} panelName Name of the panel that is going to be opened. */ async function openGlobalStylesPanel(panelName) { const selector = `//div[@aria-label="Editor settings"]//button[.//*[text()="${panelName}"]]`; await (await page.waitForXPath(selector)).click(); } /** * Opens the previous global styles panel. */ async function openPreviousGlobalStylesPanel() { await page.click('div[aria-label="Editor settings"] button[aria-label="Back"]'); } /** * Enters edit mode. */ async function enterEditMode() { try { await page.waitForSelector('.edit-site-visual-editor__editor-canvas[role="button"]', { timeout: 3000 }); await (0, _e2eTestUtils.canvas)().click('body'); } catch { // This catch is necessary for the performance tests in old branches // where the site editor toggle was not implemented yet. } } //# sourceMappingURL=site-editor.js.map