tau-advanced-playwright icon indicating copy to clipboard operation
tau-advanced-playwright copied to clipboard

[QUESTION] How to load testing data based on the environment?

Open mareru opened this issue 5 months ago • 0 comments

🐞 Describe the question: I am wondering how to do the test data mapping based on the environment? For example, in your spec book-with-fixture-and-api-isolated-auth.spec.ts I would like to have userData in a folder data/qa/user-data and also data/staging/user-data and load the test data for users based on the environment.

I have something like this:

function getDataFilePath(fileName: string): string {
  const env = process.env.testEnvironment || 'uat';
  return `data/${env}/${fileName}.ts`;
}

async function importDataFile<T = object>(filePath: string): Promise<T> {
  const response = await import(`../${filePath}`);
  return response.default as T;
}
export type TestData = {
  loginData: any;
  categoryData: any;
  paymentData: any;
  messagesData: any;
  shipmentData: any;
  urlPartData: any;
  productData: any;
};

export const getTestData = async function (): Promise<TestData> {
  const loginFilePath = getDataFilePath('login');
  const categoryFilePath = getDataFilePath('category');
  const paymentFilePath = getDataFilePath('payment');
  const messagesFilePath = getDataFilePath('messages');
  const shipmentFilePath = getDataFilePath('shipment');
  const urlPartDataFilePath = getDataFilePath('url-part');
  const productDataFilePath = getDataFilePath('product');
  return {
    loginData: await importDataFile(loginFilePath),
    categoryData: await importDataFile(categoryFilePath),
    paymentData: await importDataFile(paymentFilePath),
    messagesData: await importDataFile(messagesFilePath),
    shipmentData: await importDataFile(shipmentFilePath),
    urlPartData: await importDataFile(urlPartDataFilePath),
    productData: await importDataFile(productDataFilePath),
  };
};

and then in the test I just use testData.loginData.username or whatever I have in those data. Different envs can have different data. But this way I am loosing the intellisens that you have in your example with userData. Do you have recommendations on how to achieve/do this better?

mareru avatar Sep 04 '24 14:09 mareru