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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
import { Page, ElementHandle, Response, Locator } from 'playwright';
import { getCalypsoURL } from '../../data-helper';
import { reloadAndRetry } from '../../element-helper';
import envVariables from '../../env-variables';
import {
	EditorComponent,
	EditorPublishPanelComponent,
	EditorToolbarComponent,
	EditorSettingsSidebarComponent,
	EditorGutenbergComponent,
	NavbarComponent,
	EditorBlockListViewComponent,
	EditorInlineBlockInserterComponent,
	EditorSidebarBlockInserterComponent,
	EditorWelcomeTourComponent,
	EditorWelcomeGuideComponent,
	EditorBlockToolbarComponent,
	EditorPopoverMenuComponent,
	BlockToolbarButtonIdentifier,
	CookieBannerComponent,
	EditorToolbarSettingsButton,
} from '../components';
import { BlockInserter, OpenInlineInserter } from './shared-types';
import type {
	EditorPreviewOptions,
	EditorSidebarTab,
	ArticlePrivacyOptions,
	ArticlePublishSchedule,
} from '../components/types';

const selectors = {
	// Editor
	editorTitle: '.editor-post-title__input',

	// Within the editor body.
	blockWarning: '.block-editor-warning',

	// Toast
	toastViewPostLink: '.components-snackbar__content a:text-matches("View (Post|Page)", "i")',

	// Welcome tour
	welcomeTourCloseButton: 'button[aria-label="Close Tour"]',
};

/**
 * Represents an instance of the WPCOM's Gutenberg editor page.
 */
export class EditorPage {
	private page: Page;
	private editor: EditorComponent;
	private editorPublishPanelComponent: EditorPublishPanelComponent;
	private editorToolbarComponent: EditorToolbarComponent;
	private editorSettingsSidebarComponent: EditorSettingsSidebarComponent;
	private editorGutenbergComponent: EditorGutenbergComponent;
	private editorBlockListViewComponent: EditorBlockListViewComponent;
	private editorSidebarBlockInserterComponent: EditorSidebarBlockInserterComponent;
	private editorInlineBlockInserterComponent: EditorInlineBlockInserterComponent;
	private editorWelcomeTourComponent: EditorWelcomeTourComponent;
	private editorWelcomeGuideComponent: EditorWelcomeGuideComponent;
	private editorBlockToolbarComponent: EditorBlockToolbarComponent;
	private editorPopoverMenuComponent: EditorPopoverMenuComponent;
	private cookieBannerComponent: CookieBannerComponent;

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

		this.editor = new EditorComponent( page );

