我似乎无法在 Cypress 中设置本地存储?
当我从本地存储获取项目时,它与我设置的不同(它总是返回默认值)
我是否设置了错误的存储空间?
beforeEach(() => {
cy.clearLocalStorage();
cy.clearCookies();
window.localStorage.setItem('One', 'NL');
window.localStorage.setItem('Two', 'NL');
window.localStorage.setItem('Three', 'NL');
visitPage('/');
cy.window().then((win) => {
const myItemOne = win.localStorage.getItem('One');
const myItemTwo = win.localStorage.getItem('Two');
const myItemThree = win.localStorage.getItem('Three');
cy.log(myItemOne, myItemTwo, myItemThree);
});
Run Code Online (Sandbox Code Playgroud)
应返回日志 NL、NL、NL,但返回日志 SE、SV、SV
该localstorage
对象对于浏览器中的所有窗口都是相同的,无论您是否使用window
或cy.window()
。
cy.clearCookies();
cy.clearLocalStorage()
.then(() => {
window.localStorage.setItem('One', 'NL');
window.localStorage.setItem('Two', 'NL');
window.localStorage.setItem('Three', 'NL');
})
cy.visit('/')
.then(() => {
const myItemOne = window.localStorage.getItem('One');
const myItemTwo = window.localStorage.getItem('Two');
const myItemThree = window.localStorage.getItem('Three');
expect([myItemOne, myItemTwo, myItemThree]).to.deep.equal(['NL','NL','NL'])
})
Run Code Online (Sandbox Code Playgroud)