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

const selectors = {
	// Shared
	purchaseTitle: ( title: string ) => `.manage-purchase__title:has-text("${ title }")`,
	autoRenewToggle: 'input.components-form-toggle__input',
	modalConfirm: 'button[data-tip-target="dialog-base-action-confirm"]',
	radioButton: ( value: string ) => `input[type="radio"][value=${ value }]`,
	modalSubmit: 'button[data-e2e-button="submit"]',
	button: ( text: string ) => `button:has-text("${ text }")`,
	purchasePlaceholder: '.subscriptions__list .purchase-item__placeholder',

	// Purchased item actions: plans
	renewNowCardButton: 'button.card:has-text("Renew Now")',
	renewAnnuallyCardButton: 'button.card:has-text("Renew Annually")',

	cancelAndRefundButton: 'a:text("Cancel Subscription and Refund")',
	cancelSubscriptionButton: 'button:text("Cancel Subscription")',
	upgradeButton: 'a.card:text("Upgrade")',

	// Purchased item actions: domains
	deleteDomainCard: 'a:has-text("Delete your domain permanently")',
	cancelDomainButton: 'button:has-text("Cancel Domain and Refund")',
	cancelDomainReasonOption: 'select.confirm-cancel-domain__reasons-dropdown',
	cancelDomainReasonTextArea: 'textarea.confirm-cancel-domain__reason-details',
	cancelDomainCheckbox: 'input.form-checkbox',

	// Cancellation survey
	whyCancelOptions: 'select[id="inspector-select-control-0"]',
	whyCancelAnotherReasonInput: 'input[id="inspector-text-control-0"]',

	whereNextOptions: 'select[id="inspector-select-control-1"]',
	whereNextAnotherReasonInput: 'input[id="inspector-text-control-1"]',

	// Cancellation
	dismissBanner: '.calypso-notice__dismiss',
};

/**
 * Page representing the Purchases page for an individual purchased item (Upgrades >> Purchases >> Select a plan/purchase)
 */
export class IndividualPurchasePage {
	private page: Page;

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

	/**
	 * Validates the title of the purchase that this purchase page is for. Throws if it is not the expected title.
	 *
	 * @param {string} expectedPurchaseTitle Expected text for the title of the purchase.
	 * @throws If the expected purchase title is not the title on the page.
	 */
	async validatePurchaseTitle( expectedPurchaseTitle: string ): Promise< void > {
		// TODO: there's a bug making a web requests on this page go really slow (~35 seconds) -- see issue #55028. Remove extra timeout once fixed.
		await this.page.waitForSelector( selectors.purchaseTitle( expectedPurchaseTitle ), {
			timeout: 60 * 1000,
		} );
	}

	/**
	 * Renew purchase by clicking on the "Renew Now" or "Renew Annually" card button (as opposed to the inline "Renew now" button).
	 */
	async clickRenewNowCardButton(): Promise< void > {
		// This triggers a real navigation to the `/checkout/<site_name>` endpoint.
		await Promise.all( [
			this.page.waitForNavigation( { url: /.*\/checkout.*/ } ),
			await Promise.race( [
				this.page.click( selectors.renewNowCardButton ),
				this.page.click( selectors.renewAnnuallyCardButton ),
			] ),
		] );
	}

	/**
	 * Click the "Upgrade" button to navigate to the plans page.
	 */
	async clickUpgradeButton(): Promise< void > {
		await this.page.click( selectors.upgradeButton );
	}

	/**
	 * Toggle off domain auto renew.
	 */
	async turnOffAutoRenew(): Promise< void > {
		await this.page.click( selectors.autoRenewToggle );
		await this.page.click( selectors.modalConfirm );
		await this.page.click( selectors.radioButton( 'not-sure' ) );
		await this.page.click( selectors.modalSubmit );
		await this.page.waitForSelector( ':text-matches("successfully")' );
	}

	/* Domain Purchase actions */

	/**
	 * If the individual purchase shown on page is of type domain,
	 * execute the delete domain flow.
	 *
	 * Note that flow of domain cancellation is different depending on
	 * whether the domain is within the cancellatio and refund window
	 * or not.
	 *
	 * This flow is for domains that are within the refund window.
	 * Test developers intending to cancel a domain past this window
	 * should implement such path.
	 */
	async deleteDomain(): Promise< void > {
		await this.page.click( selectors.deleteDomainCard );
		await this.page.click( selectors.cancelDomainButton );
		await this.page.selectOption( selectors.cancelDomainReasonOption, 'other' );
		await this.page.fill( selectors.cancelDomainReasonTextArea, 'e2e testing' );
		await this.page.check( selectors.cancelDomainCheckbox );

		await Promise.all( [
			// Extended timeout here due to this process often taking long time for
			// domain-only accounts.
			this.page.waitForNavigation( { timeout: 60000 } ),
			this.page.click( selectors.button( 'Cancel Domain' ) ),
		] );
	}

	/* Cancellations */

	/**
	 * Cancel the purchase.
	 */
	async cancelPurchase(): Promise< void > {
		await Promise.all( [
			this.page.waitForNavigation(),
			this.page.click( selectors.cancelAndRefundButton ),
		] );

		await this.page.click( selectors.cancelSubscriptionButton );

		await this.completeSurvey();
		await this.page.click( selectors.button( 'Cancel plan' ) );

		// It takes a second for the plan cancellation to propagate on the backend after cancelling.
		// If you go too fast, you won't be able to close an account, because it thinks there is still an active plan.
		// Waiting for the notification banner is not accurate - it shows immediately regardless.
		// Waiting for the disappearance of the placeholder for currently active upgrades, ultimately then showing the new true status of upgrades, is much more accurate.
		await this.page.waitForSelector( selectors.purchasePlaceholder, { state: 'detached' } );

		// Still dismiss the banner though, it hangs for a while and can eat clicks if not careful.
		await this.page.click( selectors.dismissBanner );
	}

	/**
	 * Fill out and submit the cancellation survey.
	 */
	private async completeSurvey(): Promise< void > {
		const reason = 'e2e testing';

		await this.page.selectOption( selectors.whyCancelOptions, { value: 'anotherReasonOne' } );
		await this.page.fill( selectors.whyCancelAnotherReasonInput, reason );

		await this.page.selectOption( selectors.whereNextOptions, { value: 'anotherReasonTwo' } );
		await this.page.fill( selectors.whereNextAnotherReasonInput, reason );
	}
}