Skip to main content
rule official · GitHub

playwright-python — GitHub Copilot rule: what it does, how to install

Guides GitHub Copilot to write Python browser tests with Pytest. Enforces role-based locators, auto-retrying assertions, and clean test structure.

Install into GitHub Copilot · Manually: put the files in place All methods and platforms ↓
.github/instructions/playwright-python.instructions.md
Open on GitHub

What playwright-python does

This rule instructs the assistant to build browser tests using official Playwright Python guidelines. It prioritizes accessible selectors like get_by_role, get_by_label, and get_by_text instead of fragile CSS selectors. Each test imports Page and expect from playwright.sync_api and relies on the standard page: Page fixture.

The guidelines enforce web assertions with automatic retries over standard Python assert calls. That means checks like to_have_text(), to_have_count(), and to_have_url(). Arbitrary sleeps and custom timeouts are barred in favor of Playwright's built-in waiting logic. Test files live in tests/ matching test_<feature-or-page>.py to run via pytest.

The rule mandates playwright.sync_api imports directly. It fails if your repository relies on Playwright's asynchronous API.

What it helps with

  • generate Playwright tests following official Python best practices
  • replace brittle selectors with user-facing role locators
  • swap standard asserts for auto-retrying expect checks
  • prevent arbitrary sleep calls and hard-coded timeouts

Example prompts for the agent

  • Write a Playwright test to verify the landing page heading and navigation
  • Create test_login.py using get_by_role selectors and the page fixture
  • Add a Pytest test checking URL redirect with expect

How to install playwright-python

Manually: put the files in place

.github/instructions/playwright-python.instructions.md

The applyTo field sets which files it applies to

Step by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use

Questions about playwright-python

How do I install playwright-python in GitHub Copilot?

Save the instruction file to .github/instructions/playwright-python.instructions.md in your repository. GitHub Copilot will automatically apply these instructions when generating tests.

What does playwright-python do?

It instructs GitHub Copilot to use Playwright's synchronous API, user-facing role locators, and Pytest conventions instead of brittle selectors or hard-coded delays.

Does this rule support asynchronous Playwright?

The rule explicitly requires from playwright.sync_api import Page, expect, making it suitable only for synchronous Pytest setups rather than asyncio.

Source text

playwright-python.instructions.md · MIT
---
description: 'Playwright Python AI test generation instructions based on official documentation.'
applyTo: '**'
---

# Playwright Python Test Generation Instructions

## Test Writing Guidelines

### Code Quality Standards
- **Locators**: Prioritize user-facing, role-based locators (get_by_role, get_by_label, get_by_text) for resilience and accessibility.
- **Assertions**: Use auto-retrying web-first assertions via the expect API (e.g., expect(page).to_have_title(...)). Avoid expect(locator).to_be_visible() unless specifically testing for a change in an element's visibility, as more specific assertions are generally more reliable.
- **Timeouts**: Rely on Playwright's built-in auto-waiting mechanisms. Avoid hard-coded waits or increased default timeouts.
- **Clarity**: Use descriptive test titles (e.g., def test_navigation_link_works():) that clearly state their intent. Add comments only to explain complex logic, not to describe simple actions like "click a button."

### Test Structure
- **Imports**: Every test file should begin with from playwright.sync_api import Page, expect.
- **Fixtures**: Use the page: Page fixture as an argument in your test functions to interact with the browser page.
- **Setup**: Place navigation steps like page.goto() at the beginning of each test function. For setup actions shared across multiple tests, use standard Pytest fixtures.

### File Organization
- **Location**: Store test files in a dedicated tests/ directory or follow the existing project structure.
- **Naming**: Test files must follow the test_<feature-or-page>.py naming convention to be discovered by Pytest.
- **Scope**: Aim for one test file per major application feature or page.

## Assertion Best Practices
- **Element Counts**: Use expect(locator).to_have_count() to assert the number of elements found by a locator.
- **Text Content**: Use expect(locator).to_have_text() for exact text matches and expect(locator).to_contain_text() for partial matches.
- **Navigation**: Use expect(page).to_have_url() to verify the page URL.
- **Assertion Style**: Prefer `expect` over `assert` for more reliable UI tests.


## Example

```python
import re
import pytest
from playwright.sync_api import Page, expect

@pytest.fixture(scope="function", autouse=True)
def before_each_after_each(page: Page):
    # Go to the starting url before each test.
    page.goto("https://playwright.dev/")

def test_main_navigation(page: Page):
    expect(page).to_have_url("https://playwright.dev/")

def test_has_title(page: Page):
    # Expect a title "to contain" a substring.
    expect(page).to_have_title(re.compile("Playwright"))

def test_get_started_link(page: Page):
    page.get_by_role("link", name="Get started").click()
    
    # Expects page to have a heading with the name of Installation.
    expect(page.get_by_role("heading", name="Installation")).to_be_visible()
```

## Test Execution Strategy

1. **Execution**: Tests are run from the terminal using the pytest command.
2. **Debug Failures**: Analyze test failures and identify root causes

© github · MIT · github/awesome-copilot

Similar IDE rules

All IDE rules for Testing and QA →

From the same set861

The whole set →
power-bi-custom-visuals-development A GitHub Copilot rule that gives the agent patterns for building Power BI custom visuals with React, D3.js and TypeScript. ★ 40,418repo stars power-apps-code-apps Guides GitHub Copilot on Power Apps Code Apps standards using TypeScript, React, and Vite. Enforces correct SDK usage, PAC CLI structures, and Power BI embedding. ★ 40,418repo stars power-apps-canvas-yaml This rule guides GitHub Copilot when editing Canvas Apps YAML and Power Fx formulas. Use it to shape screens, controls, components, and data connections. ★ 40,418repo stars playwright-typescript This rule tells Copilot how to write Playwright tests: role-based locators, auto-retrying assertions, one file per feature. Useful when an agent writes the tests. ★ 40,418repo stars playwright-dotnet This rule guides GitHub Copilot when it creates or edits Playwright tests in C#. Use it to shape test structure, locators, and assertions. ★ 40,418repo stars php-symfony This rule steers Copilot toward official Symfony practices: project layout, dependency injection, Doctrine, Twig and testing. Useful when you keep re-explaining conventions in chat. ★ 40,418repo stars php-mcp-server Instructions for GitHub Copilot on developing Model Context Protocol servers in PHP 8.2+. Guides the assistant to use official SDK attributes and clean project structures. ★ 40,418repo stars performance-optimization This rule helps spot web performance issues tied to Core Web Vitals and review possible fixes. Use it when working on page loading, rendering, or layout stability. ★ 40,418repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor