node.js - 在 TestCafe 中设置环境变量给出未定义的值
问题描述
我正在学习 TestCafe,我是新手。
我正在尝试遵循此处提到的:https ://devexpress.github.io/testcafe/documentation/recipes/access-environment-variables-in-tests.html
我有一个在我的 Windows 机器上运行的代码!!
fixture`Getting Started`.page`http://devexpress.github.io/testcafe/example`;
test("My First Test", async t => {
await t
.typeText('#developer-name', 'John Smith')
.click('#submit-button')
.takeScreenshot();
const articleHeader = await Selector('.result-content').find('h1');
// Obtain the text of the article header
let headerText = await articleHeader.innerText;
console.log(process.env.DEV_MODE);
});
我的脚本部分是这样的
"scripts": {
"setnode": "set DEV_MODE=staging",
"test:chrome": "npm run setnode & npx testcafe \"chrome --start-fullscreen\" e2e/tests"
}
当我运行这个命令时npm run test:chrome
我得到这样的输出
ui-testcafe-poc@1.0.0 setnode C:\TestCafeProjects\ui-testcafe-poc set DEV_MODE=staging
Running tests in:
- Chrome 79.0.3945.130 / Windows 10
Getting Started
undefined
√ My First Test
为什么console.log 写为未定义而不是暂存?
解决方案
脚本中的 & 符号 ( &
) 并行运行它们。但是,您需要使用&&
操作数按顺序运行它们。
此外,npm 脚本会在后台生成 shell 进程(Windows 上的 cmd),并且该set
命令设置的变量仅存在于其自己的会话中。以下脚本应该可以工作:
"scripts": {
"test:chrome": "set DEV_MODE=staging && npx testcafe \"chrome --start-fullscreen\" e2e/tests"
}
推荐阅读
- javascript - 如何定义事件处理函数来组合事件监听器
- python - 使用 Python Qt5 的可变引用
- webpack - scss 不适用于 vue3 应用程序(TypeError: this.getOptions is not a function)
- custom-post-type - 如何使用 walker-nav 和 elementor 内容生成器创建大型菜单
- javascript - 如何记录所有事件(使用操作)具有事件侦听器的 DOM 元素?
- scala - scala中具有不同列数的表上的联合
- twig - 数据从 drupal 8 中的cropper API 到哪里去,你如何访问它?
- javascript - 如果我做出反应,机器人不会发布
- javascript - MongoDB $Lookup 不显示数据
- ruby-on-rails - 有没有办法修复 Elastic Beanstalk 中的 Gem::LoadError