Skip to content

Commit e0f587e

Browse files
Martyn Chamberlinchrisbreidingjennifer-shehane
authored
fix: iFrame input focus should not cause blur if input already activeElement (#8112)
Co-authored-by: Chris Breiding <[email protected]> Co-authored-by: Jennifer Shehane <[email protected]>
1 parent 19393e0 commit e0f587e

File tree

7 files changed

+47
-5
lines changed

7 files changed

+47
-5
lines changed

packages/driver/src/cy/focused.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,12 @@ const create = (state) => {
8989

9090
const win = $window.getWindowByElement(el)
9191

92-
// store the current focused element
93-
// since when we call .focus() it will change
94-
const $focused = getFocused()
92+
// store the current focused element, since it will change when we call .focus()
93+
//
94+
// need to pass in el.ownerDocument to get the correct focused element
95+
// when el is in an iframe and the browser is not
96+
// in focus (https://github.com/cypress-io/cypress/issues/8111)
97+
const $focused = getFocused(el.ownerDocument)
9598

9699
let hasFocused = false
97100

@@ -220,8 +223,8 @@ const create = (state) => {
220223
return false
221224
}
222225

223-
const getFocused = () => {
224-
const { activeElement } = state('document')
226+
const getFocused = (document = state('document')) => {
227+
const { activeElement } = document
225228

226229
if ($dom.isFocused(activeElement)) {
227230
return $dom.wrap(activeElement)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const e2e = require('../support/helpers/e2e').default
2+
const Fixtures = require('../support/helpers/fixtures')
3+
4+
describe('e2e issue 8111 iframe input focus', function () {
5+
e2e.setup()
6+
7+
e2e.it('iframe input retains focus when browser is out of focus', {
8+
// this test is dependent on the browser being Chrome headed
9+
// and also having --auto-open-devtools-for-tabs plugins option
10+
// (which pulls focus from main browser window)
11+
project: Fixtures.projectPath('issue-8111-iframe-input'),
12+
spec: 'iframe_input_spec.js',
13+
browser: 'chrome',
14+
headed: true,
15+
})
16+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"supportFolder": false
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
it('can type into an input in an iframe that calls auto focus', () => {
2+
cy.visit('/outer.html')
3+
cy.get('iframe')
4+
.its('0.contentDocument.body').should('not.be.empty')
5+
.then(cy.wrap)
6+
.find('input')
7+
.type(42)
8+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = (on) => {
2+
on('before:browser:launch', (browser, launchOptions) => {
3+
launchOptions.args.push('--auto-open-devtools-for-tabs')
4+
5+
return launchOptions
6+
})
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<input />
2+
<script>
3+
document.querySelector('input').focus();
4+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<iframe src="/inner.html">

0 commit comments

Comments
 (0)