Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Playwright LocatorParser

From Leeroopedia
Revision as of 11:37, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Microsoft_Playwright_LocatorParser.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Template:Implementation Page

Overview

LocatorParser parses locator strings from various programming languages (JavaScript, Python, Java, C#) back into Playwright selectors, enabling the codegen and inspector tools to work with human-readable locator expressions.

Description

The parseLocator function takes a locator expression written in any supported language and converts it back into a Playwright internal selector string. It handles:

  • Template string parsing with escape sequence support
  • Regular expression literal parsing
  • Language-specific method names (e.g., getByRole vs get_by_role)
  • Role enum values (e.g., AriaRole.Button to 'button')
  • Quote style detection for round-trip fidelity

The parser uses a template-based approach where string literals are extracted and replaced with placeholders, then the template is matched against known locator patterns.

Usage

Used by the codegen/inspector tool to parse locator expressions entered by users.

Code Reference

Source Location

packages/playwright-core/src/utils/isomorphic/locatorParser.ts (248 lines)

Function Signature

function parseLocator(locator: string, testIdAttributeName: string): {
  selector: string;
  preferredQuote: Quote | undefined;
};

Import

import { parseLocator } from '../utils/isomorphic/locatorParser';

I/O Contract

Inputs

  • locator: string -- locator expression in any supported language
  • testIdAttributeName: string -- the configured test ID attribute

Outputs

  • selector: string -- internal Playwright selector string
  • preferredQuote -- the quote style used in the original expression

Supported Patterns

  • getByRole, getByText, getByLabel, getByPlaceholder
  • getByAltText, getByTitle, getByTestId
  • locator with CSS selectors
  • Chained locators with .filter(), .nth(), etc.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment