简体   繁体   English

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

[英]How to share a global variable between test files from a test in TestCafe?

I'm manually setting auth cookies for my login purpose and I would like to share the Auth token across my tests.我正在为我的登录目的手动设置身份验证 cookie,我想在我的测试中共享身份验证令牌。 The very first time I have to perform a login in a test and then I have to save the auth token in a variable and share it across the test files.我第一次必须在测试中执行登录,然后我必须将身份验证令牌保存在一个变量中并在测试文件中共享它。

Here is the code snippet to explain what and how I'm trying to do:这是解释我要做什么以及如何做的代码片段:

loginTest.js :登录测试.js :

let authToken = null;

fixture`Login test`
  .page(inputData.url)
  .beforeEach(async (t) => {
    const nextMonth = new Date();
    nextMonth.setMonth(nextMonth.getMonth() + 1);
    await t.navigateTo(inputData.url).then(await setCookie('AUTH_COOKIE_ID', authToken, nextMonth));
  });

test
  .before(async () => {
    await loginPage.login(inputData.firstUserEmailId, inputData.firstUserPassword);
    authToken = await getCookie('AUTH_COOKIE_ID');
  })('Verify login test', async (t) => {
    await loginPage.goToPeople(personName);
    await t
      .expect(loginPage.personName.exists)
      .ok();
  });

Now, after the test, I have the actual authToken (not null) and if I have to share the authToken variable across all my tests in all my files then how do I do?现在,在测试之后,我有实际的authToken (非空),如果我必须在所有文件中的所有测试中共享authToken变量,那么我该怎么做? with this coding design I can share authToken in the same file (test suite).通过这种编码设计,我可以在同一个文件(测试套件)中共享authToken for example:例如:

I have a file peopleTest.js :我有一个文件peopleTest.js

fixture`People test`
  .page(inputData.url)
  .beforeEach(async (t) => {
    const nextMonth = new Date();
    nextMonth.setMonth(nextMonth.getMonth() + 1);
    await t.navigateTo(inputData.url).then(await setCookie('AUTH_COOKIE_ID', loginTest.authToken, nextMonth));
  });

test('Verify people test', async (t) => {
    await loginPage.goToPeople(personName);
    await t
      .expect(loginPage.personName.exists)
      .ok();
  });

In the above test, if I can do loginTest.authToken that would be great.在上面的测试中,如果我可以做loginTest.authToken那就太好了。

PS: In case, people are wondering why I'm doing setting cookie instead of using useRole . PS:以防万一,人们想知道为什么我要设置 cookie 而不是使用useRole Just to let you know that the useRole didn't work in my setup as the application sets the cookie manually in my local env so I have to manually set the cookie as a login workaround.只是为了让您知道useRole在我的设置中不起作用,因为应用程序在我的本地环境中手动设置了 cookie,因此我必须手动将 cookie 设置为登录解决方法。

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

For instance:例如:

helper.js helper.js

var authToken = 111;

function setToken(x) {
    authToken = x;
}

function getToken(x) {
    return authToken;
}

export { setToken, getToken };

test.js测试.js

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

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

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

test1.js测试1.js

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

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

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

See also:也可以看看:

import 进口

export 出口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM