401 - Unauthorized in cypress io auto testing

11,656

Solution 1

Here is my way to log in before each test.

const login = () => {
    cy.visit('http://0.0.0.0:8080/#/login');
    cy.get('#username').type('username');
    cy.get('#password').type('1234password$');
    cy.get('#login-button').click();
}

describe('UI', () => {
  // beforeEach(login);
    beforeEach(() => {
        login();
        Cypress.Cookies.preserveOnce('session_id', 'remember_token');
  });

});

Solution 2

I was facing this issue and fix by:

add {failOnStatusCode: false} will ignore the status because Cypress will stop the the run by any 40X status so with {failOnStatusCode: false} will told cypress to continue

cy.visit('/', {failOnStatusCode: false});
Share:
11,656
John
Author by

John

Updated on June 04, 2022

Comments

  • John
    John almost 2 years

    First time to write a Cypress (cypress Io framework) auto test for internal website (http://XXXX:8089/). The access of this website homepage requires special permissions by using active directory (get the windows credential and check if the user belongs to a certain group or have the certain role). The testing script is pretty simple:

    describe('The Home Page', function() {
    it('successfully loads', function() {
    cy.visit('http://XXXX:8089/')
      })
    })
    

    got the 401 -Unauthoized error message. Tried replacing url with embedded windows credential like http://username:password@xxxx:8089/, got the same error. Cannot find a solution through google. Hope someone can kindly help me solve this problem. Thanks a lot.

    • Hackerman
      Hackerman almost 6 years
      Quick question, does the http://username:password@xxxx:8089/ works if you paste the link into a chrome browser?
    • John
      John almost 6 years
      Yes. It will redirect to xxxx:8089
    • Hackerman
      Hackerman almost 6 years
    • John
      John almost 6 years
      Looks so similar to that one. So there is not any solutions available for this question so far?
    • Hackerman
      Hackerman almost 6 years
      The is a solution, but involves implementing some kind of proxy
    • John
      John almost 6 years
      Hackerman, Could you please tell me how?
  • Ben Power
    Ben Power almost 4 years
    I don't think this solves the issue. You won't be able to find any of the DOM elements such as #username if the AD security is in place. The page won't load and you'll get a 401 instead.