|
| 1 | +/** |
| 2 | + * Page object for the PalsHub premium-pal purchase flow: |
| 3 | + * browse card -> detail sheet -> Buy -> AuthSheet sign-in -> Download flip. |
| 4 | + */ |
| 5 | + |
| 6 | +import {BasePage} from './BasePage'; |
| 7 | +import {byTestId} from '../helpers/selectors'; |
| 8 | + |
| 9 | +declare const browser: WebdriverIO.Browser; |
| 10 | + |
| 11 | +export class PalPurchasePage extends BasePage { |
| 12 | + private palCard(palId: string): string { |
| 13 | + return byTestId(`palshub-pal-card-${palId}`); |
| 14 | + } |
| 15 | + private get buyButton(): string { |
| 16 | + return byTestId('buy-button'); |
| 17 | + } |
| 18 | + private get downloadButton(): string { |
| 19 | + return byTestId('download-button'); |
| 20 | + } |
| 21 | + private get emailInput(): string { |
| 22 | + return byTestId('email-input'); |
| 23 | + } |
| 24 | + private get passwordInput(): string { |
| 25 | + return byTestId('password-input'); |
| 26 | + } |
| 27 | + private get authSubmit(): string { |
| 28 | + return byTestId('auth-submit-button'); |
| 29 | + } |
| 30 | + |
| 31 | + /** Open the premium pal's detail sheet from the browse list. */ |
| 32 | + async openPalDetail(palId: string, timeout = 20000): Promise<void> { |
| 33 | + await this.tap(this.palCard(palId), timeout); |
| 34 | + await this.waitForElement(this.buyButton, timeout); |
| 35 | + } |
| 36 | + |
| 37 | + async tapBuy(timeout = 15000): Promise<void> { |
| 38 | + const btn = await this.waitForEnabled(this.buyButton, timeout); |
| 39 | + await btn.click(); |
| 40 | + } |
| 41 | + |
| 42 | + /** Fill the AuthSheet email/password and submit. */ |
| 43 | + async fillAndSubmitSignIn( |
| 44 | + email: string, |
| 45 | + password: string, |
| 46 | + timeout = 20000, |
| 47 | + ): Promise<void> { |
| 48 | + await this.typeText(this.emailInput, email, timeout); |
| 49 | + await this.typeText(this.passwordInput, password, timeout); |
| 50 | + await this.dismissKeyboard(); |
| 51 | + await this.tap(this.authSubmit, timeout); |
| 52 | + } |
| 53 | + |
| 54 | + /** Dismiss the post-submit confirmation alert ("OK") if one appears. */ |
| 55 | + async dismissAlertIfPresent(timeout = 4000): Promise<void> { |
| 56 | + const ok = browser.$( |
| 57 | + '-ios predicate string:type == "XCUIElementTypeButton" AND (label == "OK" OR label == "Ok")', |
| 58 | + ); |
| 59 | + try { |
| 60 | + await ok.waitForDisplayed({timeout}); |
| 61 | + await ok.click(); |
| 62 | + } catch { |
| 63 | + // No alert — proceed. |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Tap Buy; if it routes to sign-in, authenticate and retry. Sign-in is async, |
| 69 | + * so a Buy tap before the session settles re-opens the AuthSheet — retry until |
| 70 | + * Buy actually starts checkout (the AuthSheet no longer appears). |
| 71 | + */ |
| 72 | + async signInAndStartCheckout( |
| 73 | + email: string, |
| 74 | + password: string, |
| 75 | + attempts = 4, |
| 76 | + ): Promise<void> { |
| 77 | + for (let i = 0; i < attempts; i++) { |
| 78 | + await this.tapBuy(); |
| 79 | + const authOpened = await this.isElementDisplayed(this.emailInput, 4000); |
| 80 | + if (!authOpened) { |
| 81 | + return; // checkout started |
| 82 | + } |
| 83 | + await this.fillAndSubmitSignIn(email, password); |
| 84 | + await this.dismissAlertIfPresent(); |
| 85 | + await this.waitForElementToDisappear(this.emailInput, 15000).catch( |
| 86 | + () => {}, |
| 87 | + ); |
| 88 | + await browser.pause(2500); // let the session + observable state settle |
| 89 | + } |
| 90 | + throw new Error('Buy kept routing to sign-in; authentication never settled'); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * ASWebAuthenticationSession shows a one-time system consent alert before the |
| 95 | + * page loads. Accept it if present; a no-op otherwise (already granted). |
| 96 | + */ |
| 97 | + async acceptAuthConsentIfPresent(timeout = 8000): Promise<void> { |
| 98 | + const continueBtn = browser.$( |
| 99 | + '-ios predicate string:type == "XCUIElementTypeButton" AND label == "Continue"', |
| 100 | + ); |
| 101 | + try { |
| 102 | + await continueBtn.waitForDisplayed({timeout}); |
| 103 | + await continueBtn.click(); |
| 104 | + } catch { |
| 105 | + // No consent prompt surfaced — proceed. |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** The reconcile poll flips Buy -> Download once ownership is granted. */ |
| 110 | + async waitForDownloadButton(timeout = 30000): Promise<void> { |
| 111 | + await this.waitForElement(this.downloadButton, timeout); |
| 112 | + } |
| 113 | +} |
0 commit comments