javascript - 如何在 TestCafe 的夹具之间使用窗口变量?
问题描述
我有一个装置,“Page1”,我使用随机词生成器创建一个命名输入框。我需要在不同的夹具“Page2”上使用这个变量。我怎样才能做到这一点?我们不能使用window for testcafe,并且所有努力导出结果都会ReferenceError: fixture is not defined
出错。
在第 1 页,我有这样的代码:
const words = require('random-words');
export let inputName;
fixture('Page1');
test('Create Page1', async (t) => {
await loginPage.login();
inputName = `input ${words()}`;
和第 2 页
import {inputName} from './page1';
如果我取出导入语句,一切正常。
解决方案
您可以将变量存储在单独的文件中,例如:
夹具1.js
import getInputName from './get-input-name';
fixture('Page1');
test('Create Page1', async t => {
await t.typeText('input', getInputName());
});
夹具2.js
import getInputName from './get-input-name';
fixture('Page2');
test('Create Page2', async t => {
await t.typeText('input', getInputName());
});
获取输入名称.js
const words = require('random-words');
let inputName = null;
export default function getInputName () {
if (!inputName)
inputName = `input ${words()}`;
return inputName;
}
要获取有关在 TestCafe 中使用帮助程序的更多信息,请查看Docs 中的食谱。
推荐阅读
- vbscript - 使用 VBScript 过滤日志文件中的数据
- google-cloud-platform - Google Cloud Datalab 和 Cloud Shell 访问问题
- python - 使用查询检查关系是否存在
- php - Echoing returned value from previous function
- vue.js - v-form 和 v-text-field 在 vue cli 3.0 中不起作用
- html - AngularJS - 将变量插入脚本属性
- c++ - 如何将 cv::Mat & Img 复制到 unsigned char* img?opencv
- javascript - 发送多笔交易,但一笔总是失败
- xamarin.android - 如何从默认主题更改为其他主题Theme.Material.Light
- node.js - 将 dotenv 与捆绑的客户端代码一起使用