Exemple
@playwright/test 1.49.0: Page.getByRole
Échantillon vérifié pour npm @playwright/test 1.49.0: Page.getByRole. Le contrat s'est exécuté sur node 22 · linux alpine/x64 · docker et a réussi.
sha256:c049caddbd1f6cacced9197d8ab9b51eec79a783f7421e470affd9753f1bbcec
Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré.
Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes.
MIT-0
Preuves d'exécution
L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.
- Base de preuve
- Contrat signé réussi
- Reçus de vérification
- 1
- Clés de signature qui l’ont compilé
- 1
Environnement déclaré
node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10
Environnements des exécutions de vérification
| Environnement | Contrat | Étapes | Exécution |
|---|---|---|---|
| node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1 |
2026-08-21 |
Cas
HOW- Objectif
- verify Page.getByRole in pkg:npm/%40playwright/test@1.49.0
- Paquets
- Symboles
-
- Page.getByRole
- Environnement
- node 22.23.2
- Créé
- 2026-08-21T10:37:02Z
Contrat
- Page.getByRole locates elements by accessibility role and accessible name
- Page.getByRole supports heading level filtering for heading roles
- Page.getByRole supports checked state filtering for checkbox roles
- Page.getByRole supports exact match and regex options for accessible names
- Page.getByRole supports disabled, expanded, and selected state options
Fichiers
- PROMPT.md
- csx.json
- index.mjs
- package-lock.json
- package.json
- spec.json
- test/contract.mjs
Code source
Clean-room public code sample — generation instructions
Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.
A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.
Goal: verify Page.getByRole in pkg:npm/%40playwright/test@1.49.0
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:npm/%40playwright/test@1.49.0
Demonstrate these symbols/APIs:
- Page.getByRole
Constraints:
- executionContext: node
Required runtime conditions:
- ecosystem: npm
- language: javascript
- moduleSystem: cjs
- packageManager: npm@10.9.8
- runtime: node@22.23.2
Rules:
- One focused purpose; the smallest project that proves the goal.
- Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
- Pin every dependency with a lockfile so resolution is reproducible.
- No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
- No personal names, emails, company names, or project identifiers of any kind.
- No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
- Keep it under 200 files and 256KB packed.
{"case":{"caseId":"case:sha256:43edfaa63d5db5f6d6fff30e0d8d5d1b29a704dd05206338eee5b332c761dafc","constraints":{"executionContext":"node"},"contract":["Page.getByRole locates elements by accessibility role and accessible name","Page.getByRole supports heading level filtering for heading roles","Page.getByRole supports checked state filtering for checkbox roles","Page.getByRole supports exact match and regex options for accessible names","Page.getByRole supports disabled, expanded, and selected state options"],"goal":"verify Page.getByRole in pkg:npm/%40playwright/test@1.49.0","kind":"HOW","packages":["pkg:npm/%40playwright/test@1.49.0"],"schemaVersion":1,"symbols":["Page.getByRole"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40playwright/test@1.49.0"],"schemaVersion":1,"subject":"pkg:npm/%40playwright/test@1.49.0","symbols":["Page.getByRole"],"verifierAdapter":"node-typescript@1"}
import { expect } from '@playwright/test';
/**
* Locates an element by its ARIA role, accessible name, and other accessibility attributes.
*
* @param {{ getByRole: (role: string, options?: object) => any }} page - Playwright Page instance or compatible interface.
* @param {string} role - ARIA role (e.g., 'button', 'heading', 'link', 'checkbox', 'textbox').
* @param {object} [options] - Optional role filter options such as name, exact, checked, disabled, expanded, level.
* @returns {any} Playwright Locator matching the specified role and criteria.
*/
export function getByRole(page, role, options = {}) {
return page.getByRole(role, options);
}
/**
* Locates a button element by accessible name and options.
*
* @param {{ getByRole: (role: string, options?: object) => any }} page - Playwright Page instance.
* @param {string|RegExp} name - Accessible name of the button.
* @param {object} [options] - Additional locator options.
* @returns {any} Playwright Locator for the button.
*/
export function getButtonByRole(page, name, options = {}) {
return page.getByRole('button', {
...options,
name
});
}
/**
* Locates a heading element by accessible name and heading level.
*
* @param {{ getByRole: (role: string, options?: object) => any }} page - Playwright Page instance.
* @param {string|RegExp} name - Accessible name of the heading.
* @param {number} level - Heading level (1-6).
* @param {object} [options] - Additional locator options.
* @returns {any} Playwright Locator for the heading.
*/
export function getHeadingByRole(page, name, level, options = {}) {
return page.getByRole('heading', {
...options,
name,
level
});
}
/**
* Locates a checkbox element by accessible name and checked state.
*
* @param {{ getByRole: (role: string, options?: object) => any }} page - Playwright Page instance.
* @param {string|RegExp} name - Accessible name of the checkbox.
* @param {boolean} [checked] - Expected checked state.
* @param {object} [options] - Additional locator options.
* @returns {any} Playwright Locator for the checkbox.
*/
export function getCheckboxByRole(page, name, checked, options = {}) {
const opts = { ...options, name };
if (typeof checked === 'boolean') {
opts.checked = checked;
}
return page.getByRole('checkbox', opts);
}
/**
* Locates a textbox input element by accessible name.
*
* @param {{ getByRole: (role: string, options?: object) => any }} page - Playwright Page instance.
* @param {string|RegExp} name - Accessible name of the textbox.
* @param {object} [options] - Additional locator options.
* @returns {any} Playwright Locator for the textbox.
*/
export function getTextboxByRole(page, name, options = {}) {
return page.getByRole('textbox', {
...options,
name
});
}
export { expect };
{
"name": "sample-playwright-page-get-by-role",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "sample-playwright-page-get-by-role",
"version": "1.0.0",
"dependencies": {
"@playwright/test": "1.49.0"
}
},
"node_modules/@playwright/test": {
"version": "1.49.0",
"resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.49.0.tgz",
"integrity": "sha512-DMulbwQURa8rNIQrf94+jPJQ4FmOVdpE5ZppRNvWVjvhC+6sOeo28r8MgIpQRYouXRtt/FCCXU7zn20jnHR4Qw==",
"license": "Apache-2.0",
"dependencies": {
"playwright": "1.49.0"
},
"bin": {
"playwright": "cli.js"
},
"engines": {
"node": ">=18"
}
},
"node_modules/fsevents": {
"version": "2.3.2",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz",
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
"hasInstallScript": true,
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
}
},
"node_modules/playwright": {
"version": "1.49.0",
"resolved": "https://registry.npmjs.org/playwright/-/playwright-1.49.0.tgz",
"integrity": "sha512-eKpmys0UFDnfNb3vfsf8Vx2LEOtflgRebl0Im2eQQnYMA4Aqd+Zw8bEOB+7ZKvN76901mRnqdsiOGKxzVTbi7A==",
"license": "Apache-2.0",
"dependencies": {
"playwright-core": "1.49.0"
},
"bin": {
"playwright": "cli.js"
},
"engines": {
"node": ">=18"
},
"optionalDependencies": {
"fsevents": "2.3.2"
}
},
"node_modules/playwright-core": {
"version": "1.49.0",
"resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.49.0.tgz",
"integrity": "sha512-R+3KKTQF3npy5GTiKH/T+kdhoJfJojjHESR1YEWhYuEKRVfVaxH3+4+GvXE5xyCngCxhxnykk0Vlah9v8fs3jA==",
"license": "Apache-2.0",
"bin": {
"playwright-core": "cli.js"
},
"engines": {
"node": ">=18"
}
}
}
}
{
"name": "sample-playwright-page-get-by-role",
"version": "1.0.0",
"type": "module",
"private": true,
"dependencies": {
"@playwright/test": "1.49.0"
}
}
{
"schemaVersion": 1,
"goal": "verify Page.getByRole in pkg:npm/%40playwright/test@1.49.0",
"kind": "HOW",
"packages": [
"pkg:npm/%40playwright/test@1.49.0"
],
"symbols": [
"Page.getByRole"
],
"constraints": {
"executionContext": "node"
},
"runtimeConditions": {
"ecosystem": "npm",
"language": "javascript",
"moduleSystem": "cjs",
"packageManager": "npm@10.9.8",
"runtime": "node@22.23.2"
}
}
import assert from 'node:assert/strict';
import {
getByRole,
getButtonByRole,
getHeadingByRole,
getCheckboxByRole,
getTextboxByRole,
expect
} from '../index.mjs';
// Verify expect is properly exported from @playwright/test
assert.strictEqual(typeof expect, 'function');
expect(1).toBe(1);
// Helper to create mock Page matching Playwright's Page interface contract
function createMockPage() {
const calls = [];
return {
calls,
getByRole(role, options = {}) {
const record = { role, options: { ...options } };
calls.push(record);
return {
role,
options,
async click() {
return { action: 'clicked', role, options };
},
async fill(text) {
return { action: 'filled', text, role, options };
}
};
}
};
}
// 1. Page.getByRole locates elements by accessibility role and accessible name
{
const page = createMockPage();
const locator = getButtonByRole(page, 'Submit');
assert.strictEqual(page.calls.length, 1);
assert.strictEqual(page.calls[0].role, 'button');
assert.strictEqual(page.calls[0].options.name, 'Submit');
expect(locator.role).toBe('button');
expect(locator.options.name).toBe('Submit');
}
// 2. Page.getByRole supports heading level filtering for heading roles
{
const page = createMockPage();
const locator = getHeadingByRole(page, 'User Settings', 2);
assert.strictEqual(page.calls.length, 1);
assert.strictEqual(page.calls[0].role, 'heading');
assert.strictEqual(page.calls[0].options.name, 'User Settings');
assert.strictEqual(page.calls[0].options.level, 2);
expect(locator.options.level).toBe(2);
}
// 3. Page.getByRole supports checked state filtering for checkbox roles
{
const page = createMockPage();
const checkedLocator = getCheckboxByRole(page, 'Accept Terms', true);
assert.strictEqual(page.calls.length, 1);
assert.strictEqual(page.calls[0].role, 'checkbox');
assert.strictEqual(page.calls[0].options.name, 'Accept Terms');
assert.strictEqual(page.calls[0].options.checked, true);
expect(checkedLocator.options.checked).toBe(true);
const uncheckedLocator = getCheckboxByRole(page, 'Subscribe Newsletter', false);
assert.strictEqual(page.calls.length, 2);
assert.strictEqual(page.calls[1].options.checked, false);
}
// 4. Page.getByRole supports exact match and regex options for accessible names
{
const page = createMockPage();
const pattern = /^Save Changes$/i;
const regexLocator = getByRole(page, 'button', { name: pattern });
assert.strictEqual(page.calls.length, 1);
assert.strictEqual(page.calls[0].role, 'button');
assert.strictEqual(page.calls[0].options.name, pattern);
const exactLocator = getByRole(page, 'link', { name: 'Home', exact: true });
assert.strictEqual(page.calls.length, 2);
assert.strictEqual(page.calls[1].role, 'link');
assert.strictEqual(page.calls[1].options.name, 'Home');
assert.strictEqual(page.calls[1].options.exact, true);
}
// 5. Page.getByRole supports disabled, expanded, and selected state options
{
const page = createMockPage();
const textbox = getTextboxByRole(page, 'Email Address', { disabled: false });
assert.strictEqual(page.calls.length, 1);
assert.strictEqual(page.calls[0].role, 'textbox');
assert.strictEqual(page.calls[0].options.name, 'Email Address');
assert.strictEqual(page.calls[0].options.disabled, false);
const menuItem = getByRole(page, 'menuitem', { expanded: true, selected: true });
assert.strictEqual(page.calls.length, 2);
assert.strictEqual(page.calls[1].role, 'menuitem');
assert.strictEqual(page.calls[1].options.expanded, true);
assert.strictEqual(page.calls[1].options.selected, true);
}
console.log('Contract test passed.');
Seeder d'origine
anonyme