Skip to content

fix: handle empty quads from getContentQuads for proximity selectors #2771

Merged
NivedhaSenthil merged 5 commits into
getgauge:masterfrom
winst0niuss:fix/proximity-selectors-empty-quads
Apr 23, 2026
Merged

fix: handle empty quads from getContentQuads for proximity selectors #2771
NivedhaSenthil merged 5 commits into
getgauge:masterfrom
winst0niuss:fix/proximity-selectors-empty-quads

Conversation

@winst0niuss

Copy link
Copy Markdown
Contributor

Fixes #2757

Problem

Proximity selectors (toRightOf, toLeftOf, above, below, near) throw TypeError: Cannot read properties of undefined (reading '1') in domHandler.js:152.

Root cause

After migrating from dom.getBoxModel to dom.getContentQuads, the null-check logic broke. When getContentQuads returns quads: [] (element is hidden, off-screen, etc.), result.quads[0] is undefined, but getBoxModel still returned a non-null object — so the if (!result) guard in getBoundingClientRect
never fired, and accessing quad[1] crashed.

Two additional call sites had the same unguarded pattern:

  • getPositionalDifference — did not handle null from getBoundingClientRect
  • highlightElement — did not handle null from getBoxModel

Changes

domHandler.jsgetBoxModel: return null when quads is empty, making all downstream guards work correctly.

domHandler.jsgetPositionalDifference: return Number.POSITIVE_INFINITY when either element has no bounding box, so it never wins the proximity comparison in proximityElementSearch.js.

elementHelper.jshighlightElement: skip highlight gracefully when getBoxModel returns null (e.g. visible element with no rendered quads on certain platforms).

tests/unit-tests/proximitySelector.test.js: new regression test covering all five proximity selectors and a hidden element case (display:none) that triggers the empty-quads path.

@winst0niuss winst0niuss force-pushed the fix/proximity-selectors-empty-quads branch 2 times, most recently from d5cfdcf to 4b3fc7c Compare April 16, 2026 08:47
Comment thread packages/taiko/lib/elements/elementHelper.js
Comment thread packages/taiko/lib/elements/elementHelper.js
@@ -0,0 +1,99 @@
const chai = require("chai");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please move the tests inside tests/unit-tests/proximitySelectors and if possible preferably to respective proximity selector test files.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved the test file to tests/unit-tests/proximitySelectors/proximitySelector.test.js.

@winst0niuss winst0niuss force-pushed the fix/proximity-selectors-empty-quads branch from 02972ca to 10538bc Compare April 20, 2026 06:52
@winst0niuss winst0niuss force-pushed the fix/proximity-selectors-empty-quads branch from 10538bc to 0f1b1c5 Compare April 20, 2026 07:32
@winst0niuss

Copy link
Copy Markdown
Contributor Author

@NivedhaSenthil , check pls

When getContentQuads returns an empty quads array (e.g. for hidden or
off-screen elements), getBoxModel was returning { model: { border: undefined } }
instead of null. This caused getBoundingClientRect to crash with:
  TypeError: Cannot read properties of undefined (reading '1')

Fix getBoxModel to return null when quads is empty so that all existing
null-checks in getBoundingClientRect and boundBox work correctly.

Also guard getPositionalDifference against null rects by returning
Number.POSITIVE_INFINITY, ensuring such elements never win proximity
comparisons in proximityElementSearch.js.

Fixes getgauge#2757

Signed-off-by: winst0niuss <chumachenko.vadym@gmail.com>
After getBoxModel started returning null for empty quads, highlightElement
would crash with TypeError when trying to access result.model.border.

Skip the highlight gracefully when getBoxModel returns null (e.g. a visible
element that has no rendered quads on certain platforms/Node versions).

Signed-off-by: winst0niuss <chumachenko.vadym@gmail.com>
Signed-off-by: winst0niuss <chumachenko.vadym@gmail.com>
@winst0niuss winst0niuss force-pushed the fix/proximity-selectors-empty-quads branch from 0f1b1c5 to 924ba2f Compare April 23, 2026 08:36
Without an explicit import, the test relies on $.test.js loading
chai-as-promised first — an order-dependent side effect that breaks
on Windows where glob returns files in a different order.

Signed-off-by: winst0niuss <chumachenko.vadym@gmail.com>
@winst0niuss winst0niuss force-pushed the fix/proximity-selectors-empty-quads branch from 0781fab to 356f1e0 Compare April 23, 2026 09:37
@winst0niuss

winst0niuss commented Apr 23, 2026

Copy link
Copy Markdown
Contributor Author

After merging getgauge:master, the Windows CI jobs started failing due to a bug introduced in #2772.

Root cause: evaluate.test.js (added in #2772) uses .rejectedWith() from chai-as-promised without importing it. On macOS/Linux the tests pass by accident — $.test.js runs first (glob sorts $ before letters in ASCII) and loads chai-as-promised globally. On Windows, glob returns files in a different order, so evaluate.test.js runs before chai-as-promised is loaded, causing TypeError: rejectedWith is not a function.

@winst0niuss

Copy link
Copy Markdown
Contributor Author

Fix: Added an explicit chai-as-promised import to evaluate.test.js so the test no longer depends on execution order.

Signed-off-by: winst0niuss <chumachenko.vadym@gmail.com>
@winst0niuss winst0niuss force-pushed the fix/proximity-selectors-empty-quads branch from ec7528a to e1e93e8 Compare April 23, 2026 10:59
@winst0niuss

Copy link
Copy Markdown
Contributor Author

Added a retry (2 attempts) for Windows CI jobs to avoid flaky failures we keep seeing on GitHub runners
Other jobs unchanged

@winst0niuss

Copy link
Copy Markdown
Contributor Author

@zabil , re-run tests pls

@winst0niuss

Copy link
Copy Markdown
Contributor Author

All checks are green. Please merge 🙂

@zabil

zabil commented Apr 23, 2026

Copy link
Copy Markdown
Member

@NivedhaSenthil please merge if you are ok with the review updates.

@NivedhaSenthil NivedhaSenthil merged commit 4ac989a into getgauge:master Apr 23, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Using any proximity selector in Taiko 1.4.3/1.4.4 throws TypeError: Cannot read properties of undefined (reading '1')

3 participants