		this.editorGutenbergComponent = new EditorGutenbergComponent( page, this.editor );
		this.editorToolbarComponent = new EditorToolbarComponent( page, this.editor );
		this.editorSettingsSidebarComponent = new EditorSettingsSidebarComponent( page, this.editor );
		this.editorPublishPanelComponent = new EditorPublishPanelComponent( page, this.editor );
		this.editorBlockListViewComponent = new EditorBlockListViewComponent( page, this.editor );
		this.editorWelcomeTourComponent = new EditorWelcomeTourComponent( page, this.editor );
		this.editorWelcomeGuideComponent = new EditorWelcomeGuideComponent( page, this.editor );
		this.editorBlockToolbarComponent = new EditorBlockToolbarComponent( page, this.editor );
		this.editorSidebarBlockInserterComponent = new EditorSidebarBlockInserterComponent(
			page,
			this.editor
		);
		this.editorInlineBlockInserterComponent = new EditorInlineBlockInserterComponent(
			page,
			this.editor
		);
		this.editorPopoverMenuComponent = new EditorPopoverMenuComponent( page, this.editor );
		this.cookieBannerComponent = new CookieBannerComponent( page, this.editor );
	}

	//#region Generic and Shell Methods

	/**
	 * Opens the "new post/page" page. By default it will open the "new post" page.
	 *
	 * Example "new post": {@link https://wordpress.com/post}
	 * Example "new page": {@link https://wordpress.com/page}
	 */
	async visit(
		type: 'post' | 'page' = 'post',
		{ siteSlug = '' }: { siteSlug?: string } = {}
	): Promise< Response | null > {
		const request = await this.page.goto( getCalypsoURL( `${ type }/${ siteSlug }` ), {
			timeout: 30 * 1000,
		} );
		await this.waitUntilLoaded();

		return request;
	}

	/**
	 * Initialization steps to ensure the page is fully loaded.
	 */
	async waitUntilLoaded(): Promise< void > {
		// When the WordPress version updates on Jetpack AT sites,
		// `wp-beta` and`wp-previous` require a database update.
		// @see https://github.com/Automattic/wp-calypso/issues/82412
		const databaseUpdateMaybeRequired =
			envVariables.ATOMIC_VARIATION === 'wp-beta' ||
			envVariables.ATOMIC_VARIATION === 'wp-previous';

		if ( databaseUpdateMaybeRequired ) {
			const loadEditorWithDatabaseUpdate = async () => {
				await this.acceptDatabaseUpdate();
				await this.waitForEditorLoadedRequests( 30 * 1000 );
			};
			await Promise.race( [
				loadEditorWithDatabaseUpdate(),
				this.waitForEditorLoadedRequests( 60 * 1000 ),
			] );
		} else {
			await this.waitForEditorLoadedRequests( 60 * 1000 );
		}

		// Dismiss the Welcome Tour.
		await this.editorWelcomeTourComponent.forceDismissWelcomeTour();

		// Accept the Cookie banner.
		await this.cookieBannerComponent.acceptCookie();
	}

	/**
	 * Waits for the editor to be fully loaded by keying off of requests.
	 *
	 * @param {number} timeout Timeout for waiting for the final requests.
	 */
	private async waitForEditorLoadedRequests( timeout: number = 60 * 1000 ): Promise< void > {
		await this.page.waitForURL( /(\/post\/.+|\/page\/+|\/post-new.php|\/post.php+)/, { timeout } );
	}

	/**
	 * Accepts the WordPress version database update prompt that can happen on lagging AT sites.
	 */
	private async acceptDatabaseUpdate(): Promise< void > {
		const databaseUpdateButton = this.page.getByRole( 'link', {
			name: 'Update WordPress Database',
		} );
		await databaseUpdateButton.click( { timeout: 30 * 1000 } );
	}

	/**
	 * Resolves with the Editor parent element locator.
	 */
	async getEditorParent() {
		return await this.editor.parent();
	}

	/**
	 * Resolves with the Editor canvas element locator.
	 *
	 * You *must* use this method if you want to select an element inside the canvas
	 * iframe. This already takes into account the parent wrapper element, so
	 * there's *no* need to to chain `getEditorParent()` before calling it.
	 */
	async getEditorCanvas() {
		return await this.editor.canvas();
	}

	/**
	 * Closes all panels that can be opened in the editor.
	 *
	 * This method will attempt to close the following panels:
	 * 	- Publish Panel (including pre-publish checklist)
	 * 	- Editor Settings Panel
	 */
	async closeAllPanels(): Promise< void > {
		await Promise.allSettled( [
			this.editorPublishPanelComponent.closePanel(),
			this.editorToolbarComponent.closeSettings(),
		] );
	}

	//#endregion

	//#region Basic Entry

	/**
	 * Select a template from the grid of options.
	 *
	 * @param {string} label Label for the template (the string underneath the preview).
	 * @param param1 Keyed object parameter.
	 * @param {number} param1.timeout Timeout to apply.
	 */
	async selectTemplate(
		label: string,
		{ timeout = envVariables.TIMEOUT }: { timeout?: number } = {}
	) {
		const editor = await this.getEditorParent();
		const inserterSelector = await editor.getByRole( 'listbox', { name: 'All' } );
		const modalSelector = await editor.getByRole( 'listbox', {
			name: 'Block patterns',
		} );
		return await inserterSelector
			.or( modalSelector )
			.getByRole( 'option', { name: label, exact: true } )
			.first()
			.click( { timeout: timeout } );
	}

	/**
	 * Enters the text into the title block and verifies the result.
	 *
	 * @param {string} title Text to be used as the title.
	 * @throws {Error} If entered title does not match.
	 */
	async enterTitle( title: string ): Promise< void > {
		await this.editorGutenbergComponent.enterTitle( title );
		const enteredTitle = await this.editorGutenbergComponent.getTitle();

		const sanitizedTitle = title.trim();
		if ( enteredTitle !== sanitizedTitle ) {
			throw new Error(
				`Failed to verify title: got ${ enteredTitle }, expected ${ sanitizedTitle }`
			);
		}
	}

	/**
	 * Enters text into the body, splitting newlines into new pragraph blocks as necessary. The entered text is then read back and checked.
	 *
	 * @param {string} text Text to be entered into the paragraph blocks, separated by newline characters.
	 */
	async enterText( text: string ): Promise< void > {
		await this.editorGutenbergComponent.enterText( text );
		const enteredText = await this.editorGutenbergComponent.getText();

		if ( text !== enteredText ) {
			`Failed to verify entered text: got ${ enteredText }, expected ${ text }`;
		}
	}

	/**
	 * Returns the text found in the editor.
	 *
	 * @returns {Promise<string>} String representing text entered in each paragraph block.
	 */
	async getText(): Promise< string > {
		return await this.editorGutenbergComponent.getText();
	}

	//#endregion

	//#region Block and Pattern Insertion

	/**
	 * 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
	 *
	 * 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 to be inserted.
	 */
	async addBlockFromSidebar(
		blockName: string,
		blockEditorSelector: string,
		{
			noSearch,
			blockFallBackName,
			blockInsertedPopupConfirmButtonSelector,
		}: {
			noSearch?: boolean;
			blockFallBackName?: string;
			blockInsertedPopupConfirmButtonSelector?: string;
		} = {}
	): Promise< ElementHandle > {
		await this.editorGutenbergComponent.resetSelectedBlock();
		await this.editorToolbarComponent.openBlockInserter();
		await this.addBlockFromInserter( blockName, this.editorSidebarBlockInserterComponent, {
			noSearch: noSearch,
			blockFallBackName: blockFallBackName,
			blockInsertedPopupConfirmButtonSelector: blockInsertedPopupConfirmButtonSelector,
		} );

		const blockHandle =
			await this.editorGutenbergComponent.getSelectedBlockElementHandle( 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 typically auto-close, but sometimes there can
		// be issues with auto-close.
		if ( envVariables.VIEWPORT_NAME !== 'mobile' ) {
			await this.editorToolbarComponent.closeBlockInserter();
		} else {
			await this.editorSidebarBlockInserterComponent.closeBlockInserter();
		}

		// Return an ElementHandle pointing to the block for compatibility
		// with existing specs.
		return blockHandle;
	}

	/**
	 * 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.
	 * @returns An element handle to the added block.
	 */
	async addBlockInline(
		blockName: string,
		blockEditorSelector: string,
		openInlineInserter: OpenInlineInserter
	): Promise< ElementHandle > {
		// First, launch the inline inserter in the way expected by the script.
		await openInlineInserter( await this.editor.canvas() );
		await this.addBlockFromInserter( blockName, this.editorInlineBlockInserterComponent );

		const blockHandle =
			await this.editorGutenbergComponent.getSelectedBlockElementHandle( blockEditorSelector );
		// Return an ElementHandle pointing to the block for compatibility
		// with existing specs.
		return blockHandle;
	}

	/**
	 * 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,
		{
			noSearch,
			blockFallBackName,
			blockInsertedPopupConfirmButtonSelector,
		}: {
			noSearch?: boolean;
			blockFallBackName?: string;
			blockInsertedPopupConfirmButtonSelector?: string;
		} = {}
	): Promise< Locator > {
		if ( ! noSearch ) {
			await inserter.searchBlockInserter( blockName );
		}

		const locator = await inserter.selectBlockInserterResult( blockName, { blockFallBackName } );

		if ( blockInsertedPopupConfirmButtonSelector ) {
			const editorParent = await this.editor.parent();
			const blockInsertedPopupConfirmButtonLocator = editorParent.locator(
				blockInsertedPopupConfirmButtonSelector
			);

			// Whether the popup confirm button is not deterministic.
			// If it is not present, exit early.
			try {
				await blockInsertedPopupConfirmButtonLocator.waitFor( { timeout: 100 } );
			} catch ( e ) {
				// Probably doesn't exist. That's ok.
			}

			if ( ( await blockInsertedPopupConfirmButtonLocator.count() ) > 0 ) {
				await blockInsertedPopupConfirmButtonLocator.click();
			}
		}

		return locator;
	}

	/**
	 * Adds a pattern 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.
	 * When exactMatch is false, it will select the first pattern that
	 * contains the given name in its aria-label.
	 *
	 * Example:
	 * 		- Two images side by side
	 *
	 * @param {string} patternName Name of the pattern to insert.
	 * @param {boolean} exactMatch Whether to use exact matching for pattern names.
	 */
	async addPatternFromSidebar(
		patternName: string,
		exactMatch: boolean = true
	): Promise< Locator > {
		if ( ! ( envVariables.TEST_ON_ATOMIC && envVariables.VIEWPORT_NAME === 'mobile' ) ) {
			await this.editorGutenbergComponent.resetSelectedBlock();
		}
		await this.editorToolbarComponent.openBlockInserter();
		return await this.addPatternFromInserter(
			patternName,
			this.editorSidebarBlockInserterComponent,
			exactMatch
		);
	}

	/**
	 * Adds a pattern from the inline block inserter panel.
	 *
	 * Because there are so many different ways to open the inline inserter,
	 * this function accepts a Locator to the element that should open the inserter.
	 *
	 * The name is expected to be formatted identically (including case) to how it
	 * appears on the label when viewing in the block inserter panel.
	 *
	 * @example
	 * 	- About Me Card
	 * 	- Contact Info with Map
	 * @param {string} patternName Name of the pattern to insert as it matches the label in the inserter.
	 * @param {Locator} inserterLocator Locator to the element that will open the pattern/block inserter when clicked.
	 */
	async addPatternInline( patternName: string, inserterLocator: Locator ): Promise< Locator > {
		// Perform a click action on the locator.
		await inserterLocator.click();
		// Add the specified pattern from the inserter.
		return await this.addPatternFromInserter(
			patternName,
			this.editorInlineBlockInserterComponent
		);
	}

	/**
	 * Shared submethod to insert a pattern from a block inserter (sidebar or inline).
	 *
	 * @param {string} patternName Name of the pattern.
	 * @param {BlockInserter} inserter Block inserter component.
	 * @param {boolean} exactMatch Whether to use exact matching for pattern names.
	 */
	private async addPatternFromInserter(
		patternName: string,
		inserter: BlockInserter,
		exactMatch = true
	): Promise< Locator > {
		const editorParent = await this.editor.parent();

		await inserter.searchBlockInserter( patternName );
		const locator = await inserter.selectBlockInserterResult( patternName, {
			type: 'pattern',
			exactMatch,
		} );

		// For partial matches, get the actual pattern name from the selected element.
		let actualPatternName = patternName;
		if ( ! exactMatch ) {
			actualPatternName = ( await locator.getAttribute( 'aria-label' ) ) ?? '';
		}

		const insertConfirmationToastLocator = editorParent.locator(
			`.components-snackbar__content:text('Block pattern "${ actualPatternName }" inserted.')`
		);
		await insertConfirmationToastLocator.waitFor();
		return locator;
	}

	/**
	 * Remove the block from the editor.
	 *
	 * This method requires the handle to the block in question to be passed in as parameter.
	 *
	 * @param {ElementHandle} blockHandle ElementHandle of the block to be removed.
	 */
	async removeBlock( blockHandle: ElementHandle ): Promise< void > {
		await blockHandle.click();
		await this.page.keyboard.press( 'Backspace' );
	}

	/**
	 * Move the currently selected block up one position with the block toolbar.
	 */
	async moveBlockUp(): Promise< void > {
		await this.editorBlockToolbarComponent.moveUp();
	}

	/**
	 * Move the currently selected block down one position with the block toolbar.
	 */
	async moveBlockDown(): Promise< void > {
		await this.editorBlockToolbarComponent.moveDown();
	}

	/**
	 * Selects the matching option from the popover triggered by clicking
	 * on a block toolbar button.
	 *
	 * @param {string} name Accessible name of the element.
	 */
	async selectFromToolbarPopover( name: string ) {
		await this.editorPopoverMenuComponent.clickMenuButton( name );
	}

	/**
	 * Clicks on a button with either the name or aria-label on the
	 * editor toolbar.
	 *
	 * @param {BlockToolbarButtonIdentifier} name Object specifying either the
	 * 	text label or aria-label of the button to be clicked.
	 */
	async clickBlockToolbarButton( name: BlockToolbarButtonIdentifier ): Promise< void > {
		await this.editorBlockToolbarComponent.clickPrimaryButton( name );
	}

	/**
	 * Select the parent block of the current block using the block toolbar.
	 * This will fail and throw if the currently focused block doesn't have a parent.
	 */
	async selectParentBlock( expectedParentBlockName: string ): Promise< void > {
		if ( envVariables.VIEWPORT_NAME === 'desktop' ) {
			await this.editorBlockToolbarComponent.clickParentBlockButton( expectedParentBlockName );
		} else {
			// If the menu was already open due to another action, don't open it again.
			if ( ! ( await this.editorBlockToolbarComponent.isOptionsMenuOpen() ) ) {
				await this.editorBlockToolbarComponent.clickOptionsButton();
			}
			await this.editorPopoverMenuComponent.clickMenuButton(
				`Select parent block (${ expectedParentBlockName })`
			);
			// The menu usually closes itself on click, but this might be inconsistent.
			// Check if it did close and if not, close it for sure.
			if ( await this.editorBlockToolbarComponent.isOptionsMenuOpen() ) {
				await this.editorBlockToolbarComponent.clickOptionsButton();
			}
		}
	}

	//#endregion

	//#region Settings Sidebar

	/**
	 * Returns the locator for the root element of the settings sidebar.
	 */
	async getSettingsRoot() {
		return await this.editorSettingsSidebarComponent.getRoot();
	}

	/**
	 * Returns the locator of a section (panel body) settings sidebar.
	 *
	 * @param {string} name Name of the section.
	 */
	async getSettingsSection( name: string ) {
		return await this.editorSettingsSidebarComponent.getSection( name );
	}

	/**
	 * Opens the Settings sidebar.
	 */
	async openSettings( target: EditorToolbarSettingsButton = 'Settings' ): Promise< void > {
		await this.editorToolbarComponent.openSettings( target );
	}

	/**
	 * Closes the Settings sidebar.
	 */
	async closeSettings(): Promise< void > {
		// On mobile, the settings panel close button is located on the settings panel itself.
		if ( envVariables.VIEWPORT_NAME === 'mobile' ) {
			await this.editorSettingsSidebarComponent.closeSidebarForMobile();
		} else {
			await this.editorToolbarComponent.closeSettings();
		}
	}

	/**
	 * General method to expand the named section in the settings sidebar.
	 *
	 * @param {string} name Name of the section to expand.
	 */
	async expandSection( name: string ) {
		return await this.editorSettingsSidebarComponent.expandSection( name );
	}

	/**
	 * Clicks on a button with matching accessible name in the Editor sidebar.
	 *
	 * @param {string} name Accessible name of the button.
	 */
	async clickSidebarButton( name: string ): Promise< void > {
		await this.editorSettingsSidebarComponent.clickButton( name );
	}

	/**
	 * Clicks and activates the given tab in the Editor Settings sidebar.
	 *
	 * @param {EditorSidebarTab} tab Name of the tab to activate.
	 */
	async clickSettingsTab( tab: EditorSidebarTab ): Promise< void > {
		await this.editorSettingsSidebarComponent.clickTab( tab );
	}

	/**
	 * Sets the article's privacy/visibility option.
	 *
	 * @param {ArticlePrivacyOptions} visibility Visibility option for the article.
	 * @param param1 Object parameters.
	 * @param {string} param1.password Password for the post.
	 */
	async setArticleVisibility(
		visibility: ArticlePrivacyOptions,
		{ password }: { password?: string }
	): Promise< void > {
		await Promise.race( [
			this.editorSettingsSidebarComponent.clickTab( 'Page' ),
			this.editorSettingsSidebarComponent.clickTab( 'Post' ),
		] );

		await this.editorSettingsSidebarComponent.expandSummary( 'Summary' );
		await this.editorSettingsSidebarComponent.openVisibilityOptions();
		await this.editorSettingsSidebarComponent.selectVisibility( visibility, {
			password: password,
		} );
		await this.editorSettingsSidebarComponent.closeVisibilityOptions();
	}

	/**
	 * View revisions for the article.
	 */
	async viewRevisions(): Promise< void > {
		await Promise.race( [
			this.editorSettingsSidebarComponent.clickTab( 'Page' ),
			this.editorSettingsSidebarComponent.clickTab( 'Post' ),
		] );
		await this.editorSettingsSidebarComponent.showRevisions();
	}

	/**
	 * Select a category for the article.
	 *
	 * @param {string} name Name of the category.
	 */
	async selectCategory( name: string ): Promise< void > {
		await Promise.race( [
			this.editorSettingsSidebarComponent.clickTab( 'Page' ),
			this.editorSettingsSidebarComponent.clickTab( 'Post' ),
		] );
		await this.editorSettingsSidebarComponent.expandSection( 'Categories' );
		await this.editorSettingsSidebarComponent.checkCategory( name );
	}

	/**
	 * Adds the given tag to the article.
	 *
	 * @param {string} tag Tag to be added to article.
	 */
	async addTag( tag: string ): Promise< void > {
		await Promise.race( [
			this.editorSettingsSidebarComponent.clickTab( 'Page' ),
			this.editorSettingsSidebarComponent.clickTab( 'Post' ),
		] );
		await this.editorSettingsSidebarComponent.expandSection( 'Tags' );
		await this.editorSettingsSidebarComponent.enterTag( tag );
	}

	/**
	 * Sets the URL slug.
	 *
	 * @param {string} slug Desired URL slug.
	 */
	async setURLSlug( slug: string ): Promise< void > {
		await Promise.race( [
			this.editorSettingsSidebarComponent.clickTab( 'Page' ),
			this.editorSettingsSidebarComponent.clickTab( 'Post' ),
		] );
		await this.editorSettingsSidebarComponent.expandSummary( 'Summary' );
		await this.editorSettingsSidebarComponent.enterUrlSlug( slug );
	}

	/**
	 * Enters SEO details on the Editor sidebar.
	 *
	 * @param param0 Keyed object parameter.
	 * @param {string} param0.title SEO title.
	 * @param {string} param0.description SEO description.
	 */
	async enterSEODetails( { title, description }: { title: string; description: string } ) {
		await this.editorSettingsSidebarComponent.enterText( title, { label: 'SEO TITLE' } );
		await this.editorSettingsSidebarComponent.enterText( description, {
			label: 'SEO DESCRIPTION',
		} );
	}

	//#endregion

	//#region List View

	/**
	 * Opens the list view.
	 */
	async openListView(): Promise< void > {
		await this.editorToolbarComponent.openListView();
	}

	/**
	 * Closes the list view.
	 */
	async closeListView(): Promise< void > {
		await this.editorToolbarComponent.closeListView();
	}

	/**
	 * In the list view, click on the first block of a given type (e.g. "Heading").
	 *
	 * @param blockName Name of the block type to find and click (e.g. "Heading").
	 */
	async clickFirstListViewEntryByType( blockName: string ): Promise< void > {
		await this.editorBlockListViewComponent.clickFirstBlockOfType( blockName );
	}

	//#endregion

	//#region Publish, Draft & Schedule

	/**
	 * Returns the locator for the root element of the publish toolbar.
	 */
	async getPublishPanelRoot() {
		return await this.editorPublishPanelComponent.getRoot();
	}

	/**
	 * Publishes the post or page and returns the resulting URL.
	 *
	 * If the optional parameter `visit` parameter is specified, the page is navigated
	 * to the published article.
	 *
	 * @param {boolean} visit Whether to then visit the page.
	 * @returns {URL} Published article's URL.
	 */
	async publish( {
		visit = false,
		timeout,
	}: { visit?: boolean; timeout?: number } = {} ): Promise< URL > {
		const actionsArray = [];
		await this.editorToolbarComponent.waitForPublishButton();

		// Every publish action requires at least one click on the EditorToolbarComponent.
		actionsArray.push( this.editorToolbarComponent.clickPublish() );

		// Trigger a secondary/confirmation click if needed
		actionsArray.push( this.editorPublishPanelComponent.publish() );

		// Resolve the promises.
		const [ response ] = await Promise.all( [
			// First URL matches Atomic requests while the second matches Simple requests.
			Promise.race( [
				this.page.waitForResponse(
					async ( response ) =>
						/v2\/(posts|pages)\/[\d]+/.test( response.url() ) &&
						response.request().method() === 'POST',
					{ timeout: timeout }
				),
				this.page.waitForResponse(
					async ( response ) =>
						/.*v2\/sites\/[\d]+\/(posts|pages)\/[\d]+.*/.test( response.url() ) &&
						response.request().method() === 'PUT',
					{ timeout: timeout }
				),
			] ),
			...actionsArray,
		] );

		const json = await response.json();
		// AT and Simple sites have slightly differing response from the API.
		const publishedURL = json.link || json.body?.link;
		if ( ! publishedURL ) {
			throw new Error( 'No published article URL found in response.' );
		}

		if ( visit ) {
			await this.visitPublishedPost( publishedURL, { timeout: timeout } );
		}

		return new URL( publishedURL );
	}

	/**
	 * Schedules an article.
	 *
	 * This method requires the Editor Settings sidebar to be open.
	 */
	async schedule( date: ArticlePublishSchedule ): Promise< void > {
		await Promise.race( [
			this.editorSettingsSidebarComponent.clickTab( 'Page' ),
			this.editorSettingsSidebarComponent.clickTab( 'Post' ),
		] );

		await this.editorSettingsSidebarComponent.expandSummary( 'Summary' );
		await this.editorSettingsSidebarComponent.openSchedule();
		await this.editorSettingsSidebarComponent.setScheduleDetails( date );
		await this.editorSettingsSidebarComponent.closeSchedule();
	}

	/**
	 * Unpublishes the post or page by switching to draft.
	 */
	async unpublish(): Promise< void > {
		const editorParent = await this.editor.parent();
		await this.editorToolbarComponent.switchToDraft();

		// @TODO: eventually refactor this out to a ConfirmationDialogComponent.
		// Saves the draft
		await Promise.race( [ this.editorToolbarComponent.clickPublish(), this.confirmUnpublish() ] );
		// @TODO: eventually refactor this out to a EditorToastNotificationComponent.
		await editorParent.getByRole( 'button', { name: 'Dismiss this notice' } ).waitFor();
	}

	/**
	 * Confirms the unpublish action in some views
	 */
	async confirmUnpublish(): Promise< void > {
		const editorParent = await this.editor.parent();
		const okButtonLocator = editorParent.getByRole( 'button' ).getByText( 'OK' );

		if ( await okButtonLocator.count() ) {
			okButtonLocator.click();
		}
	}

	/**
	 * Obtains the published article's URL from the post-publish toast.
	 *
	 * This method is only able to obtain the published article's URL if the
	 * post-publish toast is still visible on the page.
	 *
	 * @deprecated Please use the return value from `EditorPage.publish` where possible.
	 * @returns {URL} Published article's URL.
	 */
	async getPublishedURLFromToast(): Promise< URL > {
		const editorParent = await this.editor.parent();
		const toastLocator = editorParent.locator( selectors.toastViewPostLink );
		const publishedURL = ( await toastLocator.getAttribute( 'href' ) ) as string;
		return new URL( publishedURL );
	}

	/**
	 * Saves the currently open post as draft.
	 */
	async saveDraft(): Promise< void > {
		await this.editorToolbarComponent.saveDraft();
	}

	/**
	 * Visits the published entry from the post-publish sidebar.
	 *
	 * @returns {Promise<void>} No return value.
	 */
	private async visitPublishedPost(
		url: string,
		{ timeout }: { timeout?: number } = {}
	): Promise< void > {
		// Some blocks, like "Click To Tweet" or "Logos" cause the post-publish
		// panel to close immediately and leave the post in the unsaved state for
		// some reason. Since the post state is unsaved, the warning dialog will be
		// displayed on the published post link click. By default, Playwright will
		// dismiss the dialog so we need this listener to accept it and open the
		// published post.
		//
		// Once https://github.com/Automattic/wp-calypso/issues/54421 is resolved,
		// this listener can be removed.
		this.allowLeavingWithoutSaving();

		await this.page.goto( url, { waitUntil: 'domcontentloaded', timeout: timeout } );

		await reloadAndRetry( this.page, confirmPostShown );

		/**
		 * Closure to confirm that post is shown on screen as expected.
		 *
		 * In rare cases, visiting the post immediately after it has been published can result
		 * in the post not being visible to the public yet. In such cases, an error message is
		 * instead shown to the user.
		 *
		 * When used in conjunction with `reloadAndRetry` this method will reload the page
		 * multiple times to ensure the post content is shown.
		 *
		 * @param page
		 */
		async function confirmPostShown( page: Page ): Promise< void > {
			await page.getByRole( 'main' ).waitFor( { timeout: timeout } );
		}
	}

	//#endregion

	//#region Previews

	/**
	 * Launches the Preview as mobile viewport.
	 *
	 * For Mobile viewports, this method will return a reference to a popup Page.
	 * For Desktop and Tablet viewports, an error is thrown.
	 *
	 * @returns {Promise<Page>} Handler for the popup page.
	 * @throws {Error} If the current viewport is not of Mobile.
	 */
	async previewAsMobile(): Promise< Page > {
		if ( envVariables.VIEWPORT_NAME !== 'mobile' ) {
			throw new Error(
				`This method only works in mobile viewport, current viewport: ${ envVariables.VIEWPORT_NAME } `
			);
		}
		return await this.editorToolbarComponent.openMobilePreview();
	}

	/**
	 * Launches the Preview as non-mobile viewport.
	 *
	 * For Desktop and Tablet viewports, this method will not return any value.
	 * For Mobile viewport, an error is thrown.
	 *
	 * @param {EditorPreviewOptions} target Target preview size.
	 * @returns {Page|void} Handler for the Page object on mobile. Void otherwise.
	 * @throws {Error} If the current viewport is not of Desktop or Tablet.
	 */
	async previewAsDesktop( target: EditorPreviewOptions ): Promise< void > {
		if ( envVariables.VIEWPORT_NAME === 'mobile' ) {
			throw new Error(
				`This method only works in non-mobile viewport, current viewport: ${ envVariables.VIEWPORT_NAME } `
			);
		}
		await this.editorToolbarComponent.openDesktopPreview( target );
	}

	/**
	 * Closes the preview.
	 *
	 * For Desktop viewports, this method will return the editor pane to the default
	 * value of `desktop`.
	 * For Mobile viewports, this method will do nothing.
	 */
	async closePreview(): Promise< void > {
		if ( envVariables.VIEWPORT_NAME === 'mobile' ) {
			return;
		}
		await this.editorToolbarComponent.openDesktopPreview( 'Desktop' );
	}

	//#endregion

	//#region Misc

	/**
	 * Leave the editor to return to the Calypso dashboard.
	 *
	 * On desktop sized viewport, this method first opens the editor navigation
	 * sidebar, then clicks on the `Dashboard` link.
	 *
	 * On mobile sized viewport, this method clicks on Navbar > My Sites.
	 *
	 * The resulting page can change based on where you come from, and the viewport. Either way, the resulting landing spot
	 * will have access to the Calyspo sidebar, allowing navigation around Calypso.
	 */
	async exitEditor(): Promise< void > {
		// There are three different places to return to,
		// depending on how the editor was entered.
		const navigationPromise = Promise.race( [
			this.page.waitForNavigation( { url: '**/home/**' } ),
			this.page.waitForNavigation( { url: '**/posts/**' } ),
			this.page.waitForNavigation( { url: '**/pages/**' } ),
			this.page.waitForNavigation( { url: '**/wp-admin/edit**' } ),
			this.page.waitForNavigation( { url: '**/write/launchpad**' } ),
		] );
		const actions: Promise< unknown >[] = [ navigationPromise ];

		if ( envVariables.VIEWPORT_NAME === 'mobile' ) {
			// Mobile viewports do not use an EditorNavSidebar.
			// Instead, the regular NavBar is used, and the
			// `<` button exits the editor.
			const navbarComponent = new NavbarComponent( this.page );
			actions.push( navbarComponent.clickEditorBackButton() );
		} else {
			actions.push( this.editorToolbarComponent.closeEditor() );
		}

		// Perform the actions and resolve promises.
		await Promise.all( actions );
	}

	/**
	 * Sets up a handler to accept the leave without saving warning.
	 */
	allowLeavingWithoutSaving(): void {
		this.page.once( 'dialog', async ( dialog ) => {
			await dialog.accept();
		} );
	}

	/**
	 * Checks whether the editor has any block warnings/errors displaying.
	 *
	 * @returns {Promise<boolean>} True if there are block warnings/errors.
	 * False otherwise.
	 */
	async editorHasBlockWarnings(): Promise< boolean > {
		return await this.editorGutenbergComponent.editorHasBlockWarning();
	}

	/**
	 * Open the editor options menu.
	 */
	async openEditorOptionsMenu(): Promise< void > {
		return this.editorToolbarComponent.openMoreOptionsMenu();
	}

	/**
	 * Close the
	 */
	async closeWelcomeGuideIfNeeded(): Promise< void > {
		return this.editorWelcomeGuideComponent.closeWelcomeGuideIfNeeded();
	}

	//#endregion
}