File size: 12,232 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
import assert from 'assert';
import { Page } from 'playwright';
import { getCalypsoURL } from '../../data-helper';

const selectors = {
	// React modal buttons
	modalButtonWithText: ( text: string ) => `.dialog__action-buttons button:has-text("${ text }")`,

	listTitle: ( section: string ) => `.plugins-results-header__title:text("${ section }")`,
	listSubtitle: ( section: string ) => `.plugins-results-header__subtitle:text("${ section }")`,
	headerTitle: ( section: string ) => `.plugins-results-header__title:text("${ section }")`,
	pluginTitle: ( plugin: string ) => `.plugins-browser-item__title:text("${ plugin }")`,
	pluginTitleOnSection: ( section: string, plugin: string ) =>
		`.plugins-browser-list:has(.plugins-results-header__title:text("${ section }")) :text-is("${ plugin }")`,
	sectionTitles: '.plugins-results-header__title',
	browseAllFree: 'a[href^="/plugins/browse/popular"]',
	browseAllPaid: 'a[href^="/plugins/browse/paid"]',
	browseFirstCategory: 'button:has-text("Search Engine Optimization")',
	breadcrumb: ( section: string ) => `.navigation-header__main a:text("${ section }") `,
	pricingToggle: ':text("Monthly Price"), :text("Annual Price")',
	monthlyPricingSelect: 'a[data-bold-text^="Monthly price"]',
	annualPricingSelect: 'a[data-bold-text^="Annual price"]',
	monthlyPricing: '.plugins-browser-item__period:text("monthly")',
	annualPricing: '.plugins-browser-item__period:text("per year")',

	// Search
	searchInput: '.components-search-control .components-input-control__input',
	searchResult: ( text: string ) => `.plugins-browser-item__title:text("${ text }")`,
	// eslint-disable-next-line no-useless-escape
	searchResultTitle: ( text: string ) => `:text('plugins for "${ text }"')`,

	// Plugin view
	installButton: '.plugin-details-cta__install-button',
	deactivateButton: 'button:text("Deactivate")',
	activateButton: 'button:text("Activate")',
	openRemoveMenuButton: '.plugin-details-cta__manage-plugin-menu button[title="Toggle menu"]',
	removeButton: '.popover__menu button:has-text("Remove")',

	// Category selector
	selectedCategory: ( categoryTitle: string ) => `.categories__header:text("${ categoryTitle }")`,

	// Plugin details view
	pluginDetailsHeaderTitle: ( section: string ) =>
		`.plugin-details-header__name:text("${ section }")`,
	planUpgradeRequiredIcon: 'span.plugin-details-cta__upgrade-required-icon',

	// Post install
	installedPluginCard: '.thank-you__step',
	installedfooterCards: '.thank-you__footer',
	manageInstalledPluginButton: 'a:has-text("Manage plugin")',
};

/**
 * Plugins page https://wordpress.com/plugins/.
 */
export class PluginsPage {
	private page: Page;

	static paidSection = 'Must-have premium plugins';
	static featuredSection = 'Our developers’ favorites';
	static freeSection = 'Popular Plugins';

	/**
	 * Constructs an instance.
	 */
	constructor( page: Page ) {
		this.page = page;
	}

	/**
	 * Visit the Plugins page in Calypso at `/plugins`.
	 *
	 * If optional parameter `site` is specified, this method will
	 * attempt to load `/plugins/<site>` endpoint.
	 *
	 * @param {string} site Optional site URL.
	 */
	async visit( site = '' ): Promise< void > {
		await Promise.all( [
			this.page.waitForResponse( /\/sites\/\d+\/plugins/, { timeout: 20 * 1000 } ),
			// This is one of the last, reliable web requests to finish on this page
			// and is a pretty good indicator the async loading is done.
			this.page.goto( getCalypsoURL( `plugins/${ site }` ), { timeout: 20 * 1000 } ),
		] );
	}

	/**
	 * Visit a specific page within the Plugins feature at `/plugins/page`.
	 *
	 * If optional paramter `site` is specified, this method will
	 * attempt to load `/plugins/<page>/<site>` endpoint.
	 *
	 * @param {string} page Sub-page to visit.
	 * @param {string} site Optional site URL.
	 */
	async visitPage( page: string, site = '' ): Promise< void > {
		await this.page.goto( getCalypsoURL( `plugins/${ page }/${ site }` ) );
	}

