我有一个固定装置“Page1”,我使用随机词生成器创建一个命名输入框。我需要在不同的夹具“Page2”上使用这个变量。我怎样才能做到这一点?我们不能为 testcafe 使用 window,并且所有努力都将结果导出为 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';
如果我取出导入语句,一切正常。
最佳答案
您可以将变量存储在单独的文件中,例如:
fixture1.js
import getInputName from './get-input-name';
fixture('Page1');
test('Create Page1', async t => {
await t.typeText('input', getInputName());
});
fixture2.js
import getInputName from './get-input-name';
fixture('Page2');
test('Create Page2', async t => {
await t.typeText('input', getInputName());
});
get-input-name.js
const words = require('random-words');
let inputName = null;
export default function getInputName () {
if (!inputName)
inputName = `input ${words()}`;
return inputName;
}
要获得有关在 TestCafe 中使用助手的更多信息,请查看 a recipe in Docs .
关于javascript - 如何在 TestCafe 的固定装置之间使用窗口变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56842941/