Skip to content

fix: Address snapshot and sanitization issues in packages/core#255

Merged
Winify merged 1 commit into
webdriverio:mainfrom
Winify:fix/elements-snapshot
Jul 20, 2026
Merged

fix: Address snapshot and sanitization issues in packages/core#255
Winify merged 1 commit into
webdriverio:mainfrom
Winify:fix/elements-snapshot

Conversation

@Winify

@Winify Winify commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

What & why

Fixes two classes of bugs in the mobile element snapshot and XML parsing pipelines discovered during focused review for platform gaps (iOS, Android, WebView).

1. Element snapshot: child-text name enrichment (element-snapshot.ts)

The previous code promoted class-based Android locators to text-based selectors by reading child text. This had three defects:

  • Wrong element target: UiSelector().text("...") resolves to the child TextView, not the clickable parent container the locator was generated for.
  • No uniqueness check: The full locator pipeline validates text uniqueness via XPath before emitting selectors. The promotion skipped this.
  • DFS preference for deeply nested text: A nested grandchild label was preferred over a direct sibling label.

Replaced with name enrichment: when an interactive Android element has only a class-based locator and no name, child text is pulled (BFS, length-guarded <100) and used as the element display name. The selector stays class-based, correctly targeting the parent.

2. XML parsing: sanitize page source before parsing (xml-parsing.ts)

Closes #240. iOS page source from XCUITest (especially WebView content) can contain HTML artifacts that @xmldom/xmldom strict XML parser rejects:

  • Bare ampersands (& in URLs, &nbsp;, &copy;) not defined in XML
  • HTML void elements (<link>, <meta>, <br>, <img>) without explicit /> self-closure

Added sanitizePageSource() called from both xmlToJSON and xmlToDOM. Two-pass: escape bare &&amp; (preserving valid XML entities), then self-close 13 known HTML void elements. Platform-agnostic — runs for both Android and iOS.

3. escapeText: added \r and \t escaping (locator-generation.ts)

Control characters in element text would produce invalid Java string literals in UiSelector().text("...") selectors.

Type of change

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Polish (an improvement to an existing feature)
  • Breaking change (existing behavior changes for users)
  • Documentation
  • Internal (build, CI, dependencies, tooling)

Packages touched

  • shared (types and contracts)
  • core (framework-agnostic capture/reporting)
  • service (WebdriverIO adapter)
  • nightwatch-devtools (Nightwatch adapter)
  • selenium-devtools (Selenium adapter)
  • backend (server)
  • app (UI)
  • script (page-injected runtime)

Notes for reviewers

  • Child-text name enrichment is Android-only for now. getFirstChildText reads attributes.text (Android attribute). An iOS equivalent would need to check name/label/value. Filed as follow-up.
  • _sourceXML auto-detection added to jsonElementToSnapshotNodes to match serializeMobileSnapshot behavior.
  • Pre-existing bugs found but not fixed: getBestAndroidLocator uses ~ prefix for attrs.text (~ maps to content-desc, not text on Android). Same pattern in getBestIOSLocator for label/value. Fixing would change behavior for existing consumers — needs a separate change.

@Winify Winify changed the title Fix/elements snapshot fix: Address snapshot and sanitization issues in packages/core Jul 19, 2026
@Winify
Winify marked this pull request as ready for review July 19, 2026 20:20
…resent with child-text and sanitize page source XML that may contain HTML artifacts

- Adding xml-parsing unit tests for sanitizePageSource
- Including changeset
@Winify
Winify force-pushed the fix/elements-snapshot branch from 30333b2 to f0cbe45 Compare July 19, 2026 20:22
@Winify
Winify requested a review from vishnuv688 July 20, 2026 11:53
@Winify
Winify merged commit 9f25708 into webdriverio:main Jul 20, 2026
3 checks passed
@Winify
Winify deleted the fix/elements-snapshot branch July 20, 2026 15:38
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.

[🐛 Bug]: Error while running tests on IOS simulator

1 participant