feat: warn when a text is passed where a selector is expected - #5696
Open
DavertMik wants to merge 1 commit into
Open
feat: warn when a text is passed where a selector is expected#5696DavertMik wants to merge 1 commit into
DavertMik wants to merge 1 commit into
Conversation
Methods like waitForElement, seeElement and grabTextFrom expect a CSS or XPath locator and, unlike click or fillField, do not fall back to searching by text. A sentence passed to them is matched as CSS, finds nothing, and the step fails on timeout with a message that says nothing about the real cause. Adds a heuristic that recognises such strings: several words, no CSS or XPath punctuation, not a chain of tag names. Wired into 32 locator-only methods across Playwright, Puppeteer and WebDriver. In debug mode it prints a [Warning] with a suggestion for the method used; with strict: true it throws InvalidSelector so the test fails immediately instead of after the full timeout. Valid locators with spaces are left alone: `div span`, `my-app my-button`, `text=Save Changes` and `~accessibility id` all pass the check. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NSR3yk8NgMFkPSspsynKUN
Contributor
Author
|
Should be rewritten as part of Locator class |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
waitForElement,seeElement,waitForVisible,grabTextFromand the rest of the locator-only family expect a CSS or XPath selector. UnlikeclickorfillField, they do not fall back to searching by text — the string goes straight to the engine, is parsed as a CSS descendant chain, matches nothing, and the step fails after the full timeout with a message that says nothing about the real cause:dontSeeElementis worse: the text matches nothing, so the assertion silently passes.Solution
A heuristic in
lib/helper/extras/selectorCheck.jsrecognises a string that cannot be a selector — several words, no CSS or XPath punctuation, not a chain of tag names:Output in debug mode, with a suggestion picked from the step name:
With
strict: truethe same check throwsInvalidSelectorinstead, so the test fails immediately rather than after the timeout. This follows the existingfocusCheckconvention exactly — silent by default, visible under--debug, fatal understrict.Scope
Wired into 32 locator-only methods in Playwright, Puppeteer and WebDriver: the
see*/dontSee*Elementfamily, allwaitFor*element waits, allgrab*element getters, plusscrollTo,scrollIntoView,saveElementScreenshotandmoveCursorTo.Deliberately not wired into methods where a text is legal:
click,clickLink,doubleClick,rightClick,forceClick,fillField,checkOption,selectOption,see,dontSee,waitForText,seeTextEquals. Appium overrides several of these methods with its own implementations, so mobile locators are untouched.False positives
The predicate also gates the strict-mode throw, so a false positive would break a passing test. Locators with spaces that are left alone:
div span,my-app my-button,ul > li,text=Save Changes,android=new UiSelector().text("Save now"),~accessibility id,//*[contains(@class,"x")].Verified by a static sweep (acorn AST) of every string literal passed as the first argument to a wired method across
test/,examples/andlib/— 330 literals, 0 flagged.Testing
test/unit/selectorCheck_test.jscodecept.Playwright.js --debug): 59 passed, 1 pre-existing failure (Dynamic Config › make API call— external API returns HTML), 0 warnings emitted🤖 Generated with Claude Code
https://claude.ai/code/session_01NSR3yk8NgMFkPSspsynKUN