javascript - Cypress - 控制要运行的测试 - 使用 Cypress 进行播种

标签 javascript cypress

在 Integrations 文件夹中,我当前有 4 个文件夹:

|- seeding [Folder1]
|  |- seeding1
|  |- seeding2
|  |- seeding3
|  |- ...
|
|- testing-feature-1 [Folder2]
|  |- test1
|  |- test2
|  |- test3
|  |- ...
|
|- testing-feature-2 [Folder3]
|  |- test4
|  |- test5
|  |- test6
|  |- ...
|
|- testing-feature-3 [Folder4]
|  |- test7
|  |- test8
|  |- test9
|  |- ...

在我看来,随着时间的推移,将会出现更多的“testing-feature-x”文件夹。

<小时/>

我希望能够控制要运行的文件夹。现在我可以运行“所有测试”或单个测试。我是唯一一个缺少“文件夹中运行规范”的人吗?!或者勾选框来选择要运行的测试?

以下是一些有用的场景:

  • 功能 2 已重制。我想运行“testing-feature-2”文件夹中的所有测试。

    目前如何实现:可以使用以下终端命令来完成:
    npx cypress run --spec 'cypress/integration/testing-feature-2/**/*' --浏览器金丝雀 --no-exit
    这显然是可行的,但仍然......
  • 功能 2 和 3 已重新制作。我想运行“testing-feature-2”和“testing-feature-3”文件夹中的所有测试。

    目前如何实现:可以通过这两个终端命令来完成:
    npx cypress run --spec 'cypress/integration/testing-feature-2/**/*' --browser canary --no-exit
    npx cypress run --spec 'cypress/integration/testing-feature-3/**/*' --browser canary --no-exit
    比较烦人一点。但还是可以的。
  • 即将推出全新版本。我想运行除“播种”文件夹之外的所有测试(如果出现问题,可能会一遍又一遍地运行)。

    目前如何实现:为了做到这一点,我需要将“种子”文件夹移出“集成”文件夹(暂时)。然后我可以运行所有测试。但如果播放单个功能,那么我就回到了终端。
    相当烦人。大量的脚法。但仍然可行。

有更好的方法吗?

最佳答案

您可以借助一些 npm 脚本做您想做的事情,从而避免重复输入繁琐的命令。

在你的 package.json 中执行如下操作:

"scripts": {
    "cypress:open": "cypress open",
    "cypress:open:feature-1": "cypress open --config integrationFolder=tests/cypress/integration/feature-1",
    "cypress:open:feature-2": "cypress open --config integrationFolder=tests/cypress/integration/feature-2"
}

npm run cypress:open 将在 integration 文件夹中运行测试

npm run cypress:open-feature-1 将在 integration/feature-1 文件夹中运行测试

您可以将种子函数放入 cypress/support/index.js 并将它们添加到全局对象中,如下所示:

global.school = () => {
  faker.seed(123) // Seeding faker means you get the same details every time
  return {
    _id: faker.random.number(),
    name: `${faker.address.city()} School`,
    slug: faker.lorem.word(),
    email: 'demo@ccc.me',
    password: 'password'
  }
}

在您的测试脚本中,您可以使用如下行调用此函数:

const s = school() // Get a fresh object that looks like a school

希望有帮助

关于javascript - Cypress - 控制要运行的测试 - 使用 Cypress 进行播种,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59662226/

相关文章:

javascript - 在这个二维数组中找到最大的区域

javascript - 猫头鹰轮播将最后一项循环回第一项

javascript - 如何修复 "Cannot find module ' fs-extra' - 错误“将 cypress 文件部署到 jenkins 中时?

slider - cypress 中的 slider 调用更改,但文本框中未捕获更改

node.js - 将 cypress.io 测试用例移交给非技术用户

javascript - 比较 Cypress 中的文本值

javascript - Google 电子表格中的排列/组合

JavaScript 键盘事件和检测

javascript - 无法获取json数据

带有 Cypress 的 iframe : How to wait api call inside iframe