【问题标题】:How to check sessionStorage value with Cypress如何使用赛普拉斯检查 sessionStorage 值
【发布时间】:2019-12-05 10:53:17
【问题描述】:

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

如何检查 sessionStorage 中的项目是否存在?


我已经通过调用:cy.wrap(sessionStorage.getItem('someItem')).should('exist'); 进行了尝试,但看起来该值是在初始化时设置的,因为在第一次测试中它显示为expected null,而在另一个测试中它需要旧的 sessionStorage 值(来自之前的测试) .

it('can set sessionItem', function() {
   cy.visit('/handlerUrl')
   // ... some code
   cy.get('[type=submit]').click()
   cy.url().should('include', '/anotherUrl')
   cy.wrap(sessionStorage.getItem('someItem')).should('exist');
})

【问题讨论】:

    标签: cypress


    【解决方案1】:

    你也可以使用

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

    【讨论】:

      【解决方案2】:

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

      cy.window().then(win=> {
        const someItem = win.sessionStorage.getItem('someItem')
        cy.wrap(someItem).should('exist') // or 'not.empty'
      }); 
      

      更多关于cy.window()的信息可以在cypress.io documentation找到。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多