File size: 7,741 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
import { Page } from 'playwright';
import { getCalypsoURL } from '../../data-helper';
import { clickNavTab } from '../../element-helper';
import envVariables from '../../env-variables';

// Types to restrict the string arguments passed in. These are fixed sets of strings, so we can be more restrictive.
export type Plans =
	| 'Free'
	| 'Personal'
	| 'Premium'
	| 'Business'
	| 'eCommerce'
	| 'Starter'
	| 'Explorer'
	| 'Creator'
	| 'Entrepreneur';
export type PlansPageTab = 'My Plan' | 'Plans';
export type PlanActionButton = 'Manage plan' | 'Upgrade';

const selectors = {
	// Generic
	placeholder: '.is-placeholder',
	managePlanButton: 'a:has-text("Manage plan")',
	addOnCombobox: ( name: Plans ) => {
		return `.plans-grid-next-plan-storage[data-plan-title="${ name }"]`;
	},
	addOnComboboxButton: 'button[role="combobox"]',
	addOnComboboxOption: ( addOn: string ) => `[role="option"]:has-text("${ addOn }")`,
	selectPlanButton: ( name: Plans ) => {
		if ( name === 'Free' ) {
			// Free plan is a pseudo-button presented as a
			// link.
			return `button:text-matches("${ name }", "i"):visible`;
		}
		return `button.is-${ name.toLowerCase() }-plan:visible`;
	},
	selectModalUpsellPlanButton: ( name: 'Free' | 'Personal' ) => {
		if ( name === 'Free' ) {
			return 'button.is-upsell-modal-free-plan:visible';
		}
		return `button.is-upsell-modal-${ name.toLowerCase() }-plan:visible`;
	},

	// Navigation
	mobileNavTabsToggle: 'button.section-nav__mobile-header',
	navigationTab: ( tabName: PlansPageTab ) => `.section-nav-tab:has-text("${ tabName }")`,
	activeNavigationTab: ( tabName: PlansPageTab ) =>
		`.is-selected.section-nav-tab:has-text("${ tabName }")`,

	// Plans tab
	PlansGrid: '.plans-features-main',
	actionButton: ( { plan, buttonText }: { plan: Plans; buttonText: PlanActionButton } ) => {
		const viewportSuffix = envVariables.VIEWPORT_NAME === 'mobile' ? 'mobile' : 'table';
		return `.plan-features__${ viewportSuffix } >> .plan-features__actions-button.is-${ plan.toLowerCase() }-plan:has-text("${ buttonText }")`;
	},
	activePlan: ( plan: Plans ) => `a.is-${ plan.toLowerCase() }-plan.is-current-plan:visible`,
	spotlightPlan: '.plan-features-2023-grid__plan-spotlight',
	// My Plans tab
	myPlanTitle: ( planName: Plans ) => `.my-plan-card__title:has-text("${ planName }")`,
};

/**
 * Page representing the Plans page under `/plans` endpoint.
 *
 * Also forms a component of the `SignupPickPlanPage` used in the
 * signup stepper flow.
 */
export class PlansPage {
	private page: Page;

	/**
	 * Constructs an instance of the Plans POM.
	 *
	 * @param {Page} page Instance of the Playwright page
	 */
	constructor( page: Page ) {
		this.page = page;
	}

	/**
	 * Visits the Plans page.
	 *
	 * @param {PlansPageTab} target Target page.
	 * @param {string} siteSlug Site slug.
	 */
	async visit( target: PlansPageTab, siteSlug: string ): Promise< void > {
		const sanitized = target.toLowerCase().replace( ' ', '-' );

		if ( target === 'My Plan' ) {
			await this.page.goto( getCalypsoURL( `plans/${ sanitized }/${ siteSlug }` ) );
		}

		if ( target === 'Plans' ) {
			await this.page.goto( getCalypsoURL( `plans/${ siteSlug }` ) );
		}
	}

	/* Plans */

	/**
	 * Selects the add-on for the target plan.
	 *
	 * @param {Plans} plan Plan to select.
	 * @param {string} addOn Add-on to select.
	 */
	async selectAddOn( plan: Plans, addOn: string ): Promise< void > {
		const combobox = this.page.locator( selectors.addOnCombobox( plan ) );

		const comboboxSelect = combobox.locator( selectors.addOnComboboxButton );
		await comboboxSelect.first().click();

		const comboboxOption = combobox.locator(
			selectors.addOnComboboxOption( `50 GB + ${ addOn }` )
		);
		await comboboxOption.first().click();
	}

