首页 > 解决方案 > 如何在 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';

如果我取出导入语句,一切正常。

标签: javascriptautomated-testse2e-testingweb-testingtestcafe

解决方案


您可以将变量存储在单独的文件中,例如:

夹具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 中的食谱


推荐阅读