Skip to content

Commit

Permalink
Fix typing into shadow dom inputs (#7847)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbreiding authored Jun 30, 2020
1 parent 715d07c commit 6c3965a
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/driver/cypress/fixtures/shadow-dom.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
const content = this.getAttribute('content')
const className = this.hasAttribute('innerClass') ? this.getAttribute('innerClass') : 'shadow-content'

root.innerHTML = `<p class="${className}">${content}</p><slot></slot>`
root.innerHTML = `<p class="${className}">${content}</p><input /><slot></slot>`
}
})
}
Expand Down
14 changes: 14 additions & 0 deletions packages/driver/cypress/integration/commands/actions/type_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3066,4 +3066,18 @@ describe('src/cy/commands/actions/type - #type', () => {
})
})
})

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

// https://github.com/cypress-io/cypress/issues/7741
it('types into input', () => {
cy
.get('#shadow-element-1')
.find('input', { includeShadowDom: true })
.type('foo')
})
})
})
95 changes: 95 additions & 0 deletions packages/driver/cypress/integration/dom/elements_spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getActiveElByDocument, isFocused } from '../../../src/dom/elements'

const { $ } = Cypress

export {}
Expand Down Expand Up @@ -169,4 +171,97 @@ describe('src/dom/elements', () => {
expect(Cypress.dom.isUndefinedOrHTMLBodyDoc($el)).to.be.false
})
})

context('.getActiveElByDocument', () => {
beforeEach(() => {
cy.visit('/fixtures/active-elements.html')
})

it('returns active element by looking it up on document', () => {
cy.get('input:first').focus().then(($el) => {
expect(getActiveElByDocument($el)).to.equal($el[0])
})
})

it('returns null if element is not focused', () => {
cy.get('input:first').then(($el) => {
expect(getActiveElByDocument($el)).to.be.null
})
})

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

it('returns active element for shadow dom by looking it up on shadow root', () => {
cy
.get('#shadow-element-1')
.find('input', { includeShadowDom: true }).focus().then(($el) => {
expect(getActiveElByDocument($el)).to.equal($el[0])
})
})
})
})

