File size: 23,645 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
import assert from 'assert';
import { Locator, Page } from 'playwright';
import {
	BlockInserter,
	EditorSidebarBlockInserterComponent,
	EditorToolbarComponent,
	EditorWelcomeTourComponent,
	EditorPopoverMenuComponent,
	EditorSiteStylesComponent,
	ColorSettings,
	TypographySettings,
	ColorLocation,
	FullSiteEditorSavePanelComponent,
	EditorBlockToolbarComponent,
	BlockToolbarButtonIdentifier,
	TemplatePartListComponent,
	FullSiteEditorNavSidebarComponent,
	FullSiteEditorDataViewsComponent,
	TemplatePartModalComponent,
	OpenInlineInserter,
	EditorInlineBlockInserterComponent,
	DimensionsSettings,
	CookieBannerComponent,
	EditorComponent,
} from '..';
import { getIdFromBlock } from '../../element-helper';
import envVariables from '../../env-variables';

const selectors = {
	editorRoot: 'body.block-editor-page',
	editorCanvasIframe: 'iframe[name="editor-canvas"]',
	editorCanvasRoot: 'body.block-editor-iframe__body',
	templateLoadingSpinner: '[aria-label="Block: Template Part"] .components-spinner',
	closeStylesWelcomeGuideButton:
		'[aria-label="Welcome to styles"] button[aria-label="Close dialog"]',
	limitedGlobalStylesModalTryButton: '.wpcom-global-styles-modal__actions button:first-child',
	// The toast can have double quotes, so we use single quotes here.
	confirmationToast: ( text: string ) => `.components-snackbar:has-text('${ text }')`,
	focusedBlock: ( blockSelector: string ) => `${ blockSelector }.is-selected`,
	parentOfFocusedBlock: ( blockSelector: string ) => `${ blockSelector }.has-child-selected`,
	limitedGlobalStylesNotice: '.wpcom-global-styles-notice',
};

/**
 * Represents an instance of the FSE site editor.
 * This class is composed of editor components, combining them into larger flows.
 */
export class FullSiteEditorPage {
	private page: Page;
	private editor: EditorComponent;
	private editorToolbarComponent: EditorToolbarComponent;
	private editorSidebarBlockInserterComponent: EditorSidebarBlockInserterComponent;
	private editorInlineBlockInserterComponent: EditorInlineBlockInserterComponent;
	private editorWelcomeTourComponent: EditorWelcomeTourComponent;
	private editorPopoverMenuComponent: EditorPopoverMenuComponent;
	private editorSiteStylesComponent: EditorSiteStylesComponent;
	private editorBlockToolbarComponent: EditorBlockToolbarComponent;
	private fullSiteEditorSavePanelComponent: FullSiteEditorSavePanelComponent;
	private fullSiteEditorNavSidebarComponent: FullSiteEditorNavSidebarComponent;
	private fullSiteEditorDataViewsComponent: FullSiteEditorDataViewsComponent;
	private templatePartModalComponent: TemplatePartModalComponent;
	private templatePartListComponent: TemplatePartListComponent;
	private cookieBannerComponent: CookieBannerComponent;

	/**
	 * Constructs an instance of the page POM class.
	 *
	 * @param {Page} page The underlying page.
	 */
	constructor( page: Page ) {
		this.page = page;

		this.editor = new EditorComponent( page );

		this.editorToolbarComponent = new EditorToolbarComponent( page, this.editor );
		this.editorWelcomeTourComponent = new EditorWelcomeTourComponent( page, this.editor );
		this.editorPopoverMenuComponent = new EditorPopoverMenuComponent( page, this.editor );
		this.editorSiteStylesComponent = new EditorSiteStylesComponent( page, this.editor );
		this.editorBlockToolbarComponent = new EditorBlockToolbarComponent( page, this.editor );
		this.fullSiteEditorNavSidebarComponent = new FullSiteEditorNavSidebarComponent(
			page,
			this.editor
		);
		this.fullSiteEditorDataViewsComponent = new FullSiteEditorDataViewsComponent(
			page,
			this.editor
		);
		this.editorSidebarBlockInserterComponent = new EditorSidebarBlockInserterComponent(
			page,
			this.editor
		);
		this.editorInlineBlockInserterComponent = new EditorInlineBlockInserterComponent(
			page,
			this.editor
		);
		this.fullSiteEditorSavePanelComponent = new FullSiteEditorSavePanelComponent(
			page,
			this.editor
		);
		this.templatePartModalComponent = new TemplatePartModalComponent( page, this.editor );
		this.templatePartListComponent = new TemplatePartListComponent( page, this.editor );
		this.cookieBannerComponent = new CookieBannerComponent( page, this.editor );
	}

