Una vez trabajé con Cypress.io antes, pero olvidé por completo cómo solucionar ese error de origen cruzado suyo. Básicamente, este código me da error:
context('searching example',() => { beforeEach('open google',() => { // Cypress.config('chromeWebSecurity', true); cy.visit('https://google.com'); }) describe('google test example', ()=>{ it('search for death star',() => { cy.get("button[role='link']").click() }) }) })El error que estoy recibiendo es:
CypressError Cypress detected a cross origin error happened on page load: > Blocked a frame with origin "https://www.google.com" from accessing a cross-origin frame. Before the page load, you were bound to the origin policy: > https://google.com A cross origin error happens when your application navigates to a new URL which does not match the origin policy above. A new URL does not match the origin policy if the 'protocol', 'port' (if specified), and/or 'host' (unless of the same superdomain) are different. Cypress does not allow you to navigate to a different origin URL within a single test. You may need to restructure some of your test code to avoid this problem. Alternatively you can also disable Chrome Web Security in Chromium-based browsers which will turn off this restriction by setting { chromeWebSecurity: false } in cypress.json.Learn moreIntenté agregar esto a cypress.json sin éxito
{"chromeWebSecurity": false}Permanente, no es: hazaña: Soporte multidominio #18075
Instalar como una versión preliminar usando
npm install https://cdn.cypress.io/beta/npm/9.6.0/win32-x64/feature-multidomain-8f7cc74ba942ead88ab09d632630c1d14679abfb/cypress.tgzDel video de Gleb Bahmutov Visite dos dominios en la misma especificación de Cypress
Aquí hay un código de muestra de esa versión.
Parece el mismo escenario que estás describiendo.
Página de Ejemplo
<body> <div> Go to different domain: <a data-cy="multi_domain_secondary_link" href="http://www.foobar.com:4466/multi_domain_secondary.html">http://www.foobar.com:4466/multi_domain_secondary.html</a> </div> </body>Prueba
// @ts-ignore / session support is needed for visiting about:blank between tests describe('multi-domain', () => { beforeEach(() => { cy.visit('/multi_domain.html') cy.get('a[data-cy="multi_domain_secondary_link"]').click() }) it('tries to find an element that doesn\'t exist and fails', () => { cy.switchToDomain('http://foobar.com:4466', () => { cy.get('#doesnotexist', { timeout: 1000, }) }) }) })El error que está recibiendo es una compensación permanente de cypress y puede leer más al respecto en los documentos de cypress . En pocas palabras, no puede trabajar con URL de diferente origen en la misma prueba.
Dadas las URL a continuación, todas tienen el mismo origen en comparación con https://www.cypress.io
https://cypress.io https://docs.cypress.io https://example.cypress.io/commands/querying Sin embargo, las URL a continuación tendrán orígenes diferentes en comparación con https://www.cypress.io .
http://www.cypress.io (Different protocol) https://docs.cypress.io:81 (Different port) https://www.auth0.com/ (Different host of different superdomain)Solución: intente dividir sus pruebas en dos pruebas diferentes, algo como esto:
it('navigates', () => { cy.visit('https://apple.com') }) // split visiting different origin in another test it('navigates to new origin', () => { cy.visit('https://google.com') // yup all good })