其他测试框架 强大的全新 Web UI 测试框架 Cypress - 只测该测之处

非洲赵子龙 for Cypress · February 13, 2019 · Last by xiaolei Liu replied at September 18, 2019 · 3179 hits

在用 Selenium、RT 或其他 web UI 自动化工具时,为了保证测试用例的独立性,通常需要做必要 setUp 和 tearDown,比如常见的登录和登出,而用 UI 来做登录和登出是很耗时的 (目前的工具对 cookie、localStorage 等支持都不够友好),在大量的 UI 自动化用例运行时,登录和登出耗时比例不可忽视。Cypress基于异步 JS 实现,几乎可以无限制的操作浏览器存储!真正让你做到 “钱 (Time) 要花在刀刃上”!

  • 自定义登录指令
Cypress.Commands.add('login', (userType, options = {}) => {
  // this is an example of skipping your UI and logging in programmatically

  // setup some basic types
  // and user properties
  const types = {
    admin: {
      name: 'Jane Lane',
      admin: true,
    },
    user: {
      name: 'Jim Bob',
      admin: false,
    }
  }

  // grab the user
  const user = types[userType]

  // create the user first in the DB
  cy.request({
    url: '/seed/users', // assuming you've exposed a seeds route
    method: 'POST',
    body: user,
  })
  .its('body')
  .then((body) => {
    // assuming the server sends back the user details
    // including a randomly generated password
    //
    // we can now login as this newly created user
    cy.request({
      url: '/login',
      method: 'POST',
      body: {
        email: body.email,
        password: body.password,
      }
    })
  })
})
  • 调用自定义指令
describe("测试模块名", function() {
  beforeEach(function() {
    cy.login("admin");
  });
  it("测试用例名", function(){
   ....
   // 真正的测试代码
   ....
})
})

Cypress 入门系列:

注:目前官方团队正在开发 Python 版本,同样的 Free to use,对 JS 恐惧的同学不妨等一等,或者直接 JS 上手,非常简单,VSCode 装上之后,你会爱上 Cypress 和 JS,笔者会慢慢介绍各种 Web UI 自动化复杂场景下 Cypress 的强大应对,目前还没发现 Cypress 无法处理的问题!欢迎一起入坑!QQ 群:947886065.

共收到 5 条回复 时间 点赞

一起入坑,一起讨论😀

9Floor has deleted

看 console 里面是登录成功了,也有返回结果了,但访问后续页面时还是未登录状态,无解

jxd98 回复

不清楚你们的产品实现,你可以问问开发,存登录凭证是存在哪儿的,cookie 还是 localStorage 等,然后把你的返回结果写进去再访问新的 url.

jxd98 回复

现在有解决吗?我也碰到这个问题了

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up