赛普拉斯可以运行在赛普拉斯文件夹之外导入的测试文件吗?
Posted
技术标签:
【中文标题】赛普拉斯可以运行在赛普拉斯文件夹之外导入的测试文件吗?【英文标题】:Can Cypress run test files imported outside of Cypress folder? 【发布时间】:2020-10-26 01:56:42 【问题描述】:我正在使用 Cypress 开发 E2E 测试框架,并希望导入 Cypress 目录之外的规范文件(包含以下目录:夹具、集成、插件、支持)。当我开始一些初始测试以验证它是否会按预期工作时,我收到这些错误消息,指出错误源自我的测试代码,而不是赛普拉斯。错误的原因是 TypeError 声明:Cannot read property 'includes' of undefined
。
关于这个问题的互联网搜索并没有真正的帮助,因为似乎没有其他人对这种行为有任何问题(至少从我的角度来看)。即使我将选择作为导入的规范文件移动到cypress/integration/
,同样的问题也会重现。
在这一点上,我不知道这个问题的原因是什么以及如何解决它。我有什么遗漏吗?
这是我在 package.json 中得到的(devDependencies 和 main 字段):
"main":"index.js",
"devDependencies":
"@cypress/webpack-preprocessor": "^5.4.1",
"cypress": "^4.9.0",
"selenium-webdriver": "^4.0.0-alpha.7",
"webpack": "^4.43.0"
我的规范在 Cypress 目录之外导入规范文件 (cypress/integration/RunE2ETest.spec.js):
import '../../Cypress_E2E_Example/SteamHeader.spec';
赛普拉斯文件夹之外的规范文件 (SteamHeader.spec) 只是显示了我如何通过导入、导出设置它:
import '../Cypress-E2E/BaseTest';
export default describe('Test Suite for ....' () =>
it('test example'), () =>
我的 BaseTest 的文件:
import default before('BaseSetup', () =>
cy.visit('/');
错误详情: 导致错误的行(node_modules/global-dirs/index.js:28:1):
26 |
27 | // Homebrew special case: `$(brew --prefix)/lib/node_modules/npm/npmrc`
> 28 | if (process.execPath.includes('/Cellar/node'))
| ^
29 | const homebrewPrefix = process.execPath.slice(0, process.execPath.indexOf('/Cellar/node'));
30 | return path.join(homebrewPrefix, '/lib/node_modules/npm/npmrc');
31 |
堆栈跟踪:
We dynamically generated a new test to display this failure.
at getGlobalNpmrc (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:86028:23)
at getNpmPrefix (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:86064:30)
at Object.eval (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:86072:32)
at Object.512._process (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:86120:4)
at o (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:1:265)
at eval (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:1:316)
at Object.eval (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:89116:20)
at Object.551.fs (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:89130:4)
at o (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:1:265)
at eval (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:1:316)
at Object.eval (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:70417:28)
at Object.425../fs (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:70842:4)
at o (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:1:265)
at eval (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:1:316)
at Object.eval (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:67474:12)
at Object.411../lib/cypress (https://store.steampowered.com/__cypress/tests?p=cypress\integration\RunE2ETest.spec.js:67503:4)
【问题讨论】:
【参考方案1】:我们遇到了同样的错误。这是由cypress
模块的导入引起的。我猜这个模块不打算在浏览器中加载的文件中导入。
【讨论】:
这是否意味着您必须将库文件和测试脚本存储在 cypress 目录下?我删除了文件中要求导入的所有行,并用 require() 替换了它们。由于我使用 cypress.json 进行设置,测试脚本至少能够加载 URL,但我得到了同样的错误。我想在 Support 目录下的 Cypress index.js 中包含测试库,得到同样的错误。尽管赛普拉斯在前端自动化方面拥有诸多优势,但赛普拉斯目录中的灵活性并不高。 好吧,正如预期的那样,只要我在 Cypress 目录中添加了我的 POM 和测试脚本,它就会按预期运行。以上是关于赛普拉斯可以运行在赛普拉斯文件夹之外导入的测试文件吗?的主要内容,如果未能解决你的问题,请参考以下文章