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

const selectors = {
	// Post body
	postPasswordInput: 'input[name="post_password"]',

	// Published content
	socialShareSection: '.sharedaddy',
};

/**
 * Represents the published site's post listings page.
 */
export class PublishedPostPage {
	private page: Page;
	private anchor: Locator;

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

	/**
	 * Fills and submits the post password for password protected entries.
	 *
	 * @param {string} password Password to submit.
	 */
	async enterPostPassword( password: string ): Promise< void > {
		await this.page.fill( selectors.postPasswordInput, password );
		await Promise.all( [ this.page.waitForNavigation(), this.page.keyboard.press( 'Enter' ) ] );
	}

	/**
	 * Clicks the Like button on the post.
	 *
	 * This method will also confirm that click action on the Like button
	 * had the intended effect.
	 */
	async likePost(): Promise< void > {
		const locator = this.page
			.frameLocator( 'iframe[title="Like or Reblog"]' )
			.getByRole( 'link', { name: 'Like', exact: true } );

		// On AT sites Playwright is not able to scroll directly to the iframe
		// containing the Like/Unlike button (similar to Post Comments).
		await locator.evaluate( ( element ) => element.scrollIntoView() );

		await locator.click();

		// The button should now read "Liked".
		await this.page
			.frameLocator( 'iframe[title="Like or Reblog"]' )
			.getByRole( 'link', { name: 'Liked', exact: true } )
			.waitFor();
	}

	/**
	 * Clicks the already-liked Like button on the post to unlike.
	 *
	 * This method will also confirm that click action on the Like button
	 * had the intended effect.
	 */
	async unlikePost(): Promise< void > {
		const locator = this.page
			.frameLocator( 'iframe[title="Like or Reblog"]' )
			.getByRole( 'link', { name: 'Liked', exact: true } );

		// On AT sites Playwright is not able to scroll directly to the iframe
		// containing the Like/Unlike button (similar to Post Comments).
		await locator.evaluate( ( element ) => element.scrollIntoView() );

		await locator.click();

		// The button should now read "Like".
		await this.page
			.frameLocator( 'iframe[title="Like or Reblog"]' )
			.getByRole( 'link', { name: 'Like', exact: true } )
			.waitFor();
	}

	/**
	 * Fills out a subscription form on the published post with the supplied
	 * email address, and confirms the subscription.
	 *
	 * Note that this method currently only handles Free subscriptions.
	 *
	 * @param {string} email Email address to subscribe.
	 */
	async subscribe( email: string ) {
		await this.anchor.getByPlaceholder( /type your email/i ).fill( email );
		await this.anchor.getByRole( 'button', { name: 'Subscribe' } ).click();

		// The popup dialog is in its own iframe.
		const iframe = this.page.frameLocator( 'iframe[id="memberships-modal-iframe"]' );

		// This handler is required because if the site owner has set up any
		// paid plans, the modal will first show a list of plans the user
		// can choose from.
		// However, we don't know for sure whether a site owner has set up any
		// newsletter plans.
		const continueButton = iframe.getByRole( 'button', { name: 'Got it', exact: true } );
		const freeTrialLink = iframe.getByRole( 'link', {
			name: 'Free - Get a glimpse of the newsletter',
		} );

		await continueButton.or( freeTrialLink ).waitFor();
		if ( await freeTrialLink.isVisible() ) {
			await freeTrialLink.click();
		}

		await continueButton.click();
	}

	/**
	 * Validates that the title is as expected.
	 *
	 * @param {string} title Title text to check.
	 */
	async validateTitle( title: string ) {
		// The dash is used in the title of the published post is
		// not a "standard" dash, instead being U+2013.
		// We have to replace any expectatiosn of "normal" dashes
		// with the U+2013 version, otherwise the match will fail.
		const sanitizedTitle = title.replace( /-/g, '–' );
		await this.anchor.getByRole( 'heading', { name: sanitizedTitle } ).waitFor();
	}

	/**
	 * Validates that the provided text can be found in the post page. Throws if it isn't.
	 *
	 * @param {string} text Text to search for in post page
	 */
	async validateTextInPost( text: string ): Promise< void > {
		const splitString = text.split( '\n' );
		const dash = /-/;
		for await ( let line of splitString ) {
			// Sanitize the string and replace U+002d (hyphen/dash)
			// with U+2013 (em dash) if the paragraph starts with a dash.
			// Note that dashes found outside of leading position are
			// not impacted.
			// https://make.wordpress.org/docs/style-guide/punctuation/dashes/
			if ( line.search( dash ) === 0 ) {
				line = line.replace( dash, '–' );
			}
			await this.page.waitForSelector( `:text("${ line }"):visible` );
		}
	}

	/**
	 * Validates that the category has been added to the article.
	 *
	 * @param {string} category Category to validate on page.
	 */
	async validateCategory( category: string ): Promise< void > {
		await this.page.waitForSelector( `a:text-is("${ category }")` );
	}

	/**
	 * Validates that the tag has been added to the article.
	 *
	 * @param {string} tag Tag to validate on page.
	 */
	async validateTags( tag: string ): Promise< void > {
		await this.page.waitForSelector( `a:text-is("${ tag }")` );
	}

	/**
	 * Validates the presence of a social sharing button on the published content.
	 *
	 * If optional parameter `click` is set, the button will be clicked to verify
	 * functionality.
	 *
	 * @param {string} name Name of the social sharing button.
	 */
	async validateSocialButton( name: string, { click }: { click?: boolean } = {} ) {
		// CSS selector have to be used due to no accessible locator for narrowing
		// to the social icons.
		const button = this.anchor
			.locator( selectors.socialShareSection )
			.getByRole( 'link', { name: name } );

		await button.waitFor();

		if ( click ) {
			const popupPromise = this.page.waitForEvent( 'popup' );
			await button.click();
			const popup = await popupPromise;
			await popup.waitForLoadState( 'load' );
			await popup.close();
		}
	}
}