Skip to content

Commit

Permalink
fix: make isFocusedOrInFocused check shadow roots (#26207)
Browse files Browse the repository at this point in the history
* fix: make isFocusedOrInFocused check shadow roots

* Add item to changelog

* Get rid of unnecessary null checks

* Add e2e test for new scenario

* Make changelog item clearer

* Add a test for the type command as well

* Update type click test

* Fix typo

* Reuse shadow-dom-focus.html

* Update changelog item

Co-authored-by: Emily Rohrbough <emilyrohrbough@users.noreply.github.com>

* Remove shadow-dom-focus for shadow-dom-type

* Check if isWithinShadowRoot and use root node

* Fix potential error w/ getSelection
Because shadowRoots don't have a getSelection function

* Make doc a const again

* Update cli/CHANGELOG.md

---------

Co-authored-by: Emily Rohrbough <emilyrohrbough@users.noreply.github.com>
  • Loading branch information
kgroat and emilyrohrbough authored Mar 28, 2023
1 parent 24194da commit 5611951
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ _Released 03/28/2023 (PENDING)_
**Bugfixes:**

- Fixed a compatibility issue so that component test projects can use [Vite](https://vitejs.dev/) version 4.2.0 and greater. Fixes [#26138](https://github.com/cypress-io/cypress/issues/26138).
- Changed the way that Git hashes are loaded so that non-relevant runs are excluded from the Debug page. Fixes [#26058](https://github.com/cypress-io/cypress/issues/26058).
- Fixed an issue where [`cy.intercept()`](https://docs.cypress.io/api/commands/intercept) added an additional `content-length` header to spied requests that did not set a `content-length` header on the original request. Fixes [#24407](https://github.com/cypress-io/cypress/issues/24407).
- Changed the way that Git hashes are loaded so that non-relevant runs are excluded from the Debug page. Fixes [#26058](https://github.com/cypress-io/cypress/issues/26058).
- Corrected the [`.type()`](https://docs.cypress.io/api/commands/type) command to account for shadow root elements when determining whether or not focus needs to be simulated before typing. Fixes [#26198](https://github.com/cypress-io/cypress/issues/26198).
- Fixed an issue where an incorrect working directory could be used for Git operations on Windows. Fixes [#23317](https://github.com/cypress-io/cypress/issues/23317).

**Misc:**
Expand Down
12 changes: 12 additions & 0 deletions packages/driver/cypress/e2e/commands/actions/type.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3092,5 +3092,17 @@ describe('src/cy/commands/actions/type - #type', () => {

cy.get('#result').should('have.text', 'typed')
})

// https://github.com/cypress-io/cypress/issues/26198
it('type calls on elements in shadow dom do not fire click if the element already has focus', () => {
cy.visit('fixtures/shadow-dom-type.html')

cy.get('test-element').shadow().find('input').as('input')

cy.get('@input').invoke('on', 'click', cy.spy().as('clickSpy'))
cy.get('@input').focus().type('asdf')

cy.get('@clickSpy').should('not.have.been.called')
})
})
})
12 changes: 11 additions & 1 deletion packages/driver/cypress/e2e/dom/elements.cy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import $elements from '../../../src/dom/elements'
const { getActiveElByDocument, isFocused, elementFromPoint } = $elements
const { getActiveElByDocument, isFocused, elementFromPoint, isFocusedOrInFocused } = $elements
const $ = window.Cypress.$

describe('src/dom/elements', () => {
Expand Down Expand Up @@ -321,4 +321,14 @@ describe('src/dom/elements', () => {
expect(elementFromPoint(doc, 1, 2)).to.equal(shadowHostNode)
})
})

context('.isFocusedOrInFocused', () => {
it('should traverse shadow roots when determining if an element has focus', () => {
cy.visit('/fixtures/shadow-dom-type.html')

cy.get('test-element').shadow().find('input').focus().then(($input) => {
expect(isFocusedOrInFocused($input[0])).to.be.true
})
})
})
})
11 changes: 9 additions & 2 deletions packages/driver/src/dom/elements/complexElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,21 @@ export const isFocusable = ($el: JQuery<HTMLElement>) => {

export const isFocusedOrInFocused = (el: HTMLElement) => {
debug('isFocusedOrInFocus', el)

const doc = $document.getDocumentFromElement(el)

if (!doc.hasFocus()) {
return false
}

const { activeElement } = doc
let root: Document | ShadowRoot

if (isWithinShadowRoot(el)) {
root = el.getRootNode() as ShadowRoot
} else {
root = doc
}

let { activeElement } = root

let elToCheckCurrentlyFocused

Expand Down

5 comments on commit 5611951

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 5611951 Mar 28, 2023

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 arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.9.0/linux-arm64/develop-5611951c351fad0fa2ed6f491e7a01156e868f17/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 5611951 Mar 28, 2023

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.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.9.0/linux-x64/develop-5611951c351fad0fa2ed6f491e7a01156e868f17/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 5611951 Mar 28, 2023

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 arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.9.0/darwin-arm64/develop-5611951c351fad0fa2ed6f491e7a01156e868f17/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 5611951 Mar 28, 2023

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.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.9.0/darwin-x64/develop-5611951c351fad0fa2ed6f491e7a01156e868f17/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 5611951 Mar 28, 2023

Choose a reason for hiding this comment

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

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

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.9.0/win32-x64/develop-5611951c351fad0fa2ed6f491e7a01156e868f17/cypress.tgz

Please sign in to comment.