From 51eb45353ee67d7f621a5eb64156084a197d7c71 Mon Sep 17 00:00:00 2001 From: Jose Olarte III Date: Wed, 28 Aug 2024 19:12:59 +0800 Subject: [PATCH] Moved common functions to testUtils --- test-playwright/25-create-project-x10.spec.ts | 22 ++---------- test-playwright/33-record-gift-x10.spec.ts | 36 ++----------------- test-playwright/testUtils.ts | 30 ++++++++++++++++ 3 files changed, 35 insertions(+), 53 deletions(-) diff --git a/test-playwright/25-create-project-x10.spec.ts b/test-playwright/25-create-project-x10.spec.ts index fba7179..bcff9df 100644 --- a/test-playwright/25-create-project-x10.spec.ts +++ b/test-playwright/25-create-project-x10.spec.ts @@ -1,23 +1,5 @@ import { test, expect } from '@playwright/test'; -import { importUser } from './testUtils'; - -// Function to generate a random string of specified length -function generateRandomString(length) { - return Math.random().toString(36).substring(2, 2 + length); -} - -// Function to create an array of unique strings -function createUniqueStringsArray(count) { - const stringsArray = []; - const stringLength = 16; - - for (let i = 0; i < count; i++) { - let randomString = generateRandomString(stringLength); - stringsArray.push(randomString); - } - - return stringsArray; -} +import { importUser, createUniqueStringsArray } from './testUtils'; test('Create 10 new projects', async ({ page }) => { test.slow(); // Extend the test timeout @@ -32,7 +14,7 @@ test('Create 10 new projects', async ({ page }) => { const finalDescriptions = []; // Create an array of unique strings - const uniqueStrings = createUniqueStringsArray(projectCount); + const uniqueStrings = await createUniqueStringsArray(projectCount); // Populate arrays with titles and descriptions for (let i = 0; i < projectCount; i++) { diff --git a/test-playwright/33-record-gift-x10.spec.ts b/test-playwright/33-record-gift-x10.spec.ts index cec1bda..1ae6b0c 100644 --- a/test-playwright/33-record-gift-x10.spec.ts +++ b/test-playwright/33-record-gift-x10.spec.ts @@ -1,35 +1,5 @@ import { test, expect } from '@playwright/test'; -import { importUser } from './testUtils'; - -// Function to generate a random string of specified length -function generateRandomString(length) { - return Math.random().toString(36).substring(2, 2 + length); -} - -// Function to create an array of unique strings -function createUniqueStringsArray(count) { - const stringsArray = []; - const stringLength = 16; - - for (let i = 0; i < count; i++) { - let randomString = generateRandomString(stringLength); - stringsArray.push(randomString); - } - - return stringsArray; -} - -// Function to create an array of two-digit non-zero numbers -function createRandomNumbersArray(count) { - const numbersArray = []; - - for (let i = 0; i < count; i++) { - let randomNumber = Math.floor(Math.random() * 99) + 1; - numbersArray.push(randomNumber); - } - - return numbersArray; -} +import { importUser, createUniqueStringsArray, createRandomNumbersArray } from './testUtils'; test('Record 10 new gifts', async ({ page }) => { test.slow(); // Extend the test timeout @@ -43,8 +13,8 @@ test('Record 10 new gifts', async ({ page }) => { const finalNumbers = []; // Create arrays for field input - const uniqueStrings = createUniqueStringsArray(giftCount); - const randomNumbers = createRandomNumbersArray(giftCount); + const uniqueStrings = await createUniqueStringsArray(giftCount); + const randomNumbers = await createRandomNumbersArray(giftCount); // Populate array with titles for (let i = 0; i < giftCount; i++) { diff --git a/test-playwright/testUtils.ts b/test-playwright/testUtils.ts index 82a1d8c..0d674e9 100644 --- a/test-playwright/testUtils.ts +++ b/test-playwright/testUtils.ts @@ -65,4 +65,34 @@ export async function generateEthrUser(page: Page): Promise { await expect(page.locator('div[role="alert"] button:has-text("Yes")')).toBeHidden(); return newDid; +} + +// Function to generate a random string of specified length +export async function generateRandomString(length: number): Promise { + return Math.random().toString(36).substring(2, 2 + length); +} + +// Function to create an array of unique strings +export async function createUniqueStringsArray(count: number): Promise { + const stringsArray = []; + const stringLength = 16; + + for (let i = 0; i < count; i++) { + let randomString = await generateRandomString(stringLength); + stringsArray.push(randomString); + } + + return stringsArray; +} + +// Function to create an array of two-digit non-zero numbers +export async function createRandomNumbersArray(count: number): Promise { + const numbersArray = []; + + for (let i = 0; i < count; i++) { + let randomNumber = Math.floor(Math.random() * 99) + 1; + numbersArray.push(randomNumber); + } + + return numbersArray; } \ No newline at end of file