	//#region Visit and Setup

	/**
	 * Visit the site editor by URL directly.
	 *
	 * @param {string} siteHostWithProtocol Host name of the site, with protocol. (e.g. https://testsite.wordpress.com)
	 */
	async visit( siteHostWithProtocol: string ): Promise< void > {
		let parsedUrl: URL;
		try {
			parsedUrl = new URL( siteHostWithProtocol );
		} catch ( error ) {
			throw new Error(
				`Invalid site host URL provided: "${ siteHostWithProtocol }". Did you remember to include the protocol?`
			);
		}

		parsedUrl.pathname = '/wp-admin/site-editor.php';
		parsedUrl.searchParams.set( 'calypso_origin', envVariables.CALYPSO_BASE_URL );

		await this.page.goto( parsedUrl.href, { timeout: 60 * 1000 } );
	}

	/**
	 * Waits until the site editor is fully loaded.
	 */
	async waitUntilLoaded(): Promise< void > {
		const editorCanvas = await this.editor.canvas();
		// But then, template parts load async afterwards!
		const spinnerLocator = editorCanvas.locator( selectors.templateLoadingSpinner );
		// There could be many spinners, so we will keep waiting for the first to be detached.
		await spinnerLocator.first().waitFor( { state: 'detached' } );
	}

	/**
	 * Does all waiting and initial actions to prepare the site editor for interaction.
	 *
	 * @param {Object} param0 Keyed object of options.
	 * @param {boolean} param0.leaveWithoutSaving Set if we should auto-except dialog about unsaved changes when leaving.
	 */
	async prepareForInteraction(
		{
			leaveWithoutSaving,
		}: {
			leaveWithoutSaving?: boolean;
		} = { leaveWithoutSaving: true }
	): Promise< void > {
		// On mobile, we don't load the canvas right away, just the sidebar. So we don't need to wait for the canvas at this point.
		if ( envVariables.VIEWPORT_NAME === 'desktop' ) {
			await this.waitUntilLoaded();
		}

		await this.editorWelcomeTourComponent.forceDismissWelcomeTour();
		await this.cookieBannerComponent.acceptCookie();

		if ( leaveWithoutSaving ) {
			this.page.on( 'dialog', async ( dialog ) => {
				if ( dialog.type() === 'beforeunload' ) {
					await dialog.accept();
				}
			} );
		}
	}

	/**
	 * Clicks on a button with the exact name.
	 */
	async clickFullSiteNavigatorButton( text: string ): Promise< void > {
		await this.fullSiteEditorNavSidebarComponent.clickNavButtonByExactText( text );
	}

	/**
	 * Clicks DataViews primary field to open editor.
	 */
	async openTemplateEditor( text: string ): Promise< void > {
		await this.fullSiteEditorDataViewsComponent.clickPrimaryFieldByExactText( text );
	}

	/**
	 * Ensures the nav sidebar is at the top level ("Design")
	 */
	async ensureNavigationTopLevel(): Promise< void > {
		await this.fullSiteEditorNavSidebarComponent.ensureNavigationTopLevel();
	}

	//#endregion

	//#region Block Actions

