File size: 3,164 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
import { Page, Locator } from 'playwright';

const EDITOR_TIMEOUT = 60 * 1000;

/**
 * Represents the Editor component.
 */
export class EditorComponent {
	private page: Page;
	private parentLocator: Locator | null;
	private canvasLocator: Locator | null;

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

	/**
	 * Returns the Editor parent locator. It will automatically resolve to the
	 * proper locator, regardless if the Editor is Gutenframed or not.
	 */
	async parent(): Promise< Locator > {
		if ( this.parentLocator ) {
			return this.parentLocator;
		}

		try {
			this.parentLocator = await Promise.any( [
				this.waitForFramedEditor(),
				this.waitForUnframedEditor(),
			] );
		} catch ( _error ) {
			throw new Error( 'Timed out waiting for the Editor' );
		}

		return this.parentLocator;
	}

	/**
	 * Returns the Editor canvas locator. It will automatically resolve to the
	 * proper locator, regardless if the canvas is iframed or not.
	 */
	async canvas(): Promise< Locator > {
		if ( this.canvasLocator ) {
			return this.canvasLocator;
		}

		try {
			this.canvasLocator = await Promise.any( [
				this.waitForFramedCanvas(),
				this.waitForUnframedCanvas(),
			] );
		} catch ( _error ) {
			throw new Error( 'Timed out waiting for the Editor canvas' );
		}

		return this.canvasLocator;
	}

	/**
	 * If the Editor is gutenframed, it will resolve with the parent element
	 * locator inside the Gutenframe once it's ready. Otherwise, it will time
	 * out.
	 */
	private async waitForFramedEditor() {
		const parentLocator = this.page
			.frameLocator( 'iframe[src*="calypsoify"]' )
			.locator( 'body.block-editor-page' );

		await parentLocator.waitFor( { timeout: EDITOR_TIMEOUT } );
		return parentLocator;
	}

	/**
	 * If the Editor is NOT gutenframed, it will resolve with the parent element
	 * locator inside the main frame. Otherwise, it will time out.
	 */
	private async waitForUnframedEditor() {
		const parentLocator = this.page.locator( 'body.block-editor-page' );

		await parentLocator.waitFor( { timeout: EDITOR_TIMEOUT } );
		return parentLocator;
	}

	/**
	 * If the Editor canvas is iframed, it will resolve with the parent element
	 * locator inside that iframe once it's ready. Otherwise, it will time out.
	 */
	private async waitForFramedCanvas() {
		const parentLocator = await this.parent();
		const canvasLocator = parentLocator
			.locator( '.editor-visual-editor' )
			.first()
			.frameLocator( 'iframe' )
			.locator( '.editor-styles-wrapper' );

		await canvasLocator.waitFor( { timeout: EDITOR_TIMEOUT } );
		return canvasLocator;
	}

	/**
	 * If the Editor canvas is NOT iframed, it will resolve with the canvas
	 * element locator inside the current Editor parent once it's ready.
	 * Otherwise, it will time out.
	 */
	private async waitForUnframedCanvas() {
		const parentLocator = await this.parent();
		const canvasWrapper = parentLocator.locator( '.editor-styles-wrapper' );
		await canvasWrapper.waitFor();

		return parentLocator;
	}
}