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 <github@chary.us>
This commit is contained in:
Kukhyeon Heo
2020-03-24 23:41:08 +09:00
committed by GitHub
parent d307e30f30
commit 6eda473163
4 changed files with 40 additions and 10 deletions
+14 -10
View File
@@ -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 <iframe>,
// Firefox returns [object Window] but Chrome returns [object Object]
// So, we try throwing an error and check the error message.
try {
val && val.document
val && val.inspect
} catch (e) {
if (e.stack.indexOf('cross-origin') !== -1 || // chrome
e.message.indexOf('cross-origin') !== -1) { // firefox
return `[window]`
}
}
})
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<head>
</head>
<body>
<iframe src="http://localhost:3501/fixtures/generic.html"></iframe>
</body>
</html>
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<head>
</head>
<body>
<iframe name="random-name" src="http://localhost:3501/fixtures/generic.html"></iframe>
</body>
</html>
@@ -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')
})
})
})