	/**
	 * Adds a Gutenberg block from the sidebar block inserter panel.
	 *
	 * The name is expected to be formatted in the same manner as it
	 * appears on the label when visible in the block inserter panel.
	 *
	 * Example:
	 * 		- Click to Tweet
	 * 		- Pay with Paypal
	 * 		- SyntaxHighlighter Code
	 *
	 * @param {string} blockName Name of the block to be inserted.
	 * @param {string} blockEditorSelector Selector to find the top-level element of the added block in the editor.
	 * @throws If the provided selector does not locate the added block correctly.
	 * @returns A frame-safe locator to the top of the block.
	 */
	async addBlockFromSidebar( blockName: string, blockEditorSelector: string ): Promise< Locator > {
		await this.editorToolbarComponent.openBlockInserter();
		await this.addBlockFromInserter( blockName, this.editorSidebarBlockInserterComponent );
		const addedBlockId = await this.getIdOfAddedBlock( blockEditorSelector );

		// Dismiss the block inserter if viewport is larger than mobile to
		// ensure no interference from the block inserter in subsequent actions on the editor.
		// In mobile, the block inserter will auto-close.
		if ( envVariables.VIEWPORT_NAME !== 'mobile' ) {
			await this.editorToolbarComponent.closeBlockInserter();
		}

		const editorCanvas = await this.editor.canvas();
		return editorCanvas.locator( `#${ addedBlockId }` );
	}

	/**
	 * Adds a Gutenberg block from the inline block inserter.
	 *
	 * Because there are so many different ways to open the inline inserter, this function accepts a function to run first
	 * that should open the inserter. This allows specs to get to the inserter in the way they need.
	 *
	 * The block name is expected to be formatted in the same manner as it
	 * appears on the label when visible in the block inserter panel.
	 *
	 * Example:
	 * 		- Click to Tweet
	 * 		- Pay with Paypal
	 * 		- SyntaxHighlighter Code
	 *
	 * The block editor selector should select the top level element of a block in the editor.
	 * For reference, this element will almost always have the ".wp-block" class.
	 * We recommend using the aria-label for the selector, e.g. '[aria-label="Block: Quote"]'.
	 *
	 * @param {string} blockName Name of the block as in inserter results.
	 * @param {string} blockEditorSelector Selector to find the block once added.
	 * @param {OpenInlineInserter} openInlineInserter Function to open the inline inserter.
	 * @throws If the provided selector does not locate the added block correctly.
	 * @returns A frame-safe locator to the top of the block.
	 */
	async addBlockInline(
		blockName: string,
		blockEditorSelector: string,
		openInlineInserter: OpenInlineInserter
	): Promise< Locator > {
		// First, launch the inline inserter in the way expected by the script.
		await openInlineInserter( await this.editor.canvas() ); // This needed button is almost always NOT in the canvas iframe.
		await this.addBlockFromInserter( blockName, this.editorInlineBlockInserterComponent );
		const addedBlockId = await this.getIdOfAddedBlock( blockEditorSelector );
		const editorCanvas = await this.editor.canvas();
		return editorCanvas.locator( `#${ addedBlockId }` );
	}

	/**
	 * Shared submethod to insert a block from a block inserter.
	 *
	 * @param {string} blockName Name of the block.
	 * @param {BlockInserter} inserter A block inserter component.
	 */
	private async addBlockFromInserter(
		blockName: string,
		inserter: BlockInserter
	): Promise< void > {
		await inserter.searchBlockInserter( blockName );
		await inserter.selectBlockInserterResult( blockName );
	}

	/**
	 * Shared submethod to wait for a just-added block and get its block ID.
	 * The ID is the most reliable way to identify the block over time.
	 *
	 * @param {string} blockEditorSelector Selector to find the block once added.
	 * @returns The ID of the recently added block.
	 */
	private async getIdOfAddedBlock( blockEditorSelector: string ): Promise< string > {
		const editorCanvas = await this.editor.canvas();
		// The added block will always either be focused, or will be the parent of a focused block.
		const addedBlockLocator = editorCanvas.locator(
			`${ selectors.focusedBlock( blockEditorSelector ) },${ selectors.parentOfFocusedBlock(
				blockEditorSelector
			) }`
		);
		await addedBlockLocator.waitFor();

		return await getIdFromBlock( addedBlockLocator );
	}

