如何使用Cypress检查sessionStorage值

如何使用Cypress检查sessionStorage值,cypress,Cypress,我是cypress.io的新手,我正在尝试检查sessionStorage中的项是否已设置 如何检查会话存储中的项目是否存在? 我通过调用:cy.wrap(sessionStorage.getItem('someItem')).should('exist')但它看起来像是在初始化时设置的值,因为在第一次测试中,它表示预期为null,在另一次测试中,它预期为旧的sessionStorage值(来自上一次测试) it('可以设置sessionItem',函数(){ cy.visit(“/handl

我是cypress.io的新手,我正在尝试检查sessionStorage中的项是否已设置

如何检查会话存储中的项目是否存在?


我通过调用:
cy.wrap(sessionStorage.getItem('someItem')).should('exist')但它看起来像是在初始化时设置的值,因为在第一次测试中,它表示
预期为null
,在另一次测试中,它预期为旧的sessionStorage值(来自上一次测试)

it('可以设置sessionItem',函数(){
cy.visit(“/handlerUrl”)
//…一些代码
cy.get('[type=submit]')。单击()
cy.url()应('include','/anotherUrl'))
cy.wrap(sessionStorage.getItem('someItem'))。应该('exist');
})

我必须使用
cy.window()
,它可以获取当前活动页面的窗口对象

cy.window().then(win=>{
const someItem=win.sessionStorage.getItem('someItem')
cy.wrap(someItem).should('exist')//或'not.empty'
}); 
有关
cy.window()
的更多信息,请参见。

您也可以使用

cy.window()
  .its("sessionStorage")
  .invoke("getItem", "token")
  .should("exist");