From 6eda47316398bea45aef926e486fb0278ae40afd Mon Sep 17 00:00:00 2001 From: Kukhyeon Heo Date: Tue, 24 Mar 2020 23:41:08 +0900 Subject: [PATCH] Fix cross-origin error for Chrome (#6344) * Set up test. * Detect cross-origin problem for Chrome * Fix comment. * Rename file. * Fix comment * Fix iframe with name attribute. * newline at end of file * Fix comment. * Remove Firefox-specific logic. * Handle the case for Firefox. * Fix logic to always check both document and inspect. Co-authored-by: Zach Bloomquist --- packages/driver/src/cy/chai.js | 24 +++++++++++-------- .../test/cypress/fixtures/cross_origin.html | 7 ++++++ .../cypress/fixtures/cross_origin_name.html | 7 ++++++ .../integration/commands/assertions_spec.js | 12 ++++++++++ 4 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 packages/driver/test/cypress/fixtures/cross_origin.html create mode 100644 packages/driver/test/cypress/fixtures/cross_origin_name.html diff --git a/packages/driver/src/cy/chai.js b/packages/driver/src/cy/chai.js index 9857e70783..a3f91d22fe 100644 --- a/packages/driver/src/cy/chai.js +++ b/packages/driver/src/cy/chai.js @@ -1,5 +1,5 @@ /* eslint-disable prefer-rest-params */ -// tests in driver/test/cypress/integration/commands/assertions_spec.coffee +// tests in driver/test/cypress/integration/commands/assertions_spec.js const _ = require('lodash') const $ = require('jquery') @@ -38,12 +38,6 @@ let chaiUtils = null chai.use(sinonChai) -const getType = function (val) { - const match = /\[object (.*)\]/.exec(Object.prototype.toString.call(val)) - - return match && match[1] -} - chai.use((chai, u) => { chaiUtils = u @@ -89,10 +83,20 @@ chai.use((chai, u) => { const { inspect, setFormatValueHook } = chaiInspect.create(chai) - // prevent tunneling into Window objects (can throw cross-origin errors in firefox) + // prevent tunneling into Window objects (can throw cross-origin errors) setFormatValueHook((ctx, val) => { - if (val && (getType(val) === 'Window')) { - return '[window]' + // https://github.com/cypress-io/cypress/issues/5270 + // When name attribute exists in + + diff --git a/packages/driver/test/cypress/fixtures/cross_origin_name.html b/packages/driver/test/cypress/fixtures/cross_origin_name.html new file mode 100644 index 0000000000..0e54c2a607 --- /dev/null +++ b/packages/driver/test/cypress/fixtures/cross_origin_name.html @@ -0,0 +1,7 @@ + + + + + + + diff --git a/packages/driver/test/cypress/integration/commands/assertions_spec.js b/packages/driver/test/cypress/integration/commands/assertions_spec.js index 1b6ab84378..fda6ecf696 100644 --- a/packages/driver/test/cypress/integration/commands/assertions_spec.js +++ b/packages/driver/test/cypress/integration/commands/assertions_spec.js @@ -2621,4 +2621,16 @@ describe('src/cy/commands/assertions', () => { }) }) }) + + context('cross-origin iframe', () => { + it(`doesn't throw when iframe exists`, () => { + cy.visit('fixtures/cross_origin.html') + cy.get('.foo').should('not.be.visible') + }) + + it(`doesn't throw when iframe with name attribute exists`, () => { + cy.visit('fixtures/cross_origin_name.html') + cy.get('.foo').should('not.be.visible') + }) + }) })