	// TODO: Add this to the Gutenberg component to make it re-usable across editors!
	/**
	 * Focus (select in a way that gives block-specific features) a block in the site editor.
	 *
	 * @param {string} blockSelector A selector that uniquely identifies this block in the editor.
	 */
	async focusBlock( blockSelector: string ): Promise< void >;
	/**
	 * Focus (select in a way that gives block-specific features) a block in the site edito
	 *
	 * @param {Locator} blockLocator The locator to the parent block element.
	 */
	async focusBlock( blockLocator: Locator ): Promise< void >;
	/**
	 * Focus (select in a way that gives block-specific features) a block in the site editor.
	 * This is the overload implementation.
	 *
	 * @param {string|Locator} block A way to locate the block (Locator or selector).
	 */
	async focusBlock( block: string | Locator ): Promise< void > {
		const editorCanvas = await this.editor.canvas();
		let originalBlockLocator: Locator;
		let focusedBlockLocator: Locator;
		if ( typeof block === 'string' ) {
			// It's a selector.
			originalBlockLocator = editorCanvas.locator( block );
			focusedBlockLocator = editorCanvas.locator( selectors.focusedBlock( block ) );
		} else {
			// It's a Locator.
			originalBlockLocator = block; // We can just re-use the Locator.
			// For the focused Locator, we have to append a class. We can't do this with a Locator.
			// So, we need to find the block's ID to use to create a focused locator.
			const blockId = await getIdFromBlock( block );
			focusedBlockLocator = editorCanvas.locator( selectors.focusedBlock( `#${ blockId }` ) );
		}

		// Some blocks are buried within parent blocks that may eat the first click.
		// Sending up to three clicks should be enough.
		for ( let clickAttempt = 1; clickAttempt <= 3; clickAttempt++ ) {
			if ( ( await focusedBlockLocator.count() ) > 0 ) {
				return;
			}
			// TODO -- we could check at this point if we've done the opposite here and selected
			// a child block, and if so click the parent button the toolbar!
			await originalBlockLocator.click();
		}
		// Do one last wait to let any async re-renders go through.
		await focusedBlockLocator.waitFor();
	}

	/**
	 * Click a primary (not buried under a dropdown) button on the block toolbar.
	 *
	 * @param {BlockToolbarButtonIdentifier} indentifier A way to identify the button.
	 */
	async clickBlockToolbarPrimaryButton(
		indentifier: BlockToolbarButtonIdentifier
	): Promise< void > {
		await this.editorBlockToolbarComponent.clickPrimaryButton( indentifier );
	}

	/**
	 * Click an option button (under the three-dots popover menu) on the block toolbar.
	 *
	 * @param {string} optionName The name of the option in the popover menu.
	 */
	async clickBlockToolbarOption( optionName: string ): Promise< void > {
		await this.editorBlockToolbarComponent.clickOptionsButton();
		await this.editorPopoverMenuComponent.clickMenuButton( optionName );
	}

	//#endregion

	//#region Toolbar Actions

	/**
	 * Click the editor undo button.
	 */
	async undo(): Promise< void > {
		await this.editorToolbarComponent.undo();
	}

	/**
	 * Click the editor redo button.
	 */
	async redo(): Promise< void > {
		await this.editorToolbarComponent.redo();
	}

	/**
	 * Save the changes in the full site editor (equivalent of publish).
	 */
	async save(): Promise< void > {
		await this.clearExistingSaveConfirmationToast();
		await this.editorToolbarComponent.saveSiteEditor();
		await this.fullSiteEditorSavePanelComponent.confirmSave();
		await this.waitForConfirmationToast( 'Site updated.' );
	}

	/**
	 * Open the navigation sidebar.
	 */
	async openNavSidebar(): Promise< void > {
		const editorParent = await this.editor.parent();
		const openButton = editorParent.locator( 'a[aria-label="Open Navigation"]' );

		await openButton.click();
	}

	/**
	 * Close the navigation sidebar. To do this, you actually just click on the editor canvas! This only works on desktop.
	 * On mobile, there is not standardized way to close the sidebar.
	 */
	async closeNavSidebar(): Promise< void > {
		if ( envVariables.VIEWPORT_NAME === 'mobile' ) {
			throw new Error(
				'There is no standardized way to close the site editor navigation sidebar on mobile. Navigate to a template or template part instead.'
			);
		}
		const editorParent = await this.editor.parent();
		const editorCanvas = await this.editor.canvas();
		const openButton = editorParent.locator( 'a[aria-label="Open Navigation"]' );

		await Promise.race( [ openButton.waitFor(), editorCanvas.locator( 'body' ).click() ] );
	}