context('.isFocused', () => {
beforeEach(() => {
cy.visit('/fixtures/active-elements.html')
})

it('returns true if the element is the active element', () => {
cy.get('input:first').focus().then(($el) => {
expect(isFocused($el[0])).to.be.true
})
})

it('returns true if the active element is the body and it is contenteditable', () => {
cy.get('body').focus().then(($body) => {
$body[0].setAttribute('contenteditable', 'true')
expect(isFocused($body[0])).to.be.true
})
})

it('returns false if the element is not the active element', () => {
cy.get('input:first').then(($el) => {
expect(isFocused($el[0])).to.be.false
})
})

it('returns false if there is no active element', () => {
cy.get('input:first').focus().then(($el) => {
$el.blur()
expect(isFocused($el[0])).to.be.false
})
})

it('returns false if the active element is the body', () => {
cy.get('body').focus().then(($body) => {
expect(isFocused($body[0])).to.be.false
})
})

it('returns false if determining the active element errors', () => {
cy.get('input:first').focus().then(($el) => {
Object.defineProperty($el[0].ownerDocument, 'activeElement', {
get () {
throw new Error('unexpected error')
},
})

expect(isFocused($el[0])).to.be.false
})
})

describe('in the shadow dom', () => {
it('returns true if the element is the active element of the shadow root', () => {
cy.visit('/fixtures/shadow-dom.html')
cy
.get('#shadow-element-1')
.find('input', { includeShadowDom: true }).focus().then(($el) => {
expect(isFocused($el[0])).to.be.true
})
})
})
})
})
4 changes: 3 additions & 1 deletion packages/driver/src/cy/commands/actions/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,9 @@ module.exports = function (Commands, Cypress, cy, state, config) {
errorOnSelect: false,
})
.then(() => {
if (!options.force && $elements.getActiveElByDocument($elToClick[0].ownerDocument) === null) {
let activeElement = $elements.getActiveElByDocument($elToClick)

if (!options.force && activeElement === null) {
const node = $dom.stringify($elToClick)
const onFail = options._log

Expand Down
4 changes: 2 additions & 2 deletions packages/driver/src/cy/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ export class Keyboard {
return options.$el.get(0)
}

const activeEl = $elements.getActiveElByDocument(doc) || doc.body
const activeEl = $elements.getActiveElByDocument(options.$el) || doc.body

return activeEl
}
Expand Down Expand Up @@ -1098,7 +1098,7 @@ export class Keyboard {

const doc = $document.getDocumentFromElement(el)

return $elements.getActiveElByDocument(doc) || doc.body
return $elements.getActiveElByDocument(options.$el) || doc.body
}

performSimulatedDefault (el: HTMLElement, key: KeyDetails, options: any) {
Expand Down
25 changes: 17 additions & 8 deletions packages/driver/src/dom/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,13 @@ const activeElementIsDefault = (activeElement, body: HTMLElement) => {

const isFocused = (el) => {
try {
const doc = $document.getDocumentFromElement(el)
let doc

if (Cypress.config('experimentalShadowDomSupport') && isWithinShadowRoot(el)) {
doc = el.getRootNode()
} else {
doc = $document.getDocumentFromElement(el)
}

const { activeElement, body } = doc

Expand Down Expand Up @@ -598,11 +604,7 @@ const getFirstCommonAncestor = (el1, el2) => {
}

const isWithinShadowRoot = (node: HTMLElement) => {
const win = $window.getWindowByElement(node)

if (!win) return false

return node.getRootNode() instanceof win.ShadowRoot
return node.getRootNode().toString() === '[object ShadowRoot]'
}

const getParent = (el) => {
Expand Down Expand Up @@ -931,8 +933,15 @@ const getFirstFocusableEl = ($el: JQuery<HTMLElement>) => {

return getFirstFocusableEl($el.parent())
}
const getActiveElByDocument = (doc: Document): HTMLElement | null => {
const activeElement = getNativeProp(doc, 'activeElement')

const getActiveElByDocument = ($el: JQuery<HTMLElement>): HTMLElement | null => {
let activeElement

if (Cypress.config('experimentalShadowDomSupport') && isWithinShadowRoot($el[0])) {
activeElement = ($el[0].getRootNode() as ShadowRoot).activeElement
} else {
activeElement = getNativeProp($el[0].ownerDocument as Document, 'activeElement')
}

if (isFocused(activeElement)) {
return activeElement as HTMLElement
Expand Down

4 comments on commit 6c3965a

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 6c3965a Jun 30, 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-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-384081/cypress.zip
npm install https://cdn.cypress.io/beta/npm/5.0.0/circle-develop-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-384059/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 6c3965a Jun 30, 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-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/cypress.zip
npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/cypress.tgz

In PowerShell:

$env:CYPRESS_INSTALL_BINARY = https://cdn.cypress.io/beta/binary/5.0.0/win32-ia32/appveyor-develop-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/cypress.zip
npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/cypress.tgz

In Git Bash:

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/5.0.0/win32-ia32/appveyor-develop-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/cypress.zip
npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/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-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/cypress.zip npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 6c3965a Jun 30, 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-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/cypress.zip
npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/cypress.tgz

In PowerShell:

$env:CYPRESS_INSTALL_BINARY = https://cdn.cypress.io/beta/binary/5.0.0/win32-x64/appveyor-develop-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/cypress.zip
npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/cypress.tgz

In Git Bash:

export CYPRESS_INSTALL_BINARY=https://cdn.cypress.io/beta/binary/5.0.0/win32-x64/appveyor-develop-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/cypress.zip
npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/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-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/cypress.zip npm install https://cdn.cypress.io/beta/npm/5.0.0/appveyor-develop-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-33828735/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 6c3965a Jun 30, 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 darwin 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/darwin-x64/circle-develop-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-384143/cypress.zip
npm install https://cdn.cypress.io/beta/npm/5.0.0/circle-develop-6c3965a0e04eddf94ebbb9ab93c5f68865826b02-384086/cypress.tgz

Please sign in to comment.