在夹具中创建测试数据

问题描述 投票:0回答:1

目前,我正在开发一个项目,我们必须创建用户并为这些用户进行测试。我正在使用

faker
生成用户的名字、姓氏和其他数据。我想创建一个包含这些详细信息的用户并将它们保存在变量中,然后使用它们调用测试。

我尝试了很多方法,例如从函数调用它们、从另一个测试调用它们,但我未能将创建的数据传递到另一个测试。

创建用户

fixture "Create test data and pass them to the other tests"
  .page('url')
  .beforeEach(async (t) => {
    await t
      .typeText("#txtUserName", 'username')
      .typeText("#txtPassword", 'password')
      .click("#btnLogin");
  });

test("Create test data for add family tests", async (t) => {
  await add_bulk_action_page.clickBulkActionButton();
  await add_bulk_action_page.clickAddFamilyButton();
  await add_family_page.selectCentre(<string>userVariables.defaultCentreName);
  var guardianFirstName = await add_family_page.typeGuardianFirstName(
    await getFirstName()
  );
  var guardianLastName = await add_family_page.typeGuardianLastName(
    await getLastName()
  );

  await add_family_page.clickAddFamilyButton();
});

在同一个文件中调用此测试

test("Access created test data", async (t) => {
    await family_list_page.typeSearchText(guardianFirstName);
    await family_list_page.typeSearchText(guardianLastName);
});

我无法给出比这些代码段更多的内容。对不起!希望这是清楚的。

数据驱动的测试在这方面并不方便,因为我们创建了太多的用户。

请帮助我

typescript testing automated-tests e2e-testing testcafe
1个回答
1
投票

没有例子,很难准确地说出任何事情。请创建一个简单的示例来演示该问题。

一般来说,您需要与测试文件分开创建测试数据。 你可以

  1. 存储或生成整套测试数据并将其导入到测试文件中
  2. 创建一个函数,根据需要生成有关虚假用户的信息,并在测试中使用它。

请参阅以下示例:

测试.js

import { users, createFakeUser } from './test-data';

fixture`fixture`
    .page`http://localhost/testcafe/index4.html`;

test('data object', async t => {
    for (let user of users) {
        await t
            .typeText('#name', user.name, { replace: true })
            .typeText('#email', user.email, { replace: true });
    }
});

test('function to get data', async t => {
    const user = createFakeUser();

    await t
        .typeText('#name', user.name)
        .typeText('#email', user.email);
});

测试数据.js

import { faker } from '@faker-js/faker';

export function createFakeUser () {
    return {
        name:  faker.name.findName(),
        email: faker.internet.email()
    }
}

export const users = [
    createFakeUser(),
    createFakeUser()
];

查看有关在 TestCafe 中使用基于数据的测试的更多信息:

© www.soinside.com 2019 - 2024. All rights reserved.