Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixing issue with blurring shadow dom elements #29125

Merged
merged 6 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<!-- See the ../guides/writing-the-cypress-changelog.md for details on writing the changelog. -->
## 13.7.1

_Released 3/26/2024 (PENDING)_

**Bugfixes:**

- Fixed an issue blurring shadow dom elements. Fixed in [#29125](/~https://github.com/cypress-io/cypress/pull/29125).

## 13.7.0

_Released 3/13/2024_
Expand Down
28 changes: 27 additions & 1 deletion packages/driver/cypress/e2e/commands/actions/focus.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,19 @@ describe('src/cy/commands/actions/focus', () => {
})
})

it('can focus element in nested shadow dom', () => {
const onFocus = cy.stub()

cy.visit('/fixtures/shadow-dom.html')
cy.get('.shadow-5 + input', { includeShadowDom: true }).as('shadow-input').then(($el) => {
$el.on('focus', onFocus)
})

cy.get('@shadow-input').focus().then(() => {
expect(onFocus).to.be.calledOnce
})
})

describe('assertion verification', () => {
beforeEach(function () {
cy.on('log:added', (attrs, log) => {
Expand Down Expand Up @@ -673,7 +686,7 @@ describe('src/cy/commands/actions/focus', () => {
})
})

it('can focus svg elements', () => {
it('can blur svg elements', () => {
const onBlur = cy.stub()

cy.$$('[data-cy=rect]').blur(onBlur)
Expand All @@ -683,6 +696,19 @@ describe('src/cy/commands/actions/focus', () => {
})
})

it('can blur element in nested shadow dom', () => {
const onBlur = cy.stub()

cy.visit('/fixtures/shadow-dom.html')
cy.get('.shadow-5 + input', { includeShadowDom: true }).as('shadow-input').then(($el) => {
$el.on('blur', onBlur).get(0).focus()
})

cy.get('@shadow-input').blur().then(() => {
expect(onBlur).to.be.calledOnce
})
})

describe('assertion verification', () => {
beforeEach(function () {
cy.on('log:added', (attrs, log) => {
Expand Down
13 changes: 9 additions & 4 deletions packages/driver/src/cy/focused.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,19 @@ export const create = (state: StateFunc) => ({
needsFocus ($elToFocus, $previouslyFocusedEl) {
const $focused = this.getFocused()

// if we dont have a focused element
// if we don't have a focused element
// we know we want to fire a focus event
if (!$focused) {
return true
}

// if we didnt have a previously focused el
// if we didn't have a previously focused el
// then always return true
if (!$previouslyFocusedEl) {
return true
}

// if we are attemping to focus a differnet element
// if we are attempting to focus a different element
// than the one we currently have, we know we want
// to fire a focus event
if ($focused.get(0) !== $elToFocus.get(0)) {
Expand All @@ -273,11 +273,16 @@ export const create = (state: StateFunc) => ({
return false
},

getFocused (document = state('document')) {
getFocused (document: Document | ShadowRoot | undefined = state('document')) {
if (document) {
const { activeElement } = document

if ($dom.isFocused(activeElement)) {
// if the active element is a shadow root, we need to recursively get the active element of the shadow root
if (activeElement?.shadowRoot && activeElement.shadowRoot.activeElement) {
return this.getFocused(activeElement.shadowRoot)
}

return $dom.wrap(activeElement)
}
}
Expand Down
Loading