Skip to content

Commit

Permalink
fix: Fix getting parents/ancestors for shadow dom elements (#8106)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbreiding committed Aug 18, 2020
1 parent 6370f2e commit 46714c5
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 16 deletions.
6 changes: 5 additions & 1 deletion packages/driver/cypress/fixtures/shadow-dom.html
Expand Up @@ -35,6 +35,10 @@
<cy-test-element id="shadow-element-9" content="Shadow Content 9" rootAddition="
<div class='shadow-div'>Shadow Content 9</div>
"></cy-test-element>
<div class="focusable" tabindex="1">
<cy-test-element innerClass="inside-focusable" content="Inside Focusable"></cy-test-element>
</div>
<cy-test-element id="shadow-element-10" innerClass="inside-non-visible" content="Not Visible" style="display:block; transform-style: preserve-3d; backface-visibility: hidden; transform: rotateY(180deg);"></cy-test-element>

<script type="text/javascript">
if (window.customElements) {
Expand All @@ -43,7 +47,7 @@
super()

const root = this.attachShadow({ mode: 'open' })
const content = this.getAttribute('content')
const content = this.getAttribute('content') || 'Shadow Content'
const className = this.hasAttribute('innerClass') ? this.getAttribute('innerClass') : 'shadow-content'
const rootAddition = this.hasAttribute('rootAddition') ? this.getAttribute('rootAddition') : 'shadow-content'

Expand Down
Expand Up @@ -3806,6 +3806,11 @@ describe('shadow dom', () => {
.rightclick()
})

it('focuses the correct ancestor when it is outside shadow dom', () => {
cy.get('.inside-focusable', { includeShadowDom: true }).click()
cy.get('.focusable').should('be.focused')
})

// https://github.com/cypress-io/cypress/issues/7679
it('does not hang when experimentalShadowDomSupport is false and clicking on custom element', () => {
Cypress.config('experimentalShadowDomSupport', false)
Expand Down
Expand Up @@ -799,6 +799,17 @@ describe('src/cy/commands/actions/scroll', () => {
})
})

describe('shadow dom', () => {
beforeEach(() => {
cy.visit('/fixtures/shadow-dom.html')
})

// https://github.com/cypress-io/cypress/issues/7986
it('does not hang', () => {
cy.get('.shadow-1', { includeShadowDom: true }).scrollIntoView()
})
})

describe('assertion verification', () => {
beforeEach(function () {
cy.on('log:added', (attrs, log) => {
Expand Down
15 changes: 12 additions & 3 deletions packages/driver/cypress/integration/e2e/visibility_spec.js
Expand Up @@ -29,11 +29,20 @@ describe('visibility', () => {
})
})

// https://github.com/cypress-io/cypress/issues/7794
describe('with shadow dom and fixed position ancestor', () => {
it('does not hang when checking visibility', () => {
describe('with shadow dom', () => {
// https://github.com/cypress-io/cypress/issues/7794
it('fixed position ancestor does not hang when checking visibility', () => {
cy.visit('/fixtures/issue-7794.html')
cy.get('.container-2').should('be.visible')
})

// TODO: move with tests added in this PR when it merges: https://github.com/cypress-io/cypress/pull/8166
it('non-visible ancestor causes element to not be visible', () => {
cy.visit('/fixtures/shadow-dom.html')
cy
.get('#shadow-element-10')
.find('.shadow-div', { includeShadowDom: true })
.should('not.be.visible')
})
})
})
2 changes: 1 addition & 1 deletion packages/driver/src/cy/commands/actions/scroll.js
Expand Up @@ -7,7 +7,7 @@ const $utils = require('../../../cypress/utils')
const $errUtils = require('../../../cypress/error_utils')

const findScrollableParent = ($el, win) => {
const $parent = $el.parent()
const $parent = $dom.getParent($el)

// if we're at the body, we just want to pass in
// window into jQuery scrollTo
Expand Down
2 changes: 1 addition & 1 deletion packages/driver/src/cy/commands/traversals.js
Expand Up @@ -49,7 +49,7 @@ const autoShadowTraversals = {
},
parent: (cy, $el) => {
const parents = $el.map((i, el) => {
return $elements.getParent(el)
return $elements.getParentNode(el)
})

return sortedUnique(cy, parents)
Expand Down
8 changes: 5 additions & 3 deletions packages/driver/src/dom/document.ts
Expand Up @@ -4,13 +4,15 @@ const docNode = window.Node.DOCUMENT_NODE
const docFragmentNode = window.Node.DOCUMENT_FRAGMENT_NODE

//TODO: make this not allow jquery
const isDocument = (obj: Node): obj is Document => {
const isDocument = (obj: Node | JQuery): obj is Document => {
try {
let node = obj as Node

if ($jquery.isJquery(obj)) {
obj = obj[0]
node = obj[0]
}

return obj?.nodeType === docNode || obj?.nodeType === docFragmentNode
return node?.nodeType === docNode || node?.nodeType === docFragmentNode
} catch (error) {
return false
}
Expand Down
24 changes: 18 additions & 6 deletions packages/driver/src/dom/elements.ts
Expand Up @@ -611,7 +611,7 @@ const isWithinShadowRoot = (node: HTMLElement) => {
return isShadowRoot(node.getRootNode())
}

const getParent = (el) => {
const getParentNode = (el) => {
// if the element has a direct parent element,
// simply return it.
if (el.parentElement) {
Expand All @@ -629,9 +629,13 @@ const getParent = (el) => {
return null
}

const getParent = ($el: JQuery): JQuery => {
return $(getParentNode($el[0]))
}

const getAllParents = (el: HTMLElement, untilSelector?: string) => {
const collectParents = (parents, node) => {
const parent = getParent(node)
const parent = getParentNode(node)

if (!parent || untilSelector && $(parent).is(untilSelector)) {
return parents
Expand Down Expand Up @@ -901,7 +905,7 @@ const isDescendent = ($el1, $el2) => {

const findParent = (el, condition) => {
const collectParent = (node) => {
const parent = getParent(node)
const parent = getParentNode(node)

if (!parent) return null

Expand All @@ -925,17 +929,17 @@ const getFirstFocusableEl = ($el: JQuery<HTMLElement>) => {
return $el
}

const parent = $el.parent()
const $parent = getParent($el)

// if we have no parent then just return
// the window since that can receive focus
if (!parent.length) {
if (!$parent.length) {
const win = $window.getWindowByElement($el.get(0))

return $(win)
}

return getFirstFocusableEl($el.parent())
return getFirstFocusableEl(getParent($el))
}

const getActiveElByDocument = ($el: JQuery<HTMLElement>): HTMLElement | null => {
Expand Down Expand Up @@ -1247,6 +1251,12 @@ const elementFromPoint = (doc, x, y) => {
return elFromPoint
}

const getShadowRoot = ($el: JQuery): JQuery<Node> => {
const root = $el[0].getRootNode()

return $(root)
}

const findAllShadowRoots = (root: Node): Node[] => {
const collectRoots = (roots, nodes, node) => {
const currentRoot = roots.pop()
Expand Down Expand Up @@ -1360,5 +1370,7 @@ export {
getFirstStickyPositionParent,
getFirstScrollableParent,
getParent,
getParentNode,
getAllParents,
getShadowRoot,
}
3 changes: 2 additions & 1 deletion packages/driver/src/dom/transform.ts
@@ -1,4 +1,5 @@
import _ from 'lodash'
import { getParent } from './elements'
import { isDocument } from './document'

export const detectVisibility = ($el: any) => {
Expand Down Expand Up @@ -39,7 +40,7 @@ const extractTransformInfoFromElements = ($el: any, list: TransformInfo[] = []):
list.push(info)
}

const $parent = $el.parent()
const $parent = getParent($el)

if (!$parent.length || isDocument($parent)) {
return list
Expand Down

3 comments on commit 46714c5

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 46714c5 Aug 18, 2020

Choose a reason for hiding this comment

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

Circle has built the linux x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/5.0.0/linux-x64/circle-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-425246/cypress.zip
npm install https://cdn.cypress.io/beta/npm/5.0.0/circle-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-425227/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 46714c5 Aug 18, 2020

Choose a reason for hiding this comment

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

AppVeyor has built the win32 x64 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

Instructions are included below, depending on the shell you are using.

In Command Prompt (cmd.exe):

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/5.0.0/win32-x64/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.zip
npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.tgz

In PowerShell:

$env:CYPRESS_INSTALL_BINARY = https://cdn.cypress.io/beta/binary/5.0.0/win32-x64/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.zip
npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.tgz

In Git Bash:

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/5.0.0/win32-x64/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.zip
npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.tgz

Using cross-env:

If the above commands do not work for you, you can also try using cross-env:

npm i -g cross-env
cross-env CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/5.0.0/win32-x64/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.zip npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 46714c5 Aug 18, 2020

Choose a reason for hiding this comment

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

AppVeyor has built the win32 ia32 version of the Test Runner.

You can install this pre-release platform-specific build using instructions at https://on.cypress.io/installing-cypress#Install-pre-release-version.

You will need to use custom CYPRESS_INSTALL_BINARY url and install Cypress using an url instead of the version.

Instructions are included below, depending on the shell you are using.

In Command Prompt (cmd.exe):

set CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/5.0.0/win32-ia32/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.zip
npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.tgz

In PowerShell:

$env:CYPRESS_INSTALL_BINARY = https://cdn.cypress.io/beta/binary/5.0.0/win32-ia32/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.zip
npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.tgz

In Git Bash:

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/5.0.0/win32-ia32/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.zip
npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.tgz

Using cross-env:

If the above commands do not work for you, you can also try using cross-env:

npm i -g cross-env
cross-env CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/5.0.0/win32-ia32/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.zip npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-46714c5758fa5d2ca108a650bf182ffe5a6a143b-34720776/cypress.tgz

Please sign in to comment.