<legend id='Onwr9'><style id='Onwr9'><dir id='Onwr9'><q id='Onwr9'></q></dir></style></legend><tfoot id='Onwr9'></tfoot>

    <small id='Onwr9'></small><noframes id='Onwr9'>

      <bdo id='Onwr9'></bdo><ul id='Onwr9'></ul>

      1. <i id='Onwr9'><tr id='Onwr9'><dt id='Onwr9'><q id='Onwr9'><span id='Onwr9'><b id='Onwr9'><form id='Onwr9'><ins id='Onwr9'></ins><ul id='Onwr9'></ul><sub id='Onwr9'></sub></form><legend id='Onwr9'></legend><bdo id='Onwr9'><pre id='Onwr9'><center id='Onwr9'></center></pre></bdo></b><th id='Onwr9'></th></span></q></dt></tr></i><div id='Onwr9'><tfoot id='Onwr9'></tfoot><dl id='Onwr9'><fieldset id='Onwr9'></fieldset></dl></div>

        如何在 TestCafe 中的测试中共享测试文件之间的全

        时间:2023-09-06
        <tfoot id='mfDan'></tfoot>

          <bdo id='mfDan'></bdo><ul id='mfDan'></ul>
            1. <small id='mfDan'></small><noframes id='mfDan'>

                <tbody id='mfDan'></tbody>
              <i id='mfDan'><tr id='mfDan'><dt id='mfDan'><q id='mfDan'><span id='mfDan'><b id='mfDan'><form id='mfDan'><ins id='mfDan'></ins><ul id='mfDan'></ul><sub id='mfDan'></sub></form><legend id='mfDan'></legend><bdo id='mfDan'><pre id='mfDan'><center id='mfDan'></center></pre></bdo></b><th id='mfDan'></th></span></q></dt></tr></i><div id='mfDan'><tfoot id='mfDan'></tfoot><dl id='mfDan'><fieldset id='mfDan'></fieldset></dl></div>

              1. <legend id='mfDan'><style id='mfDan'><dir id='mfDan'><q id='mfDan'></q></dir></style></legend>

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

                  上一篇:TestCafe 有没有办法验证 Chrome 网络调用? 下一篇:我如何从 Web 元素中获取文本并在控制台中打印

                  相关文章

                  最新文章

                      <bdo id='dlmPW'></bdo><ul id='dlmPW'></ul>

                  1. <small id='dlmPW'></small><noframes id='dlmPW'>

                    <tfoot id='dlmPW'></tfoot>

                    <legend id='dlmPW'><style id='dlmPW'><dir id='dlmPW'><q id='dlmPW'></q></dir></style></legend>
                    <i id='dlmPW'><tr id='dlmPW'><dt id='dlmPW'><q id='dlmPW'><span id='dlmPW'><b id='dlmPW'><form id='dlmPW'><ins id='dlmPW'></ins><ul id='dlmPW'></ul><sub id='dlmPW'></sub></form><legend id='dlmPW'></legend><bdo id='dlmPW'><pre id='dlmPW'><center id='dlmPW'></center></pre></bdo></b><th id='dlmPW'></th></span></q></dt></tr></i><div id='dlmPW'><tfoot id='dlmPW'></tfoot><dl id='dlmPW'><fieldset id='dlmPW'></fieldset></dl></div>