Skip to content

Conversation

@DavertMik
Copy link
Contributor

@DavertMik DavertMik commented Dec 16, 2025

This PR adds support for using JSON strings as locators in CodeceptJS. When a string starts with { and ends with }, it will be parsed as JSON and transformed into a locator object.

Changes Made

  1. Enhanced Locator Class (lib/locator.js)
  • Added parsedJsonAsString() method to handle JSON string parsing
  • Supports simple key-value pairs as requested
  • Falls back gracefully to normal string processing for invalid JSON
  • Marks parsed locators as strict
  1. Comprehensive Test Coverage (test/unit/locator_test.js)
  • Added 12 test cases for JSON string parsing
  • Tests cover various locator types: CSS, XPath, ID, custom locators
  • Includes edge cases: whitespace, invalid JSON, aria-style locators
  • Verifies equivalence between object and JSON string formats

Usage Examples

  // These are now equivalent:
  I.seeElement({ css: '#button' })
  I.seeElement('{"css": "#button"}')

  // Aria-style locators 
  I.seeElement('{"role": "button", "text": "Save"}')

@incoming-th
Copy link

@DavertMik Will that conflict with shadow? https://codecept.io/shadow/#locating-shadow-dom-elements

I tested shadow and not sure if codecept still compatible with it, but your change may also have an impact or I am mistaken?

@DavertMik
Copy link
Contributor Author

Good question! THanks, let me check this!

…, and strict mode

- Add JSON string parsing for locators (e.g., '{"css": "#button"}')
- Add selectOption support for ARIA combobox/listbox roles
- Add strict mode option for Playwright that throws MultipleElementsFound error
- Add MultipleElementsFound error class with detailed element info
- Add tests for all new features

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@DavertMik DavertMik force-pushed the feat/locator-json-as-string branch from 604a651 to 6adfb39 Compare January 21, 2026 04:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants