Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Nir.Tal authored and Nir.Tal committed Nov 4, 2024
1 parent ea587c3 commit fe21bec
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
24 changes: 4 additions & 20 deletions tests/excel.spec.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
import { Constants } from '../utilities/constants';
import { FileUtils } from '../utilities/fileUtils';
import { expect, test } from '@playwright/test';
import xlsx from 'xlsx';
import path from 'path';

/**
* Reads data from an Excel file and converts it to a JSON array.
* @param {string} filePath - The path to the Excel file.
* @returns {Promise<{ [key: string]: any }[]>} - A promise that resolves to an array of JSON objects representing the data in the Excel file.
*/
export const readExcelFile = async (filePath: string): Promise<{ [key: string]: any }[]> => {
const file = xlsx.readFile(filePath);
const data: any[] = [];
const sheets = file.SheetNames;
for (let i = 0; i < sheets.length; i++) {
const sheetData = xlsx.utils.sheet_to_json(file.Sheets[sheets[i]]);
data.push(...sheetData);
}
return data;
};

const data = await readExcelFile(path.join(Constants.DATA_PATH, 'data.xls'));
const data = await FileUtils.readExcelFile(path.join(Constants.DATA_PATH, 'data.xls'));

data.forEach((record) => {
test(`Login test for ${record.description}`, async ({ page }) => {
await page.goto('https://www.saucedemo.com/');
if (record.user !== undefined) {
if (record.user) {
await page.fill('input[data-test="username"]', record.user);
}
if (record.password !== undefined) {
if (record.password) {
await page.fill('input[data-test="password"]', record.password);
}
await page.click('input[data-test="login-button"]');
Expand Down
22 changes: 22 additions & 0 deletions utilities/fileUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import xlsx from 'xlsx';

/**
* Utility class for file operations
*/
export class FileUtils {
/**
* Reads data from an Excel file and converts it to a JSON array.
* @param {string} filePath - The path to the Excel file.
* @returns {Promise<{ [key: string]: any }[]>} - A promise that resolves to an array of JSON objects representing the data in the Excel file.
*/
static async readExcelFile(filePath: string): Promise<{ [key: string]: any }[]> {
const file = xlsx.readFile(filePath);
const data: any[] = [];
const sheets = file.SheetNames;
for (let i = 0; i < sheets.length; i++) {
const sheetData = xlsx.utils.sheet_to_json(file.Sheets[sheets[i]]);
data.push(...sheetData);
}
return data;
}
}

0 comments on commit fe21bec

Please sign in to comment.