Save local storage across tests to avoid re-authentication

橙三吉。 提交于 2020-01-24 00:34:07

问题


I'm wondering if it is possible to save the state of localStorage across tests. Mainly because I want to avoid re-authentication on each test. I realize that I can create a command that sends an API request to our backend to avoid going through the auth flow but for various reasons this won't work in my situation.

I am asking if it possible to have a workflow like this:

  1. Go to login page: Authenticate get back response and save session to local storage
  2. Persist local storage somehow...
  3. Go through other tests as authenticated user

回答1:


Here's what I ended up doing:

  1. Go to login page: Authenticate At this point we have data we want to persist between tests in localStorage but we are not allowed to whitelist localStorage. However, we are allow to whitelist cookies

I have some code like this inside my support/commands.js that act as helpers

const sessionKeys = {
  authTokens: 'auth.tokens',
  sessionConfig: 'session.config',
};

// The concatenation of username and cid will be the key to set the session
Cypress.Commands.add('persistSession', (key) => {

  const authTokens = localStorage.getItem(key);

  cy.setCookie(key, authTokens);
});

Cypress.Commands.add('restoreSession', (key) => {
  cy.getCookie(key).then(authTokens => {
    localStorage.setItem(key, authTokens.value);
  });

});
  1. So we call cy.persistSession(key) after we login, which means we have all the authentication saved as cookies which are whitelisted inside of support/index.js with code.

Like this:

Cypress.Cookies.defaults({
  whitelist: function(cookie){
    // Persist auth stuff
    const reAuthTokens = new RegExp('.*auth\.tokens');
    if(reAuthTokens.test(cookie.name)){
      return true;
    }

    return false;
  }
});
  1. Now anytime we need our auth tokens inside our other tests before running them we cy.restoreSession(key) and we should be good!



回答2:


Anything you can do in JS you can do in a cypress test. If you have some way to store creds (auth token, etc.) in local storage, I see no reason why you can't do that. If cypress is clearing out your local storage between tests, you will have to write a beforeEach hook that saves an authenticated token (hard-coded by you) to local storage before each test.




回答3:


Here is the useful link that solves my problem like yours: Preserve cookies through multiple tests

my code like:

    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'); 
      });
    });

hope can help you.




回答4:


You can use the cypress-localstorage-commands package to persist localStorage between tests, so you'll be able to do login only once:

In support/commands.js:

import "cypress-localstorage-commands";

In your tests:

before(() => {
  // Do your login stuff here
  cy.saveLocalStorage();
});

beforeEach(() => {
  cy.restoreLocalStorage();
});


来源:https://stackoverflow.com/questions/50393987/save-local-storage-across-tests-to-avoid-re-authentication

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!