	/**
	 * Click the editor document actions icon.
	 */
	async openDocumentActionsDropdown(): Promise< void > {
		await this.editorToolbarComponent.clickDocumentActionsIcon();
	}

	/**
	 * Click on an item in the document actions dropdown.
	 */
	async clickDocumentActionsDropdownItem( itemSelector: string ): Promise< void > {
		await this.editorToolbarComponent.clickDocumentActionsDropdownItem( itemSelector );
	}

	/**
	 * Click the editor document actions icon.
	 */

	//#endregion

	//#region Site Global Styles

	/**
	 * Opens the site styles sidebar in the site editor.
	 *
	 * @param {Object} param0 Keyed options parameter.
	 * @param {boolean} param0.closeWelcomeGuide Set if should close welcome guide on opening.
	 */
	async openSiteStyles(
		{ closeWelcomeGuide }: { closeWelcomeGuide: boolean } = { closeWelcomeGuide: true }
	): Promise< void > {
		if ( ! ( await this.editorSiteStylesComponent.siteStylesIsOpen() ) ) {
			await this.editorToolbarComponent.openMoreOptionsMenu();

			if ( closeWelcomeGuide ) {
				// The unawaited promise and no-op catch are both intentional here!
				// We want to close the welcome guide if it opens, but not slow down the test if it doesn't.
				// This will effectively register a handler that waits for the welcome guide to close it if it appears
				// but otherwise doesn't affect the following actions.
				const safelyWatchForWelcomeGuide = () =>
					this.closeStylesWelcomeGuide().catch( () => {
						// No-op
					} );
				safelyWatchForWelcomeGuide();
			}
			await this.editorPopoverMenuComponent.clickMenuButton( 'Styles' );
		}
	}

	/**
	 * Closes the site styles welcome guide.
	 */
	private async closeStylesWelcomeGuide(): Promise< void > {
		const editorParent = await this.editor.parent();
		const locator = editorParent.locator( selectors.closeStylesWelcomeGuideButton );
		await locator.click( { timeout: 5 * 1000 } );
	}

	/**
	 * Close the site styles sidebar/panel.
	 */
	async closeSiteStyles(): Promise< void > {
		await this.editorSiteStylesComponent.closeSiteStyles();
	}

	/**
	 * Clicks a navigation menu item/button in the site styles sidebar/panel.
	 *
	 * @param {string} buttonName Name on the menu item/button.
	 */
	async clickStylesMenuButton( buttonName: string ): Promise< void > {
		await this.editorSiteStylesComponent.clickMenuButton( buttonName );
	}

	/**
	 * Returns to the top menu level of the styles sidebar/panel.
	 */
	async returnToStylesTopMenu(): Promise< void > {
		await this.editorSiteStylesComponent.returnToTopMenu();
	}

	/**
	 * Sets a color style setting globaly for the site.
	 * This auto-handles returning to top menu and navigating down.
	 *
	 * @param {ColorLocation} colorLocation What part of the site we are updating the color for.
	 * @param {ColorSettings} colorSettings Settings for the color to set.
	 */
	async setGlobalColorStyle(
		colorLocation: ColorLocation,
		colorSettings: ColorSettings
	): Promise< void > {
		await this.editorSiteStylesComponent.setGlobalColor( colorLocation, colorSettings );
	}

	/**
	 * Sets a typography style for a block.
	 * This auto-handles returning to top menu and navigating down.
	 *
	 * @param {string} blockName Block name (as appears in list).
	 * @param {TypographySettings} typographySettings Typography settings to set.
	 */
	async setBlockTypographyStyle(
		blockName: string,
		typographySettings: TypographySettings
	): Promise< void > {
		await this.editorSiteStylesComponent.setBlockTypography( blockName, typographySettings );
	}

