From 830d2280ff281ec2e0953907d712f705a0c6d627 Mon Sep 17 00:00:00 2001 From: KHeo Date: Wed, 12 Aug 2020 11:26:04 +0900 Subject: [PATCH 1/7] Add test for issue-6125.html --- .../driver/cypress/fixtures/issue-6125.html | 20 +++++++++++++++++++ .../driver/cypress/fixtures/issue-6125.js | 12 +++++++++++ .../integration/commands/actions/type_spec.js | 7 +++++++ packages/driver/src/cy/keyboard.ts | 4 ++-- 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 packages/driver/cypress/fixtures/issue-6125.html create mode 100644 packages/driver/cypress/fixtures/issue-6125.js diff --git a/packages/driver/cypress/fixtures/issue-6125.html b/packages/driver/cypress/fixtures/issue-6125.html new file mode 100644 index 000000000000..d4a63a75c1ce --- /dev/null +++ b/packages/driver/cypress/fixtures/issue-6125.html @@ -0,0 +1,20 @@ + + + + + + Sign In | Simple + + +
+ + + + + + + +
+ + + diff --git a/packages/driver/cypress/fixtures/issue-6125.js b/packages/driver/cypress/fixtures/issue-6125.js new file mode 100644 index 000000000000..cca9307836a3 --- /dev/null +++ b/packages/driver/cypress/fixtures/issue-6125.js @@ -0,0 +1,12 @@ +// Extracted the code that causes the problem in signin.simple.com +// The cause was overriding the Event class. + +(function() { + this.Event = function(n, i) { + const r = n.type + + if (r.indexOf("key")) { + // do something + } + } +})() diff --git a/packages/driver/cypress/integration/commands/actions/type_spec.js b/packages/driver/cypress/integration/commands/actions/type_spec.js index fdc823b6a283..4c6be178267b 100644 --- a/packages/driver/cypress/integration/commands/actions/type_spec.js +++ b/packages/driver/cypress/integration/commands/actions/type_spec.js @@ -177,6 +177,13 @@ describe('src/cy/commands/actions/type - #type', () => { .should('have.value', 'foobar') }) + // https://github.com/cypress-io/cypress/issues/6125 + it('works even if Event class is overridden', () => { + cy.visit('fixtures/issue-6125.html') + cy.get('#login_username') + .type('foobar') + }) + describe('actionability', () => { it('can forcibly type + click even when element is invisible', () => { const $txt = cy.$$(':text:first').hide() diff --git a/packages/driver/src/cy/keyboard.ts b/packages/driver/src/cy/keyboard.ts index 2152422ff2a6..c44de36a807c 100644 --- a/packages/driver/src/cy/keyboard.ts +++ b/packages/driver/src/cy/keyboard.ts @@ -902,8 +902,8 @@ export class Keyboard { } else { // For some reason we can't set certain props on Keyboard Events in chrome < 63. // So we'll use the plain Event constructor - // event = new win[eventConstructor](eventType, eventOptions) - event = new win['Event'](eventType, eventOptions) + event = new win[eventConstructor](eventType, eventOptions) + // event = new win['KeyboardEvent'](eventType, eventOptions) _.extend(event, eventOptions) } From 5e2eb30d2107d1fe774602eedff425f1aa8b878e Mon Sep 17 00:00:00 2001 From: KHeo Date: Wed, 12 Aug 2020 11:35:01 +0900 Subject: [PATCH 2/7] Add tests for KeyboardEvent. --- .../driver/cypress/fixtures/issue-5650.html | 18 ++++++++++++++++++ .../integration/commands/actions/type_spec.js | 8 ++++++++ 2 files changed, 26 insertions(+) create mode 100644 packages/driver/cypress/fixtures/issue-5650.html diff --git a/packages/driver/cypress/fixtures/issue-5650.html b/packages/driver/cypress/fixtures/issue-5650.html new file mode 100644 index 000000000000..951d30179a89 --- /dev/null +++ b/packages/driver/cypress/fixtures/issue-5650.html @@ -0,0 +1,18 @@ + + + + + Test input event + + + +
+ + + \ No newline at end of file diff --git a/packages/driver/cypress/integration/commands/actions/type_spec.js b/packages/driver/cypress/integration/commands/actions/type_spec.js index 4c6be178267b..9bab40637b1b 100644 --- a/packages/driver/cypress/integration/commands/actions/type_spec.js +++ b/packages/driver/cypress/integration/commands/actions/type_spec.js @@ -184,6 +184,14 @@ describe('src/cy/commands/actions/type - #type', () => { .type('foobar') }) + // https://github.com/cypress-io/cypress/issues/5650 + it('should trigger KeyboardEvent, not Event, for event listeners', () => { + cy.visit('fixtures/issue-5650.html') + + cy.get('#test-input').type('A') + cy.get('#result').contains('isKeyboardEvent: true') + }) + describe('actionability', () => { it('can forcibly type + click even when element is invisible', () => { const $txt = cy.$$(':text:first').hide() From 0260a70c21143f9d1c6a1335e7203b6050e35023 Mon Sep 17 00:00:00 2001 From: KHeo Date: Wed, 12 Aug 2020 11:35:14 +0900 Subject: [PATCH 3/7] Remove comments. --- packages/driver/src/cy/keyboard.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/driver/src/cy/keyboard.ts b/packages/driver/src/cy/keyboard.ts index c44de36a807c..9c6b6d84e737 100644 --- a/packages/driver/src/cy/keyboard.ts +++ b/packages/driver/src/cy/keyboard.ts @@ -900,10 +900,7 @@ export class Keyboard { // or is IE } else { - // For some reason we can't set certain props on Keyboard Events in chrome < 63. - // So we'll use the plain Event constructor event = new win[eventConstructor](eventType, eventOptions) - // event = new win['KeyboardEvent'](eventType, eventOptions) _.extend(event, eventOptions) } From 2fb3007fbaf2512fa1d73f79699103eeede28aae Mon Sep 17 00:00:00 2001 From: KHeo Date: Wed, 12 Aug 2020 12:30:00 +0900 Subject: [PATCH 4/7] Fix FireFox error. --- packages/driver/src/cy/keyboard.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/driver/src/cy/keyboard.ts b/packages/driver/src/cy/keyboard.ts index 9c6b6d84e737..8453c2515d7c 100644 --- a/packages/driver/src/cy/keyboard.ts +++ b/packages/driver/src/cy/keyboard.ts @@ -900,7 +900,15 @@ export class Keyboard { // or is IE } else { - event = new win[eventConstructor](eventType, eventOptions) + let constructor = win[eventConstructor] + + // When event constructor doesn't exist, fallback to KeyboardEvent. + // It's necessary because FireFox doesn't support InputEvent. + if (typeof constructor !== 'function') { + constructor = win['KeyboardEvent'] + } + + event = new constructor(eventType, eventOptions) _.extend(event, eventOptions) } From d9a68bfd9eb14f5c65424c7350936bcc6dd29e0d Mon Sep 17 00:00:00 2001 From: KHeo Date: Thu, 13 Aug 2020 18:33:34 +0900 Subject: [PATCH 5/7] Fix Firefox error. --- .../driver/src/cy/commands/actions/type.js | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/driver/src/cy/commands/actions/type.js b/packages/driver/src/cy/commands/actions/type.js index b58504a6bc21..7a5754e2d073 100644 --- a/packages/driver/src/cy/commands/actions/type.js +++ b/packages/driver/src/cy/commands/actions/type.js @@ -202,19 +202,25 @@ module.exports = function (Commands, Cypress, cy, state, config) { return } - // issue the click event to the 'default button' of the form - // we need this to be synchronous so not going through our - // own click command - // as of now, at least in Chrome, causing the click event - // on the button will indeed trigger the form submit event - // so we dont need to fire it manually anymore! - if (!clickedDefaultButton(defaultButton)) { - // if we werent able to click the default button - // then synchronously fire the submit event - // currently this is sync but if we use a waterfall - // promise in the submit command it will break again - // consider changing type to a Promise and juggle logging - return cy.now('submit', form, { log: false, $el: form }) + // In Firefox, submit event is automatically fired + // when we send {Enter} KeyboardEvent to the input fields. + // Because of that, we don't have to click the submit buttons. + // Otherwise, we trigger submit events twice. + if (!Cypress.isBrowser('firefox')) { + // issue the click event to the 'default button' of the form + // we need this to be synchronous so not going through our + // own click command + // as of now, at least in Chrome, causing the click event + // on the button will indeed trigger the form submit event + // so we dont need to fire it manually anymore! + if (!clickedDefaultButton(defaultButton)) { + // if we werent able to click the default button + // then synchronously fire the submit event + // currently this is sync but if we use a waterfall + // promise in the submit command it will break again + // consider changing type to a Promise and juggle logging + return cy.now('submit', form, { log: false, $el: form }) + } } } From a6e8763918a2e783bf84f086f0e77975eac9543b Mon Sep 17 00:00:00 2001 From: KHeo Date: Thu, 13 Aug 2020 18:33:51 +0900 Subject: [PATCH 6/7] Fix typo in Firefox name. --- packages/driver/src/cy/keyboard.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/driver/src/cy/keyboard.ts b/packages/driver/src/cy/keyboard.ts index 8453c2515d7c..dfdb62f7f40a 100644 --- a/packages/driver/src/cy/keyboard.ts +++ b/packages/driver/src/cy/keyboard.ts @@ -903,7 +903,7 @@ export class Keyboard { let constructor = win[eventConstructor] // When event constructor doesn't exist, fallback to KeyboardEvent. - // It's necessary because FireFox doesn't support InputEvent. + // It's necessary because Firefox doesn't support InputEvent. if (typeof constructor !== 'function') { constructor = win['KeyboardEvent'] } From 1a94c40667970f897a7663362dfe45d636b4ab3c Mon Sep 17 00:00:00 2001 From: KHeo Date: Mon, 17 Aug 2020 14:56:10 +0900 Subject: [PATCH 7/7] Remove unnecessary issue-5650.html. --- .../driver/cypress/fixtures/issue-5650.html | 18 ------------------ .../integration/commands/actions/type_spec.js | 15 +++++++++++---- 2 files changed, 11 insertions(+), 22 deletions(-) delete mode 100644 packages/driver/cypress/fixtures/issue-5650.html diff --git a/packages/driver/cypress/fixtures/issue-5650.html b/packages/driver/cypress/fixtures/issue-5650.html deleted file mode 100644 index 951d30179a89..000000000000 --- a/packages/driver/cypress/fixtures/issue-5650.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - - Test input event - - - -
- - - \ No newline at end of file diff --git a/packages/driver/cypress/integration/commands/actions/type_spec.js b/packages/driver/cypress/integration/commands/actions/type_spec.js index 9bab40637b1b..3ae5a4a3039c 100644 --- a/packages/driver/cypress/integration/commands/actions/type_spec.js +++ b/packages/driver/cypress/integration/commands/actions/type_spec.js @@ -185,11 +185,18 @@ describe('src/cy/commands/actions/type - #type', () => { }) // https://github.com/cypress-io/cypress/issues/5650 - it('should trigger KeyboardEvent, not Event, for event listeners', () => { - cy.visit('fixtures/issue-5650.html') + it('should trigger KeyboardEvent, not Event, for event listeners', (done) => { + cy.$$('input:first').on('keydown', (e) => { + if (e.originalEvent instanceof e.currentTarget.ownerDocument.defaultView.KeyboardEvent) { + done() + + return + } + + throw new Error('event was not instanceOf KeyboardEvent') + }) - cy.get('#test-input').type('A') - cy.get('#result').contains('isKeyboardEvent: true') + cy.get('input:first').type('A') }) describe('actionability', () => {