Skip to content

fix: preserve string literals in XPath shorthand selectors - #1201

Merged
codeskyblue merged 1 commit into
openatx:masterfrom
David-the-happy-piglet:fix/xpath-string-literals
Nov 2, 2026
Merged

codeskyblue merged 1 commit into
openatx:masterfrom
David-the-happy-piglet:fix/xpath-string-literals

Conversation

@David-the-happy-piglet

Copy link
Copy Markdown
Contributor

XPath shorthand currently formats values with Python repr(). XPath 1.0 does not interpret those backslash escapes: text containing both quote types can raise XPathError, while backslashes and newlines can cause matching nodes to be missed. Regex shorthand such as ^A\d+$ also fails to match A123.

Encode values as XPath string literals, using concat(...) when both quote types are present. Apply the same encoding to @resource-id shorthand. Regex patterns retain their original characters and semantics.

Add 84 cases using real lxml/PageSource evaluation, covering exact, substring, prefix, suffix, resource-ID and regex selectors, matching and nonmatching nodes, and literal edge cases.

Validation on macOS 26.5.2 arm64, CPython 3.12.15 and lxml 6.1.3, based on master bad641e7:

  • Before the fix, the new tests produce 39 failures and 45 passes.
  • After the fix, all 99 XPath tests pass (84 new and 15 existing).
  • Full unit suite: 190 passed, one existing failure in test_missing_command_shows_help. Unchanged upstream has the same failure with 106 passes; that CLI behavior is covered separately by fix: show successful help for an empty u2cli invocation #1198.
  • Coverage run produces a 44% report and exits nonzero for that same baseline failure.
  • Changed-file isort, diff checks, CLI help/version and imports pass.
  • Source distribution and wheel build pass using the project's declared Poetry build backend through uv build.
  • Independent local review found no actionable issues and reproduced the final tests' failures on upstream and success with the fix.

Android device tests, Windows/Linux and other Python versions were not run.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Device-side compatibility remains unverified because validation covers only lxml/PageSource rather than the Android XPath service, particularly for concat() and literal handling.

No open issues 🎉

What changed in this PR

This pull request fixes XPath shorthand selectors by encoding values as valid XPath 1.0 string literals, preserving quotes, backslashes, newlines, Unicode, and regex escapes.

Changes:

  • Added XPath-safe literal encoding with concat(...) support for values containing both quote types.
  • Applied encoding to text, content-description, resource-ID, and regex shorthand selectors.
  • Added 84 lxml-backed regression tests covering matching behavior and literal edge cases.
File Description
uiautomator2/​xpath.py Implements XPath 1.0-compatible string literal encoding for shorthand selectors.
tests/​test_xpath_literals.py Adds comprehensive coverage for exact, substring, prefix, suffix, resource-ID, regex, and literal edge cases.

Review effort: Lite

@codeskyblue
codeskyblue merged commit 5d373ad into openatx:master Nov 2, 2026
6 of 7 checks passed
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