Home > Mobile >  Create test data in a fixture
Create test data in a fixture

Time:03-10

Currently, I am working on a project which we have to create users and do tests for those users. I am using faker to generate user first name, last name and other data. I want to create a user with those details and save them in a variable and then call in the tests using them.

I have tried many methods like calling them from a function, calling from another test and I failed to pass created data to another test.

Create user

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

Call in this test in the same file

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

I cannot give more than these code segments. I am sorry! Hope this is clear.

Data driven tests are not convenient in this matter because we are creating so much of users.

Please help me

CodePudding user response:

Without an example, it's hard to say anything precisely. Please create a simple sample that demonstrates the issue.

In general, you need to create test data separately from test files. You can

  1. store or generate the entire set of testing data and import it in test files
  2. create a function that generates information about a fake user by demand and use it in tests.

See the following example:

test.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);
});

test-data.js

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

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

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

See more info about using data-based tests in TestCafe:

  • Related