First conversion batch: auth, password-reset, resend-verification. verify-email is left alone — it already used semantic locators and duplicated nothing, and rewriting it to prove a point would be churn. Four of the nine copies of "register a customer" go here. auth.spec.ts and password-reset.spec.ts each carried their own `register`, and resend-verification.spec.ts its own `registerCustomer`, all three re-explaining the same bcrypt wait and the same "the header, not the URL, proves the session exists" reasoning in slightly different words. Two also carried their own `logout`, and two their own `uniqueEmail` with different prefixes. Most of those tests were not about registering. They needed an account to exist so they could test logging out, resetting a password, or resending a verification email, and they paid for a bcrypt round-trip through the form to get one. Those now take the `customer` fixture, which registers through the API. The three tests that genuinely are about the registration form still drive it, because the thing under test has to be the thing exercised. The batch drops from 47s to 26s as a side effect, which is the cost of that round-trip made visible. password-reset.spec.ts loses its inline pg.Client. The reasoning for reading the database directly is unchanged and still right — an endpoint returning a reset token for an arbitrary address is account takeover if it is ever reachable — but it now lives in support/db.ts where it cannot be copied into the next spec wanting a shortcut. It also stops defaulting to port 55432, which is the integration suite's disposable Postgres rather than the database the app under test is connected to, and is Hyper-V-reserved on at least one machine here. Both tests in that file previously failed with a bare ECONNREFUSED unless TEST_PGPORT was set by hand; they now pass with no environment at all. New PasswordResetPages object covers both halves of recovery — requesting a link, and using one — because they are one flow and a test usually crosses between them. One lint decision worth recording. Requesting a Playwright fixture IS using it: destructuring `customer` is what makes the account exist, whether or not the body then reads the address. The linter cannot see that side effect and reports every such fixture as an unused variable. The first attempt at appeasing it was a `void customer;` line per test, which is noise standing in for a comment — and sonarjs flags that too, so it traded one warning for another. `no-unused-vars` is now configured with `args: 'none'` for tests only, with the reason written next to it. Variables are still checked; only parameters are exempt. Verified: 26/26 in the converted batch, and 127 passed in the full suite with four failures — three in the known #116 flaky family, and resend-verification's rate-limit test, which passes 9/9 across three repeats in isolation and is timing-sensitive under parallel load rather than changed by this commit. Refs #137
113 lines
4.2 KiB
TypeScript
113 lines
4.2 KiB
TypeScript
import { Locator, Page, expect } from '@playwright/test';
|
|
|
|
/**
|
|
* The register / log in form.
|
|
*
|
|
* It is a modal wherever it appears — over the storefront at /register and
|
|
* /login, and over whatever the customer was doing when they clicked Add to
|
|
* Cart while signed out. One object covers all three, because it is one
|
|
* component (`AuthForm`, shared by `AuthRouteModal` and `AuthPromptModal`).
|
|
*
|
|
* Locators are scoped to the dialog rather than the page. The storefront behind
|
|
* the modal has a "Log in" button of its own — the one that opened this — so an
|
|
* unscoped `getByRole('button', { name: 'Log in' })` matches two things and
|
|
* fails on strict mode, which is a confusing way to learn that the modal is a
|
|
* modal.
|
|
*/
|
|
export class AuthModal {
|
|
readonly registerDialog: Locator;
|
|
readonly logInDialog: Locator;
|
|
readonly email: Locator;
|
|
readonly firstName: Locator;
|
|
readonly lastName: Locator;
|
|
readonly password: Locator;
|
|
readonly marketingConsent: Locator;
|
|
readonly createAccountButton: Locator;
|
|
readonly createAccountTab: Locator;
|
|
readonly logInTab: Locator;
|
|
readonly forgotPasswordButton: Locator;
|
|
|
|
constructor(private readonly page: Page) {
|
|
this.registerDialog = page.getByRole('dialog', { name: 'Create an account' });
|
|
this.logInDialog = page.getByRole('dialog', { name: 'Log in' });
|
|
this.email = page.getByRole('textbox', { name: 'Email' });
|
|
this.firstName = page.getByRole('textbox', { name: 'First name' });
|
|
this.lastName = page.getByRole('textbox', { name: 'Last name' });
|
|
this.password = page.getByLabel('Password');
|
|
this.marketingConsent = page.getByRole('checkbox');
|
|
this.createAccountButton = page.getByRole('button', { name: 'Create account' });
|
|
this.createAccountTab = page.getByRole('tab', { name: 'Create Account' });
|
|
this.logInTab = page.getByRole('tab', { name: 'Log In' });
|
|
this.forgotPasswordButton = page.getByRole('button', { name: 'Forgot password?' });
|
|
}
|
|
|
|
/** The submit button inside the log in dialog, not the one that opened it. */
|
|
get submitLogInButton(): Locator {
|
|
return this.logInDialog.getByRole('button', { name: 'Log in' });
|
|
}
|
|
|
|
/** Closes whichever of the two dialogs is open. */
|
|
get closeButton(): Locator {
|
|
return this.page.getByRole('dialog').getByRole('button', { name: 'Close' });
|
|
}
|
|
|
|
/**
|
|
* Signs in through the modal, wherever it was opened from.
|
|
*
|
|
* Scoped to the dialog throughout: the storefront behind it has its own
|
|
* "Log in" button — the one that opened this — and an unscoped fill or click
|
|
* matches both.
|
|
*/
|
|
async logIn(email: string, password: string): Promise<void> {
|
|
await this.logInDialog.getByRole('textbox', { name: 'Email' }).fill(email);
|
|
await this.logInDialog.getByLabel('Password').fill(password);
|
|
await this.submitLogInButton.click();
|
|
}
|
|
|
|
async gotoRegister(): Promise<void> {
|
|
await this.page.goto('/register');
|
|
}
|
|
|
|
async gotoLogIn(): Promise<void> {
|
|
await this.page.goto('/login');
|
|
}
|
|
|
|
/** Fills the registration form without submitting, for tests about validation. */
|
|
async fillRegistration(details: {
|
|
email: string;
|
|
password: string;
|
|
firstName?: string;
|
|
lastName?: string;
|
|
}): Promise<void> {
|
|
await this.email.fill(details.email);
|
|
if (details.firstName !== undefined) await this.firstName.fill(details.firstName);
|
|
if (details.lastName !== undefined) await this.lastName.fill(details.lastName);
|
|
await this.password.fill(details.password);
|
|
}
|
|
|
|
async submitRegistration(): Promise<void> {
|
|
await this.createAccountButton.click();
|
|
}
|
|
|
|
async fillCredentials(email: string, password: string): Promise<void> {
|
|
await this.email.fill(email);
|
|
await this.password.fill(password);
|
|
}
|
|
|
|
async submitLogIn(): Promise<void> {
|
|
await this.submitLogInButton.click();
|
|
}
|
|
|
|
/**
|
|
* Waits for the modal to close, which is what registering or logging in does.
|
|
*
|
|
* The dialog going away is the completion of the action rather than an
|
|
* assertion about it: a test that carries on while the modal is still over
|
|
* the page clicks the modal's backdrop instead of what it meant to.
|
|
*/
|
|
async waitForDismissal(): Promise<void> {
|
|
await expect(this.registerDialog).toHaveCount(0);
|
|
await expect(this.logInDialog).toHaveCount(0);
|
|
}
|
|
}
|