	/**
	 * Validate page has the section
	 */
	async validateHasSection( section: string ): Promise< void > {
		await this.page.waitForSelector( selectors.listTitle( section ) );
	}

	/**
	 * Validate page has a list subtitle containing text
	 */
	async validateHasSubtitle( section: string ): Promise< void > {
		await this.page.waitForSelector( selectors.listSubtitle( section ) );
	}

	/**
	 * Validate page has the right category selected
	 */
	async validateSelectedCategory( categoryTitle: string ): Promise< void > {
		await this.page.waitForSelector( selectors.selectedCategory( categoryTitle ) );
	}

	/**
	 * Validate page has a header title containing text
	 */
	async validateHasHeaderTitle( section: string ): Promise< void > {
		await this.page.waitForSelector( selectors.headerTitle( section ) );
	}

	/**
	 * Validate plugin details page has a header title containing text
	 */
	async validatePluginDetailsHasHeaderTitle( section: string ): Promise< void > {
		await this.page.waitForSelector( selectors.pluginDetailsHeaderTitle( section ) );
	}

	/**
	 * Validate section is not present on page
	 */
	async validateNotHasSection( section: string ): Promise< void > {
		await this.page.waitForSelector( selectors.sectionTitles );
		const titles = this.page.locator( selectors.sectionTitles );
		const count = await titles.count();
		assert.notEqual( count, 0 ); // ensure at least one is loaded before checking the negative
		for ( let i = 0; i < count; i++ ) {
			const title = await titles.nth( i ).innerText();
			assert.notEqual( title, section );
		}
	}

	/**
	 * Validate page has the section
	 */
	async validateHasPluginOnSection( section: string, plugin: string ): Promise< void > {
		await this.page.waitForSelector( selectors.pluginTitleOnSection( section, plugin ) );
	}

	/**
	 * Validate category has the plugin
	 */
	async validateHasPluginInCategory( section: string, plugin: string ): Promise< void > {
		await this.page.waitForSelector( selectors.headerTitle( section ) );
		await this.page.waitForSelector( selectors.pluginTitle( plugin ) );
	}

	/**
	 * Click Browse All Free Plugins
	 */
	async clickBrowseAllFreePlugins(): Promise< void > {
		await this.page.click( selectors.browseAllFree );
	}

	/**
	 * Click Browse All Paid Plugins
	 */
	async clickBrowseAllPaidPlugins(): Promise< void > {
		await this.page.click( selectors.browseAllPaid );
	}

	/**
	 * Validate Category Button
	 */
	async validateCategoryButton( category: string, isDesktop: boolean ): Promise< void > {
		let categoryLocator;
		if ( isDesktop ) {
			categoryLocator = this.page.getByRole( 'radio', { name: category } );
		} else {
			await this.page.getByRole( 'button', { name: 'More', exact: true } ).click();
			categoryLocator = this.page.getByRole( 'menuitem', { name: category } );
		}
		await categoryLocator.click();
		await this.page.waitForSelector( selectors.headerTitle( category ) );
	}

	/**
	 * Click the "Back" breadcrumb
	 */
	async clickBackBreadcrumb(): Promise< void > {
		await this.page.click( selectors.breadcrumb( 'Back' ) );
	}

	/**
	 * Click the "Plugins" breadcrumb
	 */
	async clickPluginsBreadcrumb(): Promise< void > {
		await this.page.click( selectors.breadcrumb( 'Plugins' ) );
	}

	/**
	 * Click the "Search Results" breadcrumb
	 */
	async clickSearchResultsBreadcrumb(): Promise< void > {
		await this.page.click( selectors.breadcrumb( 'Search Results' ) );
	}

	/**
	 * Select monthly
	 */
	async selectMonthlyPricing(): Promise< void > {
		await this.page.click( selectors.pricingToggle );
		await this.page.click( selectors.monthlyPricingSelect );
	}

	/**
	 * Select annual
	 */
	async selectAnnualPricing(): Promise< void > {
		await this.page.click( selectors.pricingToggle );
		await this.page.click( selectors.annualPricingSelect );
	}

	/**
	 * Check Is Monthly Pricing
	 */
	async validateIsMonthlyPricing(): Promise< void > {
		await this.page.waitForSelector( selectors.monthlyPricing );
	}

	/**
	 * Check Is Annual Pricing
	 */
	async validateIsAnnualPricing(): Promise< void > {
		await this.page.waitForSelector( selectors.annualPricing );
	}

