javascript - 如何在 TestCafe 的固定装置之间使用窗口变量?

标签 javascript automated-tests e2e-testing web-testing testcafe

我有一个固定装置“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/

相关文章:

safari - 在 Safari 中可编辑内容上按 Enter 键

javascript - 如何使用 jQuery 在页面加载时关注表单输入文本字段?

javascript - 使用 Object.prototype.toString() 对内置类型进行一般类型检查

javascript - Nodejs - MongoDB findone 完全匹配但不区分大小写

linux - 适合运维团队的测试自动化工具

javascript - 如何在Protractor中控制测试用例在不同浏览器上运行

JavaScript 和 Java WebSocket SSL 连接错误

android - 在没有网络摄像头对话框的情况下启动 Android 模拟器

数组的PHP组合

node.js - Node-sass 没有与 TestCafe Docker Image 中使用的 Node 版本绑定(bind)