agentleFS
RoninForgeRoninForge/roninforge-playwright/skills/playwright-new-test/SKILL.md

playwright-new-test

Skill0 starsChanged 7 months agoMITView on GitHub

Scaffold a new Playwright test the modern way: semantic locators (getByRole over CSS), web-first assertions (await expect(loc).toBeVisible()), test.extend fixtures (not beforeEach), POM-via-fixture for repeated screens, async-arrow signature with destructured fixtures, and the canonical test file layout.

  • Reads credentials

The file

We split this skill into the rules it gives an agent

5 rules. About 0 are common, 1 is distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  • could be automatic: a setting could enforce it
  1. Very specific:

    Use when generating a new *.spec.ts file or adding a new test to an existing spec.

    line 10
  2. Very specific:

    Fixtures over beforeEach: if you'd write beforeEach to set up a value, write a fixture instead.

    line 108
  3. Very specific:

    test.describe for grouping: when the file has more than one logical scenario.

    line 112
  4. Distinctive:

    Add web-first assertions at every meaningful state transition.

    line 119
  5. Very specific:

    Run npx playwright test --ui locally before committing.

    line 120

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Scaffold a New Playwright Test

When to Use

Use when generating a new *.spec.ts file or adding a new test to an existing spec. The output should be CI-grade from line one - no page.click("text=..."), no waitForTimeout, no beforeEach for what should be a fixture.

Output

For a screen with no shared setup:

// tests/checkout.spec.ts
import { test, expect } from "@playwright/test";

test.describe("checkout flow", () => {
  test("user can place an order", async ({ page }) => {
    await page.goto("/products/widget");
    await page.getByRole("button", { name: "Add to cart" }).click();
    await page.getByRole("link", { name: "Cart" }).click();
    await page.getByRole("button", { name: "Checkout" }).click();

    // Card number from env (every PSP has its own test cards). Expiry/CVC
    // are non-secret test-fixture values - inline literals are fine.
    await page.getByLabel("Card number").fill(process.env.E2E_TEST_CARD!);
    await page.getByLabel("Expiry").fill("12/30");
    await page.getByLabel("CVC").fill("123");
    await page.getByRole("button", { name: "Pay" }).click();

    await expect(page.getByRole("heading", { name: "Thank you" })).toBeVisible();
    await expect(page).toHaveURL(/\/orders\/\w+/);
  });
});

For a screen with repeated setup, use a fixture:

// tests/fixtures.ts
import { test as base, expect } from "@playwright/test";
import { CheckoutPage } from "./pages/checkout.page";

type Fixtures = {
  checkoutPage: CheckoutPage;
};

export const test = base.extend<Fixtures>({
  checkoutPage: async ({ page }, use) => {
    const checkoutPage = new CheckoutPage(page);
    await checkoutPage.goto();
    await use(checkoutPage);
  },
});

export { expect };
// tests/pages/checkout.page.ts
import type { Page, Locator } from "@playwright/test";

export class CheckoutPage {
  readonly cardNumber: Locator;
  readonly expiry: Locator;
  readonly cvc: Locator;
  readonly payButton: Locator;

  constructor(private readonly page: Page) {
    this.cardNumber = page.getByLabel("Card number");
    this.expiry = page.getByLabel("Expiry");
    this.cvc = page.getByLabel("CVC");
    this.payButton = page.getByRole("button", { name: "Pay" });
  }

  async goto() {
    await this.page.goto("/checkout");
  }

  async pay(card: string, expiry: string, cvc: string) {
    await this.cardNumber.fill(card);
    await this.expiry.fill(expiry);
    await this.cvc.fill(cvc);
    await this.payButton.click();
  }
}
// tests/checkout.spec.ts
import { test, expect } from "./fixtures";

test("user can pay", async ({ checkoutPage, page }) => {
  await checkoutPage.pay(process.env.E2E_TEST_CARD!, "12/30", "123");
  await expect(page.getByRole("heading", { name: "Thank you" })).toBeVisible();
});

Rules baked into the scaffold

  1. Imports: test, expect, Page, Locator from @playwright/test only. Never from playwright or playwright-core.
  2. Locators: getByRole first; getByLabel/getByText/getByPlaceholder next; getByTestId last. No CSS, no XPath, no text=.
  3. Assertions: await expect(locator).toBe...(). Never expect(await locator.isVisible()).toBeTruthy(). Always await the assertion.
  4. No timing helpers: no page.waitForTimeout, no page.waitForSelector followed by an action. Locator actions auto-wait.
  5. Fixtures over beforeEach: if you'd write beforeEach to set up a value, write a fixture instead.
  6. POM as locator getters: page objects expose Locator properties, not ElementHandle, and never call expect themselves.
  7. Secrets via env: process.env.E2E_USER! not hardcoded strings.
  8. No test.only: ever. forbidOnly: !!process.env.CI in config will fail CI.
  9. test.describe for grouping when the file has more than one logical scenario; otherwise top-level test() is fine.

Workflow

  1. Identify the screen / flow under test.
  2. Decide: standalone test, or POM-backed fixture? Use POM when the same locators appear in 2+ tests.
  3. Pick semantic locators based on the rendered DOM (use npx playwright codegen <url> to bootstrap, then refactor away the getByText overuse).
  4. Add web-first assertions at every meaningful state transition.
  5. Run npx playwright test --ui locally before committing.
  6. Confirm npx playwright test --reporter=list path/to/spec.ts passes headlessly.

Common mistakes to refuse

  • A beforeEach that constructs a POM and stores it in module-level state. Use a fixture.
  • An await page.waitForTimeout(N) "just to be safe." Replace with the assertion that justifies the wait.
  • A try/catch around an assertion to "make tests more robust." Web-first assertions handle their own retry. Catching the failure hides bugs.
  • A locator like page.locator("body > div:nth-child(3) > .foo"). Refactor to a semantic locator.
  • A hardcoded "alice@example.com" and "hunter2" in the test body. Move to env vars.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.