	/**
	 * Selects the target plan on the plans grid.
	 *
	 * @param {Plans} plan Plan to select.
	 */
	async selectPlan( plan: Plans ): Promise< void > {
		const locator = this.page.locator( selectors.selectPlanButton( plan ) );
		// In the `/plans` view, there are two buttons for "Upgrade" on the
		// plan comparison chart. Select the first one.
		await locator.first().click();
	}

	/**
	 * Selects the plan on the modal upsell.
	 *
	 * @param {Plans} plan Plan to select.
	 */
	async selectModalUpsellPlan( plan: Plans ): Promise< void > {
		if ( plan !== 'Free' && plan !== 'Personal' ) {
			throw Error( `Unsupported plan to be selected in modal upsell: ${ plan }` );
		}

		const locator = this.page.locator( selectors.selectModalUpsellPlanButton( plan ) );

		await locator.first().click();
	}

	/* Generic */

	/**
	 * Validates the expected plan is active.
	 *
	 * This method accepts a parameter of `Plan` type which defines the expected
	 * plan to be active on the site. This method then compares this against the
	 * actual text on both the My Plans and Plans tabs, hence the race.
	 *
	 * @param {Plans} expectedPlan Name of the expected plan.
	 * @throws If the expected plan title is not found in the timeout period.
	 */
	async validateActivePlan( expectedPlan: Plans ): Promise< void > {
		await this.page.locator( selectors.spotlightPlan ).getByText( expectedPlan ).waitFor();
	}

	/**
	 * Clicks on the "Manage plan" button, which can be found in the My Plan tab.
	 */
	async clickManagePlan(): Promise< void > {
		await this.page.click( selectors.managePlanButton );
	}
	/**
	 * Validates that the provided tab name is the the currently active tab in the wrapper Plans page. Throws if it isn't.
	 *
	 * @param {PlansPageTab} expectedTab Name of the expected tab.
	 * @throws If the expected tab name is not the active tab.
	 */
	async validateActiveTab( expectedTab: PlansPageTab ): Promise< void > {
		// For mobile sized viewport, the currently selected tab name
		// is hidden behind a pseudo-dropdown.
		// Therefore the valicdation will look for hidden element.
		const currentSelectedLocator = this.page.locator(
			selectors.activeNavigationTab( expectedTab )
		);
		if ( envVariables.VIEWPORT_NAME === 'mobile' ) {
			await currentSelectedLocator.waitFor( { state: 'hidden' } );
		} else {
			await currentSelectedLocator.waitFor();
		}
	}

	/**
	 * Clicks on the navigation tab (desktop) or dropdown (mobile).
	 *
	 * @param {PlansPageTab} targetTab Name of the tab.
	 */
	async clickTab( targetTab: PlansPageTab ): Promise< void > {
		// The way PlansPage loads its contents is particularly prone to
		// flakiness outside the control of Playwright auto-retry mechanism.
		// To work around this, forcibly click on the target selector
		// once everything has been loaded.
		// This affects primarily Mobile viewports but also can also occur
		// on Desktop viewports.
		// See https://github.com/Automattic/wp-calypso/issues/64389
		// and https://github.com/Automattic/wp-calypso/pull/64421#discussion_r892589761.
		await Promise.all( [
			this.page.waitForLoadState( 'networkidle', { timeout: 20 * 1000 } ),
			this.page.waitForResponse( /.*active-promotions.*/ ),
		] );
		await clickNavTab( this.page, targetTab, { force: true } );
	}

	/**
	 * Click a plan action button (on the plan cards on the "Plans" tab) based on expected plan name and button text.
	 *
	 * @param {Object} param0 Object containing plan name and button text
	 * @param {Plans} param0.plan Name of the plan (e.g. "Premium")
	 * @param {PlanActionButton} param0.buttonText Expected action button text (e.g. "Upgrade")
	 */
	async clickPlanActionButton( {
		plan,
		buttonText,
	}: {
		plan: Plans;
		buttonText: PlanActionButton;
	} ): Promise< void > {
		const selector = selectors.actionButton( {
			plan: plan,
			buttonText: buttonText,
		} );
		// These action buttons trigger real page navigations.
		await Promise.all( [ this.page.waitForNavigation(), this.page.click( selector ) ] );
	}
}