cy.origin redirects user to a blank page

Viewed 61

Scenario: I am clicking a login button from my application served on localhost. It redirects me to azure sso login through cy.origin Authentication is performed fine. User logs in successfully to the app. But it redirects me to a blank page and hence rest of the IT blocks get failed. The code attached below works fine but as soon as first IT block passes, upon the execution of second IT block page is set to about:blank so the test cases fail. Question: What should be the workaround so that I can continue testing on application under test?

Second describe gets failed

   Cypress.Commands.add('authenticate', () =>{


    cy.visit('http://localhost:8080/')
    cy.get('input[value="***"]').click();
    cy.origin(`https://login.microsoftonline.com/`, () => {
        
        cy.wait(3000)
        cy.get('#i0116').type('username')
        cy.get('#idSIButton9').click()
        cy.wait(3000)
        cy.get('#i0118').type('password')
        cy.wait(2000)
        cy.get('#idSIButton9').click()
        cy.wait(2000)
        cy.get('#idSIButton9').click();        

})

        cy.wait(6000)
        cy.url().should('contain', 'Welcome') 


})
2 Answers

According to the documentation, that behavior is by design

Take a look at cy.origin()

The cy.origin() command is currently experimental and can be enabled by setting the experimentalSessionAndOrigin flag to true in the Cypress config.

Enabling this flag does the following:

  • It adds the following new behaviors (that will be the default in a future major version release of Cypress) at the beginning of each test:

    • The page is cleared (by setting it to about:blank).

If by "Second describe gets failed" you mean the second test is not visiting the Welcome page, then just explicitly visit cy.visit('http://localhost:8080/') at the beginning of the second test.

This is the recommended approach when using cy.origin.

By the way, you should set http://localhost:8080/ as baseUrl in configuration, and use cy.visit('/') instead - from Cypress best practices.

Cypress.Commands.add("session_thing", (email, password) => {

cy.session([email, password], () => {
cy.visit('http://localhost:8080/AdminWebapp/Welcome.html')
cy.get('input[value="Log In With Office 365"]').click();

cy.origin(
  `https://login.microsoftonline.com/`,
  { args: [email, password] },
  ([email, password]) => {
    cy.wait(3000)
        cy.get('#i0116').type(email)
        cy.get('#idSIButton9').click()
        cy.wait(3000)
        cy.get('#i0118').type(password)
        cy.wait(2000)
        cy.get('#idSIButton9').click()
        cy.wait(2000)
        cy.get('#idSIButton9').click();  
  }
);

cy.url().should('contain', 'Welcome')

}); });

The desired behavior was achieved with above code. It restores the session in beforeEach hook. I am simply calling the cy.visit('/') in every IT block and perform the required actions which is kind of very fast with session feature.

Related