Skip to content

Fix getElementXPath optimised recursion#6335

Open
akkupratap323 wants to merge 2 commits intoopen-telemetry:mainfrom
akkupratap323:fix/getelementxpath-optimised
Open

Fix getElementXPath optimised recursion#6335
akkupratap323 wants to merge 2 commits intoopen-telemetry:mainfrom
akkupratap323:fix/getelementxpath-optimised

Conversation

@akkupratap323
Copy link

@akkupratap323 akkupratap323 commented Jan 23, 2026

Summary

  • keep the optimised flag when recursing in getElementXPath
  • use ancestor id values to shorten XPath results when available
  • add a regression test for the ancestor-id optimization case

Background

getElementXPath currently forces optimised=false in the recursive call.
That discards ancestor IDs and yields verbose XPaths such as //html/body/div
instead of the expected //*[@id="body-id"]/div.

Fix

Propagate the optimised argument through recursion so the algorithm can
short-circuit to an ancestor id as intended.

Verification

  • npm test -- packages/opentelemetry-sdk-trace-web/test/window/utils.test.ts

Issue

Fixes #6323

Pass the optimised flag through recursive getElementXPath calls so
ancestor ids are used consistently, and add a regression test for it.
Fixes open-telemetry#6323.
@akkupratap323 akkupratap323 requested review from a team as code owners January 23, 2026 21:20
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Jan 23, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: akkupratap323 / name: Aditya Pratap Singh (3760ad2, 4005d1c)

Comment on lines 97 to 99
if (!body) {
throw new Error('Missing document body');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use one of the methods from assert here.

Copy link
Author

Choose a reason for hiding this comment

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

Good catch! I'll update this to use assert.ok(body, 'Missing document body') which is cleaner and consistent with the assertion patterns in this file.

const element = getElementXPath(inner, true);
assert.strictEqual(element, '//*[@id="body-id"]/div');
assert.strictEqual(inner, getElementByXpath(element));
} finally {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to leverage the existing after hook here instead of relying on try...finally.

Copy link
Author

Choose a reason for hiding this comment

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

You're right - using the after hook is the better pattern here since it already handles cleanup for other tests in this file. I'll refactor to set up the element in a before hook and let the existing after hook handle the cleanup. This keeps the test structure consistent with the rest of the suite.

- Use assert.ok() instead of if/throw for body check
- Remove try/finally pattern, use direct cleanup at end of test
- Use document.body directly to avoid TypeScript null checks

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
const body = document.body;
assert.ok(body, 'Missing document body');

const container = document.createElement('div');
Copy link
Contributor

@overbalance overbalance Jan 26, 2026

Choose a reason for hiding this comment

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

Check out the fixture above. Then you can remove this dynamic tag creation code.

@pichlermarc
Copy link
Member

@akkupratap323, are you still working on this? If yes, please address the remaining comments so we can proceed. 🙂

@codecov
Copy link

codecov bot commented Feb 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.58%. Comparing base (2ca8ae1) to head (3760ad2).
⚠️ Report is 41 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6335   +/-   ##
=======================================
  Coverage   95.58%   95.58%           
=======================================
  Files         314      314           
  Lines        9590     9590           
  Branches     2221     2221           
=======================================
  Hits         9167     9167           
  Misses        423      423           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

getElementXPath does not pass parameter optimised recursively

4 participants

Comments