如何在赛普拉斯中声明localStorage

匿名co夫

我担心自己做错了,但是文档中没有明确的示例。

我有一个通过登录流程的测试。我还想验证一下登录后在localStorage内是否已设置某些值。

当我执行以下操作时,我收到一个AssertionError,“预期存在null”:

describe("Trying to log in...", function() {
  it("Visits the Home Page", function() {
    cy.visit("/") // Uses baseUrl prefix from config

    // [ ... snip ... ]

    // Form should be visible and fillable now!
    cy.get("form")
      .should("be.visible")
      .within(() => {
        cy.findByLabelText(/email address/i)
          .should("exist")
          .should("be.visible")
          .click() // Clicking the label should focus the element:
          .type("[test username]")
        cy.findByLabelText(/password/i)
          .click()
          .type("[test password]")
        cy.findByText(/sign in/i).click()
      })

    // Now we're logged in... 

    cy.url().should("include", "home")

    // ========= THE FOLLOWING BREAKS: =======================
    // Check for our localStorage item:
    expect(localStorage.getItem("KeyToOurValue")).to.exist()
  })
})

但是,如果我将其expect放在最后一个回调中should,则似乎可行?

describe("Trying to log in...", function() {
  it("Visits the Home Page", function() {
    cy.visit("/") // Uses baseUrl prefix from config

    // [ ... snip ... ]

    // Form should be visible and fillable now!
    cy.get("form")
      .should("be.visible")
      .within(() => {
        cy.findByLabelText(/email address/i)
          .should("exist")
          .should("be.visible")
          .click() // Clicking the label should focus the element:
          .type("[test username]")
        cy.findByLabelText(/password/i)
          .click()
          .type("[test password]")
        cy.findByText(/sign in/i).click()
      })

    // Now we're logged in... 

    cy.url().should("include", "home", ()=> {
        // ========= THE FOLLOWING WORKS! ========
        // Check for our localStorage item:
        expect(localStorage.getItem("KeyToOurValue")).to.exist()
    })
  })
})

那是我应该怎么做?

看来我应该可以做第一个方法?

在某一cy行运行之后,对localStorage值进行资产化的正确方法是什么

洪特兰

您应该在should块中声明localStorage吗?是的你应该。查看赛普拉斯的官方示例


对于cy命令(cy.put,cy.get ...),它们已入队并由Cypress接连运行。另一方面,非cy命令(期望)会立即执行。因此,如果你离开你的localStorage不可预料之外应该() 因为登录还没有完成。

通过移动期望里面应该,它的后运行手段cy.url()完成。由于cy.url是赛普拉斯内部队列中的最后一项,因此其他cy命令(cy.visit,cy.get,cy.findBy ...)已经完成。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在赛普拉斯中存根节点模块?

如何在赛普拉斯中访问baseURL的值

如何在赛普拉斯中单击x次

如何在赛普拉斯中创建命令

赛普拉斯-如何在赛普拉斯中进行轮询?

赛普拉斯布尔声明

如何在赛普拉斯中使用变量

赛普拉斯如何在失败时提出请求

如何在赛普拉斯中使用软断言

如何在赛普拉斯中获取隐藏的iframe元素?以及如何写呢?

如何验证赛普拉斯中的错误消息?

如何获得赛普拉斯中隐藏元素的HTML?

如何避免赛普拉斯中的重复代码

在赛普拉斯中,如何等待页面加载?

在赛普拉斯中如何计算包含文本的元素?

如何从赛普拉斯的 .sql 文件中读取数据?

如何在赛普拉斯中动态生成测试用例?

如何在赛普拉斯中编写自定义命令?

如何在赛普拉斯中获取一行并选择特定的TD?

如何在赛普拉斯中自定义断言

如何在赛普拉斯测试中公开/访问Redux之类的数据存储?

如何在赛普拉斯中重复动作并获得每个动作的结果

您如何在赛普拉斯自定义命令中调用“ request”?

如何在赛普拉斯中返回文档尺寸以供以后测试

如何在赛普拉斯配置文件中引用环境变量?

赛普拉斯(Cypress.IO):如何在select中获取所选选项的文本?

如何在赛普拉斯中检查电子邮件验证

如何在赛普拉斯中检查多个CSS类的元素?

如何在赛普拉斯中测试以某种形状的对象作为参数的存根函数?