且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 TestCafe 中的测试***享测试文件之间的全局变量?

更新时间:2023-11-14 08:20:52

参考提取可重用测试代码收据以查找有关如何在测试用例之间共享测试代码的信息.

Refer to the Extract Reusable Test Code receipt to find information on how you can share your test code between your test cases.

此外,如果您只想在特定夹具的测试之间共享您的对象,您可以使用夹具上下文对象:在夹具挂钩和测试代码之间共享变量.

Also, you can use the fixture context object if you want to share your object only between tests of a particular fixture: Sharing Variables Between Fixture Hooks and Test Code.

例如:

helper.js

var authToken = 111;

function setToken(x) {
    authToken = x;
}

function getToken(x) {
    return authToken;
}

export { setToken, getToken };

test.js

import { getToken, setToken } from './helper.js'

fixture("test1")
        .page("http://localhost");

test('test1', async t => {
    console.log('token: ' + getToken());
    setToken(111);
});

test1.js

import { getToken } from './helper.js'

fixture("test2")
        .page("http://localhost");

test('test2', async t => {
    console.log('token2: ' + getToken());
});

另见:

导入

导出