From 715d07c1a9308ad215fe35da2775100a31dbf982 Mon Sep 17 00:00:00 2001 From: Brian Mann Date: Mon, 29 Jun 2020 14:16:20 -0400 Subject: [PATCH] fix flaky test because of inline element --- .../cypress/integration/commands/actions/click_spec.js | 3 ++- packages/driver/src/dom/coordinates.js | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/driver/cypress/integration/commands/actions/click_spec.js b/packages/driver/cypress/integration/commands/actions/click_spec.js index 93ff851c272a..7fd96b610db7 100644 --- a/packages/driver/cypress/integration/commands/actions/click_spec.js +++ b/packages/driver/cypress/integration/commands/actions/click_spec.js @@ -3711,7 +3711,8 @@ describe('shadow dom', () => { it('does not hang when experimentalShadowDomSupport is false and clicking on custom element', () => { Cypress.config('experimentalShadowDomSupport', false) // needs some size or it's considered invisible and click will fail its prerequisites - cy.$$('#shadow-element-1').css({ padding: 2 }) + // so we make it display: block so its getClientRects() contains only a single + cy.$$('#shadow-element-1').css({ display: 'block' }) cy.get('#shadow-element-1').click() }) diff --git a/packages/driver/src/dom/coordinates.js b/packages/driver/src/dom/coordinates.js index 2de5b81b6508..f115fcb8faba 100644 --- a/packages/driver/src/dom/coordinates.js +++ b/packages/driver/src/dom/coordinates.js @@ -1,3 +1,4 @@ +const _ = require('lodash') const $window = require('./window') const $elements = require('./elements') @@ -7,6 +8,13 @@ const getElementAtPointFromViewport = (doc, x, y) => { const isAutIframe = (win) => !$elements.getNativeProp(win.parent, 'frameElement') +const getFirstValidSizedRect = (el) => { + return _.find(el.getClientRects(), (rect) => { + // use the first rect that has a nonzero width and height + return rect.width && rect.height + }) || el.getBoundingClientRect() // otherwise fall back to the parent client rect +} + /** * @param {JQuery} $el */ @@ -32,7 +40,7 @@ const getElementPositioning = ($el) => { // returns a zero length DOMRectList in that case, which becomes undefined. // so we fallback to getBoundingClientRect() so that we get an actual DOMRect // with all properties 0'd out - const rect = [...el.getClientRects()].find((e) => e.width && e.height) || el.getBoundingClientRect() + const rect = getFirstValidSizedRect(el) // we want to return the coordinates from the autWindow to the element // which handles a situation in which the element is inside of a nested