	/**
	 * Set global layout settings for the site.
	 * Note that only the "Padding" dimension is available globally.
	 * This auto-handles returning to top menu and navigating down.
	 *
	 * @param {DimensionsSettings} dimensionsSettings The dimensions settings to set.
	 */
	async setGlobalLayoutStyle( dimensionsSettings: DimensionsSettings ): Promise< void > {
		await this.editorSiteStylesComponent.setGlobalLayout( dimensionsSettings );
	}

	/**
	 * Resets the global layout style to the layout defaults (empty).
	 */
	async resetGlobalLayoutStyle(): Promise< void > {
		await this.editorSiteStylesComponent.resetGlobalLayout();
	}

	/**
	 * Resets the site styles to the defaults for the theme.
	 */
	async resetStylesToDefaults(): Promise< void > {
		await this.editorSiteStylesComponent.openMoreActionsMenu();
		await this.editorPopoverMenuComponent.clickMenuButton( 'Reset to defaults' );
	}

	/**
	 * Selects the "Try it out" option on the Limited Global Styles upgrade modal.
	 */
	async tryGlobalStyles(): Promise< void > {
		const editorParent = await this.editor.parent();
		const locator = editorParent.locator( selectors.limitedGlobalStylesModalTryButton );
		await locator.click();
	}

	/**
	 * Sets a style variation for the site.
	 * This auto-handles returning to top menu and navigating down.
	 *
	 * @param {string} styleVariationName The name of the style variation to set.
	 */
	async setStyleVariation( styleVariationName: string ): Promise< void > {
		await this.fullSiteEditorNavSidebarComponent.setStyleVariation( styleVariationName );
		const hasCustomStyles = styleVariationName !== 'Default';
		const editorParent = await this.editor.parent();
		const limitedGlobalStylesNotice = editorParent.locator( selectors.limitedGlobalStylesNotice );
		if ( hasCustomStyles ) {
			await limitedGlobalStylesNotice.waitFor();
		} else {
			const count = await limitedGlobalStylesNotice.count();
			assert.equal( count, 0 );
		}
	}

	//#endregion

	//#region Template Parts

	/**
	 * Names and creates a template part from the creation modal.
	 * Because that modal can come from a variety of flows,
	 * this method assumes you have launched the modal first.
	 *
	 * @param {string} name Name to use for the template part.
	 */
	async nameAndFinalizeTemplatePart( name: string ): Promise< void > {
		await this.templatePartModalComponent.enterTemplateName( name );
		await this.templatePartModalComponent.clickCreate();
	}

	/**
	 * Select an existing template part from the template part selection modal.
	 * Because that modal can come from a variety of flows,
	 * this method assumes you have launched the modal first.
	 *
	 * @param {string} name Name of the template part to select.
	 */
	async selectExistingTemplatePartFromModal( name: string ): Promise< void > {
		await this.templatePartModalComponent.selectExistingTemplatePart( name );
		// This toast always fires for all insertertions done from this modal.
		await this.waitForConfirmationToast( `Template Part "${ name }" inserted.` );
	}

	/**
	 * Delete a template part in the site editor.
	 *
	 * @param {string[]} names Name of the template part.
	 */
	async deleteTemplateParts( names: string[] ): Promise< void > {
		await this.openNavSidebar();
		await this.fullSiteEditorNavSidebarComponent.navigateToTemplatePartsManager();
		for ( const name of names ) {
			await this.templatePartListComponent.deleteTemplatePart( name );
			await this.waitForConfirmationToast( `"${ name }" deleted.` );
		}
	}

	//#endregion

	//#region Misc

	/**
	 * Waits for a confirmation toast to appear with the provided text.
	 *
	 * @param {string} text The text we expect on the confirmation toast.
	 */
	async waitForConfirmationToast( text: string ): Promise< void > {
		const editorParent = await this.editor.parent();
		const locator = editorParent.locator( selectors.confirmationToast( text ) );
		await locator.waitFor();
	}

	/**
	 * Clears existing save confirmation toasts.
	 */
	private async clearExistingSaveConfirmationToast(): Promise< void > {
		const editorParent = await this.editor.parent();
		const toastLocator = editorParent.locator( selectors.confirmationToast( 'Site updated.' ) );
		if ( ( await toastLocator.count() ) > 0 ) {
			await toastLocator.click();
		}
	}

	//#endregion
}