如何在TestCafe中的测试的测试文件之间共享全局变量? [英] How to share a global variable between test files from a test in TestCafe?

查看:216
本文介绍了如何在TestCafe中的测试的测试文件之间共享全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于登录目的,我正在手动设置身份验证Cookie,我想在所有测试中共享身份验证令牌.第一次,我必须在测试中执行登录,然后必须将auth令牌保存在变量中,并在测试文件之间共享它.

I'm manually setting auth cookies for my login purpose and I would like to share the Auth token across my tests. 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:

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();
  });

现在,在测试之后,我有了实际的authToken(不为null),如果我必须在所有文件中的所有测试中共享 authToken 变量,那我该怎么办?做?通过这种编码设计,我可以在同一文件(测试套件)中共享authToken.例如:

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? with this coding design I can share authToken in the same file (test suite). for example:

我有一个文件 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();
  });

在上面的测试中,如果我可以做 loginTest.authToken ,那就太好了.

In the above test, if I can do loginTest.authToken that would be great.

PS:以防万一,人们想知道为什么我要设置cookie而不使用useRole.只是要通知您useRole在我的设置中不起作用,因为该应用程序会在本地环境中手动设置cookie,因此我必须手动将cookie设置为登录解决方法.

PS: In case, people are wondering why I'm doing setting cookie instead of using 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.

推荐答案

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

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());
});

另请参阅:

导入

导出

这篇关于如何在TestCafe中的测试的测试文件之间共享全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