	/**
	 * Performs a search for the provided string.
	 *
	 * @param {string} query String to search for.
	 */
	async search( query: string ): Promise< void > {
		await this.page.fill( selectors.searchInput, query );
		await this.page.press( selectors.searchInput, 'Enter' );
		await this.page.waitForSelector( selectors.searchResultTitle( query ) );
	}

	/**
	 * Click on the search result.
	 *
	 * @param {string} name Name of the plugin to click.
	 */
	async clickSearchResult( name: string ): Promise< void > {
		await this.page.click( selectors.searchResult( name ) );
	}

	/**
	 * Validate Expected Search Result Found
	 */
	async validateExpectedSearchResultFound( expectedResult: string ): Promise< void > {
		await this.page.waitForSelector( selectors.searchResult( expectedResult ) );
	}

	/* Plugin View */

	/**
	 * Deactivate the plugin on the current plugin page.
	 */
	async clickDeactivatePlugin(): Promise< void > {
		await this.page.getByRole( 'button', { name: 'Deactivate', exact: true } ).click();
		await this.page.getByRole( 'button', { name: 'Activate', exact: true } ).waitFor();
	}

	/**
	 * Clicks on the button to install the plugin.
	 *
	 * For sites without a supported plan, this method will click on the additional
	 * modal that appears prompting the user to purchase a plan upgrade.
	 */
	async clickInstallPlugin(): Promise< void > {
		const button = await this.page.locator( selectors.installButton );

		const text = await button.innerText();
		if ( /^(Purchase|Upgrade) and activate$/.test( text ) ) {
			await Promise.all( [ this.page.waitForResponse( /eligibility/ ), button.click() ] );
			// Modal will appear to re-confirm to the user that an upgrade is necessary.
			// Accept the confirmation.
			await this.page.getByRole( 'button', { name: 'Upgrade and activate plugin' } ).click();
		} else {
			await Promise.all( [ this.page.waitForResponse( /.*install\?.*/ ), button.click() ] );
		}
	}

	/**
	 * Clicks on the `Remove Plugin` button.
	 */
	async clickRemovePlugin(): Promise< void > {
		const openRemoveMenuButtonLocator = this.page.locator( selectors.openRemoveMenuButton );
		await openRemoveMenuButtonLocator.click();

		const removeButtonLocator = this.page.locator( selectors.removeButton );
		await removeButtonLocator.click();
		const confirmDialogButton = this.page.locator( selectors.modalButtonWithText( 'Remove' ) );
		await Promise.all( [
			this.page.waitForResponse( /.*delete\?.*/ ),
			confirmDialogButton.click(),
		] );
	}

	/**
	 * Validates you've landed on the confirmation page post-install.
	 *
	 * @param {string} expectedPluginName Name of the plugin to validate.
	 */
	async validateConfirmationPagePostInstall( expectedPluginName?: string ): Promise< void > {
		await this.page
			.getByRole( 'heading', { name: "Congrats on your site's new superpowers!" } )
			.click();

		// Check for expiration text
		await this.page.locator( selectors.installedPluginCard ).getByText( 'expire' ).waitFor();

		// Check for the correct footer cards
		await this.page.locator( selectors.installedfooterCards ).getByText( 'Keep growing' ).waitFor();
		await this.page.locator( selectors.installedfooterCards ).getByText( 'Learn More' ).waitFor();

		// Check for plugin name
		if ( expectedPluginName ) {
			await this.page
				.locator( selectors.installedPluginCard )
				.getByText( expectedPluginName )
				.waitFor();
		}
	}

	/**
	 * After installing a plugin, clicks the button to manage that plugin.
	 */
	async clickManageInstalledPluginButton(): Promise< void > {
		await this.page.locator( selectors.manageInstalledPluginButton ).click();
	}

	/**
	 * Click on a category
	 */
	async clickCategory( name: string ): Promise< void > {
		await this.page.getByRole( 'radio', { name } ).click();
	}

	/**
	 * Open the categories dropdown, shown on mobile
	 */
	async openCategoriesDropdown(): Promise< void > {
		await this.page.getByRole( 'button', { name: 'More', exact: true } ).click();
	}

	/**
	 * Open the categories dropdown, shown on mobile
	 */
	async clickDropdownCategory( name: string ): Promise< void > {
		await this.openCategoriesDropdown();
		await this.page.getByRole( 'menuitem', { name } ).click();
	}
}