File size: 1,702 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
import { Frame, Page } from 'playwright';

const selectors = {
	previewPane: '.web-preview',
	iframe: '.web-preview__frame',

	// Actions on pane
	closeButton: 'button[aria-label="Close preview"]',
	activateButton: 'text=Activate',
};

/**
 * Component representing the site published preview component. This same preview modal is used for themes and editor previewing.
 */
export class PreviewComponent {
	private page: Page;

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

	/**
	 * Waits for the preview pane to be visible.
	 *
	 * @returns {Promise<void>} No return value.
	 */
	async previewReady(): Promise< void > {
		await this.page.waitForSelector( selectors.previewPane );
	}

	/**
	 * Close the preview pane.
	 *
	 * @returns {Promise<void>} No return value.
	 */
	async closePreview(): Promise< void > {
		await this.page.click( selectors.closeButton );
	}

	/**
	 * Validates that the provided text can be found in the preview content. Throws if it isn't.
	 *
	 * @param {string} text Text to search for in preview content
	 */
	async validateTextInPreviewContent( text: string ): Promise< void > {
		const frame = await this.getPreviewFrame();
		await frame.waitForSelector( `text=${ text }` );
	}

	/**
	 * Get the Iframe element handle for the preview content. This frame is the one located within the preview popup.
	 *
	 * @returns {Frame} Handle for the preview content Iframe
	 */
	private async getPreviewFrame(): Promise< Frame > {
		const elementHandle = await this.page.waitForSelector( selectors.iframe );
		return ( await elementHandle.contentFrame() ) as Frame;
	}
}