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

How to share a global variable between test files from a test in TestCafe?(如何在 TestCafe 中的测试中共享测试文件之间的全局变量?)
本文介绍了如何在 TestCafe 中的测试中共享测试文件之间的全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为登录目的手动设置身份验证 cookie,并且我想在我的测试中共享身份验证令牌.第一次我必须在测试中执行登录,然后我必须将身份验证令牌保存在一个变量中并在测试文件中共享它.

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(非空),如果我必须在所有人之间共享 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 中的测试中共享测试文件之间的全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How do I define global variables in CoffeeScript?(如何在 CoffeeScript 中定义全局变量?)
Is there a spec that the id of elements should be made global variable?(是否有规范将元素的 id 设为全局变量?)
How do I get fetch result from API to store as a global variable?(如何从API获取FETCH结果以存储为全局变量?)
What#39;s the difference between a global var and a window.variable in javascript?(javascript中的全局变量和window.variable有什么区别?)
What are some of the problems of quot;Implied Global variablesquot;?(“隐含全局变量有哪些问题?)
Is event a global variable that is accessible everywhere inside the callback chain?(事件是回调链中随处可访问的全局变量吗?)