question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cookies are broken with cy.origin

See original GitHub issue

Current behavior

I’m trying to test a WordPress plugin. The plugin does different things depending on if the domain is recognized or not.

For this reason I’m running the exact same test (except the last assertion) twice, once normally, and once wrapped in cy.origin. experimentalSessionAndOrigin is set to true

The test without the cy.origin works perfectly.

The one wrapped in cy.origin runs twice (test retry):

  • On the first iteration, it is unable to log in to /wp-admin. The browser freezes for a while, then it returns to the login screen without any errors
  • On the second try, it manages to log in, and displays the WP dashboard, but the very next page navigation kicks us back to the login screen image

This is fairly repeatable

Desired behavior

No response

Test code to reproduce

describe('Manual plugin activation', () => {
  describe('w/ random domain', () => {
    beforeEach(() => {
      // This command brings up the appropriate docker containers, and sets WP up with a random domain,
      // which it adds to the hosts file so the browser can access it.
      // It then executes Cypress.config('baseUrl', baseUrl); to prepare for the tests
      // This seem to be working correctly, as the page does show up correctly in response to cy.visit()
      cy.resetTestingEnv({ randomBaseUrl: true })
    })

    it('should display signup CTA when autoconfig fails', () => {
      cy.origin(Cypress.config('baseUrl'), {}, () => {
        // cy.adminAuth()
        cy.visit('/wp-admin')

        // eslint-disable-next-line cypress/no-unnecessary-waiting
        cy.wait(500) // The login page clears/resets the input from js whan loaded, so need to wait

        cy.contains('Username').type(Cypress.env('CYPRESS_WP_ADMIN_USER'))
        cy.contains('Password').type(Cypress.env('CYPRESS_WP_ADMIN_PASS'))
        cy.contains('Log In').click()

        cy.contains('Dashboard').should('exist')

        // cy.activatePluginGui();
        cy.visit('/wp-admin/plugins.php')

        cy.get('#activate-plugin1').click()
        cy.contains('Thank you for installing plugin1 plugin!').should(
          'exist',
        )

        // cy.visitPluginSettings();
        cy.visit('/wp-admin/admin.php?page=plugin1')
        cy.contains('Create your account')
        .should('have.attr', 'href')
        .and(
          'include',
          'https://www.plugin1-staging.com/signup-wordpress',
        )
      })
    })
  })

  describe('w/ standard domain', () => {
    beforeEach(() => {
      // In this case it does the exact same as above with the exception that it sets a pre-defined domain
      // so it is recognized by the plugin. This domain does match the one given in cypress.config.js -> e2e.baseUrl
      cy.resetTestingEnv()
    })

    it('should display MS admin link when autoconfig succeeds', () => {
      // cy.adminAuth();
      cy.visit('/wp-admin')

      // eslint-disable-next-line cypress/no-unnecessary-waiting
      cy.wait(500) // The login page clears/resets the input from js whan loaded, so need to wait

      cy.contains('Username').type(Cypress.env('CYPRESS_WP_ADMIN_USER'))
      cy.contains('Password').type(Cypress.env('CYPRESS_WP_ADMIN_PASS'))
      cy.contains('Log In').click()

      cy.contains('Dashboard').should('exist')

      // cy.activatePluginGui();
      cy.visit('/wp-admin/plugins.php')

      cy.get('#activate-plugin1').click()
      cy.contains('Thank you for installing plugin1\'s plugin!').should(
        'exist',
      )

      // cy.visitPluginSettings();
      cy.visit('/wp-admin/admin.php?page=plugin1')
      cy.contains('Go to your plugin1 backend').should(
        'have.attr',
        'href',
        'https://admin.plugin1-staging.com/sites/145',
      )
    })
  })
})

Cypress Version

10.3, 10.3.1, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 10.11

Other

I suspect that changing Cypress.config(‘baseUrl’); might be a contributing factor in this issue

Not sure how to create a demo test setup of this complexity (multiple domains, etc.) so please let me know what other information or debug output to provide in order to track down the issue

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:24 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
denke8commented, Sep 14, 2022

Hi @AtofStryker!

I have checked against the latest Cypress (10.8), unfortunately, it’s still broken. I updated the description above and the reproduction repository

2reactions
denke8commented, Sep 4, 2022

I have updated the reproduction repo to Cypress 10.7, so you can work against the latest version. The problem is also present in 10.7

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress basics: Where did my cookies disappear? - Filip Hric
The reason is actually simple. Cypress clears out the state of browser in between tests. All your cookies are deleted from your app...
Read more >
Web Security - Cypress Documentation
Different superdomain per test requires cy.origin command ... In your application code, you set cookies and store a session on the browser.
Read more >
Cypress cy.origin() & Google sign in not working
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our ...
Read more >
Cookie same origin policy
Origin A can access origin B's DOM if match on. (scheme, domain, port). Today: Same Original Policy (SOP) for cookies: ▫ Generally speaking,...
Read more >
cypress-io/cypress - Gitter
Have you tried setting cookie with cy.setCookie(accessTokenCookieName, authResult.accessToken); ? Btw, when you have several cookies, window.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found