Practice Learn Playwright BDD (Cucumber)
3 lectures
Curriculum . BDD with Cucumber + Playwright

Playwright BDD with Cucumber

Write tests that read like the requirements your product manager just emailed you. This track wires Cucumber-JS on top of Playwright with the Page Object Model, so the Given / When / Then story stays close to business intent and the step implementations stay close to clean Playwright code. Three lectures, one ready-to-clone framework, every example wired against our TTACart sandbox.

3
Lectures
Setup, data-driven, CI + env. Built sequentially.
24
Drill exercises
Eight per lecture, hint reveal under each.
~25
Diagrams
Mermaid + hand-styled inline SVG, lecture-board feel.
1
Framework branch
cucumber-bdd-pom on Advance-Playwright-Framework.

01What is BDD

Behaviour-Driven Development is the practice of writing tests as conversations between a tester, a developer, and a business analyst. The artefact those three people agree on is a .feature file written in Gherkin: Given (the state before the action), When (the action), and Then (the expected outcome). It is not a clever way to write JavaScript - it is a clever way to make a single document that is readable by everyone in the room.

The BDD pyramid below shows the three audiences. The top says "this is what the system should do" in plain English. The middle says "this is how each English sentence wires to runnable code". The base says "this is the Page Object that touches the browser".

Feature files (.feature) PM + BA + QA + Dev all read this Step definitions QA + Dev. Glue between English and code. Page Objects + Playwright QA / SDET. Touches the actual DOM.
The BDD pyramid - one feature file at the top fans out into many step definitions and page objects below.

BDD is not the same as "writing tests". You can write traditional Playwright tests and call them behaviour-driven if the test names match user stories, but the moment a product manager wants to edit a test without help, you need the Gherkin syntax. The whole point of Cucumber is that the highest-value-readers can change the test, not just the engineers.

What you get when you combine BDD + Playwright

  • Business-readable specs. A non-engineer can open login.feature and tell whether the rule matches the requirement.
  • Single source of truth. The same file is the requirement, the test, and the acceptance criterion.
  • Playwright's power underneath. Auto-waiting, network interception, traces, web-first assertions - none of it is lost. Cucumber is a thin orchestration layer; Playwright still drives the browser.
  • Living documentation. The HTML report Cucumber emits doubles as the regression report. If a step is red, the story is red.

02Cucumber-JS + Playwright in one sentence

Cucumber-JS reads .feature files, matches each English line to a JavaScript function (a step definition), and runs them. Those JavaScript functions drive Playwright. The Page Object Model wraps Playwright into per-page classes (LoginPage, InventoryPage) so the step definitions stay one-liners.

flowchart LR
  Feature[login.feature
Given I am on the login page
When I sign in as standard_user
Then I should see the products page]
  Steps[loginSteps.ts
Given/When/Then arrow functions]
  POM[LoginPage.ts
page.getByTestId('username').fill(...)]
  PW[Playwright runtime
Browser - DOM]
  Feature -->|Cucumber matches text| Steps
  Steps -->|Step calls method| POM
  POM -->|POM calls Playwright API| PW
  PW -->|Result + screenshot| POM
  POM -->|Returns to step| Steps
  Steps -->|Pass / fail| Feature
Gherkin -> Step definition -> Page Object -> Playwright - the four hops every BDD scenario takes.

Why not just plain Playwright Test?

Cucumber adds overhead. You have an extra layer to maintain. You have to keep the Gherkin in sync with the steps. You pay that cost when your team includes non-engineers who need to read and edit the spec, or when the test suite is the acceptance criterion the business signs off on. For pure engineer-only teams, plain Playwright Test is usually a better fit. For mixed teams or SDET interviews where a "real-world BDD framework" question is expected, Cucumber + Playwright + POM is the canonical answer.

Layer Owner File Editable by non-engineer?
FeaturePM + BA + QAfeatures/login.featureYes
Step definitionsQA + SDETstep-definitions/login.steps.tsNo
Page ObjectSDET + Devpages/LoginPage.tsNo
Cucumber configSDET leadcucumber.cjsNo
@cucumber/cucumber playwright@latest ts-node dotenv cucumber-html-reporter

03The three lectures

Each lecture is a self-contained page with a live walkthrough, ~5 mermaid diagrams, ~3 hand-styled SVGs and 8 drill exercises. Plan to spend 60-90 minutes per lecture, including the drills.

01

Setup + POM walkthrough

Project scaffold, folder structure, your first .feature file, step definitions, custom World, hooks, and a Page Object Model for the TTACart login page. First green run on npx cucumber-js.

Lecture 1 Open lecture
02

Data-driven tests

Scenario Outlines, Examples tables, type coercion in step parameters, @tags, tag expressions, multi-feature parallel runs, and a products-sort data-driven scenario.

Lecture 2 Open lecture
03

CI + tags + env

dotenv for .env.dev / .env.staging / .env.prod, npm scripts per tag and per environment, failure screenshots, HTML reports, and a GitHub Actions matrix that fans out across environments.

Lecture 3 Open lecture

04Clone the framework

Every code sample in this track lines up with the cucumber-bdd-pom branch of the Advance Playwright Framework. Clone it once, keep it open in a second editor window as you read the lectures.

Advance Playwright Framework - cucumber-bdd-pom branch

A reference project that pairs Cucumber-JS, Playwright, TypeScript, dotenv, and an HTML reporter. Step definitions live next to the features; pages live in pages/.

git clone -b cucumber-bdd-pom https://github.com/PramodDutta/Advance-Playwright-Framework.git
cd Advance-Playwright-Framework
npm install
npx cucumber-js

05How the pieces fit together

The Cucumber-JS runtime owns the lifecycle. It reads features, finds matching step definitions, calls hooks around each scenario, and writes the report at the end. Your World object carries shared state between steps - usually the Playwright Page, the Browser, and a BrowserContext.

flowchart TB
  subgraph "Cucumber-JS lifecycle"
    A[Read cucumber.cjs] --> B[Load features]
    B --> C[For each Scenario]
    C --> D[Before hooks]
    D --> E[Run each step]
    E --> F[After hooks - screenshot on fail]
    F --> G[Next scenario]
  end
  E -->|step body| W[World instance]
  W -->|page, context| POM[Page Object class]
  POM -->|locator + action| PW[Playwright]
Lifecycle - Cucumber drives, World carries state, Page Object talks to Playwright.
login.feature - business readable Gherkin login.steps.ts - Cucumber-JS glue CustomWorld - shared Page, Context, env LoginPage / InventoryPage - POM classes Below: Playwright APIs - browser.newContext, page.goto, expect()
The five layers of a BDD + Playwright + POM framework. Each row uses only the row below it.
Mental model. The feature file is a contract. The step file is the translator. The POM is the receptionist who knows the building. Playwright is the courier who actually walks through the door.

06When to reach for BDD

  • Mixed team. Your PM, BA, or QA lead wants to read or edit the spec without JavaScript knowledge.
  • Acceptance criteria as tests. The story's "Definition of Done" is the green scenario.
  • Living documentation needed. Regulated industry, audit, or onboarding doc all read from the same source.
  • Interview / portfolio piece. A clean Cucumber + Playwright + POM repo is the fastest way to demonstrate framework design at an SDET interview.

When not to reach for it: a small engineering team that only owns the spec, a project where the spec is more easily expressed in test code than in English, or anywhere the extra Gherkin layer is pure overhead.