ubc-web-services / cypress-d10-test
为UBC WS通用组件的Cypress测试套件
Requires
- drupal/core: ^9 || ^10
This package is auto-updated.
Last update: 2024-08-29 21:15:01 UTC
README
以下是设置您的项目Cypress D10测试套件的说明,以及如何在Cypress中编写测试。
这些说明也可以在Confluence上找到。然而,这两个文档并未保持同步,此README应被视为最新版本。
最小设置
- 运行
composer require ubc-web-services/cypress-d10-test
- 按正常本地开发启动lando
- 在项目根目录下,运行
npm install --save-dev cypress && npm install --save-dev cypress-real-events
(应至少为版本13,否则将提示更新) - 要启动Cypress,运行
npx cypress open
- 如果提示,按照迁移步骤接受所有更改(这是如果已安装较旧的Cypress版本)
- 用cp -f vendor/ubc-web-services/cypress-d10-test/cypress.config.js .覆盖根目录下的当前cypress.config.js/cypress.json文件。
- 此时,我们不需要根目录下的cypress文件夹。要么删除该文件夹,要么将其重命名为cypress_custom,以便cypress.config.js可以识别其中的测试。
- 将cypress.config.js的baseUrl更改为lando站点的URL(例如https://exampleitubcca.lndo.site)
如何编写Cypress测试
一个好的基本教程可以在这里找到: https://docs.cypress.io/guides/end-to-end-testing/writing-your-first-end-to-end-test
Cypress已经提供了示例测试供参考:https://github.com/cypress-io/cypress-example-recipes
您还可以将vendor/ubc-web-services/cypress-d10-test/e2e中的测试作为编写自己测试的参考。
以下是一个简单的.cy.js文件示例
describe("[Generic] Test Suite - tests related to XYZ", () => {
beforeEach(() => {
cy.doLogin(); // triggers login custom helper function, which uses lando drush uli to login
cy.visit('/admin/reports/dblog');
})
it("Checks that the title loaded (fails if whitescreen)", () => {
// cy.get uses CSS selectors
cy.get('#block-gin-page-title').should('contain', 'Recent log messages');
// using .contains() or .find() are also great ways to "grab hold" of an element, especially if it has bad selector options
// Alternative approach
cy.contains("Recent log messages").should("exist");
})
})
如何将测试添加为自定义测试
在cypress.config.js内部,有一个选项'specPattern',它指定了测试的查找位置。
已经添加了一个名为"cypress_custom"的路径,这意味着放置在该文件夹内的测试将被Cypress识别(例如cypress_custom/test.cy.js或cypress_custom/test/hello.cy.js)。
您还可以将您选择的目录的路径追加到'specPattern'中。
如何忽略测试
与'specPattern'类似,在cypress.config.js内部还有一个选项'excludeSpecPattern',它忽略匹配特定路径的测试。
已经包含了一个包含辅助函数的文件的路径,可以用作参考。
如何编写“好的”测试
我不知道!!!然而,有一些好的资源
https://docs.cypress.io/guides/core-concepts/introduction-to-cypress
总的来说,文档读起来相当简单:https://docs.cypress.io/guides/
如何避免将敏感参数提交到仓库
请参阅https://docs.cypress.io/guides/guides/environment-variables#Option-2-cypressenvjson了解如何添加环境变量。
项目链接
Cypress烟雾测试("默认"仓库):https://github.com/ubc-web-services/cypress-d10-test
之前合作学生使用Cypress进行测试的仓库:https://github.com/ubc-web-services/cypress-test#introduction
自动化事后分析:https://github.com/ubc-web-services/cypress-d10-test/blob/master/automation-post-mortem.md