如何在 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 中的测试中共享测试文件之间的全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)
How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)