| import { Frame, Page } from 'playwright'; |
| import { getCalypsoURL } from '../../data-helper'; |
| import envVariables from '../../env-variables'; |
| import type { PaymentDetails, RegistrarDetails } from '../../types/data-helper.types'; |
|
|
| const selectors = { |
| |
| modalContinueButton: '.checkout-modal__content button:text("Continue")', |
|
|
| |
| dismissBanner: 'button[aria-label="Dismiss"]', |
|
|
| |
| cartItem: ( itemName: string ) => |
| `[data-testid="review-order-step--visible"] .checkout-line-item >> text=${ itemName.trim() }`, |
| removeCartItemButton: ( itemName: string ) => |
| `[data-testid="review-order-step--visible"] button[aria-label*="Remove ${ itemName.trim() } from cart"]`, |
| cartItems: '[data-testid="review-order-step--visible"] .checkout-line-item', |
|
|
| |
| editOrderButton: 'button[aria-label="Edit your order"]', |
| editPaymentStep: 'button[aria-label="Edit the payment method"]', |
| removeCouponButton: ( coupon: string ) => |
| `button[aria-label="Remove Coupon: ${ coupon } from cart"]`, |
| saveOrderButton: 'button[aria-label="Save your order"]', |
|
|
| |
| firstNameInput: 'input[aria-describedby="validation-field-first-name"]', |
| lastNameInput: 'input[aria-describedby="validation-field-last-name"]', |
| phoneInput: 'input[name="phone"]', |
| phoneSelect: 'select.phone-input__country-select', |
| countrySelect: 'select[aria-describedby="country-selector-description"]', |
| addressInput: 'input[aria-describedby="validation-field-address-1"]', |
| cityInput: 'input[aria-describedby="validation-field-city"]', |
| stateSelect: 'select[aria-describedby="validation-field-state"]', |
| postalCodeInput: 'input[aria-describedby="validation-field-postal-code"]', |
| submitRegistrarInformationButton: |
| 'button[aria-label="Continue with the entered contact details"]', |
|
|
| |
| countryCode: 'select[aria-labelledby="country-selector-label"]', |
| postalCode: 'input[id="contact-postal-code"]', |
| submitBillingInformationButton: |
| '[data-testid="contact-form--visible"] button.checkout-button.is-status-primary', |
|
|
| |
| existingCreditCard: ( cardHolderName: string ) => |
| `label[for*="existingCard"]:has-text("${ cardHolderName }")`, |
|
|
| |
| cardholderName: 'input[id="cardholder-name"]', |
| cardNumberFrame: 'iframe[title="Secure card number input frame"]', |
| cardNumberInput: 'input[data-elements-stable-field-name="cardNumber"]', |
| cardExpiryFrame: 'iframe[title="Secure expiration date input frame"]', |
| cardExpiryInput: 'input[data-elements-stable-field-name="cardExpiry"]', |
| cardCVVFrame: 'iframe[title="Secure CVC input frame"]', |
| cardCVVInput: 'input[data-elements-stable-field-name="cardCvc"]', |
|
|
| |
| couponCodeInputButton: 'button:text("Have a coupon?"):visible', |
| couponCodeInput: 'input[id="order-review-coupon"]', |
| couponCodeApplyButton: 'button:text("Apply")', |
| disabledButton: 'button[disabled]:has-text("Processing")', |
| paymentButton: '.checkout-submit-button button', |
| totalAmount: |
| envVariables.VIEWPORT_NAME === 'mobile' |
| ? '.wp-checkout__total-price' |
| : '.wp-checkout-order-summary__total-price', |
| purchaseButton: '.checkout-submit-button button:has-text("Pay")', |
| thirdPartyDeveloperCheckboxLabel: |
| 'You agree that an account may be created on a third party developer’s site related to the products you have purchased.', |
|
|
| |
| closeLeaveButton: 'button:text("Leave items")', |
| closeEmptyCartButton: 'button:text("Empty cart")', |
| }; |
|
|
| |
| |
| |
| export class CartCheckoutPage { |
| private page: Page; |
|
|
| |
| |
| |
| |
| |
| constructor( page: Page ) { |
| this.page = page; |
| } |
|
|
| |
| |
| |
| |
| |
| async visit( blogName: string ): Promise< void > { |
| await this.page.goto( getCalypsoURL( `checkout/${ blogName }` ), { |
| waitUntil: 'networkidle', |
| timeout: 20 * 1000, |
| } ); |
| } |
|
|
| |
| |
| |
| async validatePaymentForm(): Promise< void > { |
| const cardholderNameLocator = this.page.locator( selectors.cardholderName ); |
| await cardholderNameLocator.waitFor( { state: 'visible', timeout: 20 * 1000 } ); |
| } |
| |
| |
| |
| |
| |
| |
| async validateCartItem( expectedCartItemName: string ): Promise< void > { |
| await this.page.waitForSelector( selectors.cartItem( expectedCartItemName ) ); |
| } |
|
|
| |
| |
| |
| |
| |
| async removeCartItem( cartItemName: string ): Promise< void > { |
| await this.page.click( selectors.removeCartItemButton( cartItemName ) ); |
| await this.page.click( selectors.modalContinueButton ); |
| } |
|
|
| |
| |
| |
| async validateCartItemsCount( totalItems: number ): Promise< void > { |
| await this.page.waitForSelector( selectors.cartItems ); |
| const cartItemsLocator = this.page.locator( selectors.cartItems ); |
| const itemsCount = await cartItemsLocator.count(); |
| if ( itemsCount !== totalItems ) { |
| throw new Error( `Expected ${ totalItems } items in cart, but found ${ itemsCount }` ); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| async getPaymentButtonText(): Promise< string > { |
| const elementHandle = await this.page.waitForSelector( selectors.paymentButton ); |
| return await elementHandle.innerText(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async enterCouponCode( coupon: string ): Promise< void > { |
| await this.page.click( selectors.couponCodeInputButton ); |
|
|
| await this.page.fill( selectors.couponCodeInput, coupon ); |
| await this.page.click( selectors.couponCodeApplyButton ); |
|
|
| |
| |
| await this.page.waitForSelector( selectors.disabledButton, { state: 'hidden' } ); |
| if ( await this.page.isVisible( selectors.dismissBanner ) ) { |
| await this.page.click( selectors.dismissBanner ); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| async removeCouponCode( coupon: string ): Promise< void > { |
| await this.page.click( selectors.editOrderButton ); |
| await this.page.click( selectors.removeCouponButton( coupon ) ); |
| await this.page.click( selectors.modalContinueButton ); |
| await this.page.click( selectors.saveOrderButton ); |
|
|
| |
| await this.page.waitForSelector( selectors.disabledButton, { state: 'hidden' } ); |
| if ( await this.page.isVisible( selectors.dismissBanner ) ) { |
| await this.page.click( selectors.dismissBanner ); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async getCheckoutTotalAmount( { rawString = false }: { rawString?: boolean } = {} ): Promise< |
| number | string |
| > { |
| const totalAmountLocator = this.page.locator( selectors.totalAmount ); |
| await totalAmountLocator.waitFor( { timeout: 20 * 1000 } ); |
|
|
| const stringAmount = await totalAmountLocator.innerText(); |
| if ( rawString ) { |
| |
| return stringAmount; |
| } |
|
|
| const parsedAmount = stringAmount.replace( /,/g, '' ).match( /\d+\.?\d*/g ); |
| if ( ! parsedAmount?.length ) { |
| throw new Error( 'Unable to locate or parse cart amount.' ); |
| } |
| return parseFloat( parsedAmount.pop() as string ); |
| } |
|
|
| |
| |
| |
| |
| |
| async enterDomainRegistrarDetails( registrarDetails: RegistrarDetails ): Promise< void > { |
| await this.page.fill( selectors.firstNameInput, registrarDetails.firstName ); |
| await this.page.fill( selectors.lastNameInput, registrarDetails.lastName ); |
| await this.page.selectOption( selectors.phoneSelect, registrarDetails.countryCode ); |
| await this.page.fill( selectors.phoneInput, registrarDetails.phone ); |
| await this.page.selectOption( selectors.countrySelect, registrarDetails.countryCode ); |
| await this.page.fill( selectors.addressInput, registrarDetails.address ); |
| await this.page.fill( selectors.cityInput, registrarDetails.city ); |
| await this.page.selectOption( selectors.stateSelect, registrarDetails.stateCode ); |
| await this.page.fill( selectors.postalCodeInput, registrarDetails.postalCode ); |
| await this.page.click( selectors.submitRegistrarInformationButton ); |
| } |
|
|
| |
| |
| |
| |
| |
| async enterBillingDetails( paymentDetails: PaymentDetails ): Promise< void > { |
| await this.page.fill( selectors.postalCode, paymentDetails.postalCode ); |
| await this.page.selectOption( selectors.countryCode, paymentDetails.countryCode ); |
| await this.page.click( selectors.submitBillingInformationButton ); |
| } |
|
|
| |
| |
| |
| |
| |
| async selectSavedCard( cardHolderName: string ): Promise< void > { |
| |
| |
| |
| |
| |
| |
| const cardSelector = this.page |
| .locator( selectors.existingCreditCard( cardHolderName ) ) |
| .first(); |
| const editPaymentButton = this.page.locator( selectors.editPaymentStep ); |
|
|
| await cardSelector.or( editPaymentButton ).first().waitFor( { state: 'visible' } ); |
|
|
| if ( await editPaymentButton.isVisible() ) { |
| await editPaymentButton.click(); |
| } |
|
|
| await cardSelector.click(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| async enterPaymentDetails( paymentDetails: PaymentDetails ): Promise< void > { |
| |
| |
| const cardInputLocator = this.page.locator( 'span:has-text("Credit or debit card")' ); |
| await cardInputLocator.click(); |
|
|
| |
| |
| await this.page.fill( selectors.cardholderName, paymentDetails.cardHolder ); |
|
|
| const cardNumberFrameHandle = await this.page.waitForSelector( selectors.cardNumberFrame ); |
| const cardNumberFrame = ( await cardNumberFrameHandle.contentFrame() ) as Frame; |
| const cardNumberInput = await cardNumberFrame.waitForSelector( selectors.cardNumberInput ); |
| await cardNumberInput.fill( paymentDetails.cardNumber ); |
|
|
| const expiryFrameHandle = await this.page.waitForSelector( selectors.cardExpiryFrame ); |
| const expiryFrame = ( await expiryFrameHandle.contentFrame() ) as Frame; |
| const expiryInput = await expiryFrame.waitForSelector( selectors.cardExpiryInput ); |
| await expiryInput.fill( `${ paymentDetails.expiryMonth }${ paymentDetails.expiryYear }` ); |
|
|
| const cvvFrame = ( await ( |
| await this.page.waitForSelector( selectors.cardCVVFrame ) |
| ).contentFrame() ) as Frame; |
| const cvvInput = await cvvFrame.waitForSelector( selectors.cardCVVInput ); |
| await cvvInput.fill( paymentDetails.cvv ); |
| } |
|
|
| |
| |
| |
| async purchase( { timeout }: { timeout?: number } = {} ): Promise< void > { |
| await Promise.all( [ |
| this.page.waitForResponse( /.*me\/transactions.*/, { timeout: timeout } ), |
| this.page.click( selectors.purchaseButton ), |
| ] ); |
| } |
|
|
| |
| |
| |
| |
| |
| async closeCheckout( leaveItems: boolean ): Promise< void > { |
| await this.page.getByRole( 'button', { name: 'Close Checkout' } ).click(); |
| await Promise.all( [ |
| this.page.click( leaveItems ? selectors.closeLeaveButton : selectors.closeEmptyCartButton ), |
| ] ); |
| } |
| } |
|
|