From 78a8c3ddc28470d9b5b3ff53fb3cc7ae66b29e68 Mon Sep 17 00:00:00 2001 From: cmurrell148 Date: Fri, 20 Sep 2024 09:49:39 +0100 Subject: [PATCH] Add fixes for PR comments --- tests/NewCypress/cypress.config.ts | 16 ++++++++ .../trusts/data-download-trust.cy.ts | 20 +++++++--- .../{dataDowload.ts => dataDownload.ts} | 40 ++++++++++--------- 3 files changed, 52 insertions(+), 24 deletions(-) rename tests/NewCypress/cypress/pages/trusts/{dataDowload.ts => dataDownload.ts} (55%) diff --git a/tests/NewCypress/cypress.config.ts b/tests/NewCypress/cypress.config.ts index ea148d121..e6f988d32 100644 --- a/tests/NewCypress/cypress.config.ts +++ b/tests/NewCypress/cypress.config.ts @@ -33,11 +33,27 @@ module.exports = defineConfig({ return { success: true }; } return { success: false, message: 'File not found' }; + }, + + // Custom task to check if files exist in the downloads folder + checkForFiles(folderPath) { + const files = fs.readdirSync(folderPath); + return files.length > 0 ? files : null; + }, + + // Custom task to delete all files in the downloads folder + clearDownloads(folderPath) { + const files = fs.readdirSync(folderPath); + files.forEach((file) => { + fs.unlinkSync(path.join(folderPath, file)); + }); + return { success: true }; } }); return config; }, + downloadsFolder: 'cypress/downloads', }, }); diff --git a/tests/NewCypress/cypress/e2e/regression/trusts/data-download-trust.cy.ts b/tests/NewCypress/cypress/e2e/regression/trusts/data-download-trust.cy.ts index eeb4f2b9b..37a0d6217 100644 --- a/tests/NewCypress/cypress/e2e/regression/trusts/data-download-trust.cy.ts +++ b/tests/NewCypress/cypress/e2e/regression/trusts/data-download-trust.cy.ts @@ -1,15 +1,23 @@ -import dataDownload from "../../../pages/trusts/dataDowload"; +import dataDownload from "../../../pages/trusts/dataDownload"; describe('Trust export and content verification', () => { beforeEach(() => { - cy.login() + cy.login(); cy.visit('/trusts/academies/details?uid=5712'); + + // Clear the downloads folder before running each test + cy.task('checkForFiles', 'cypress/downloads').then((files) => { + if (files) { + cy.task('clearDownloads', 'cypress/downloads'); + } + }); }); it('should export a trust and verify it has downloaded and has content', () => { - dataDownload.clickDownloadButton(); - dataDownload.checkFileDownloaded(); - dataDownload.checkFileHasContent(); - dataDownload.deleteDownloadedFile(); + dataDownload + .clickDownloadButton() + .checkFileDownloaded() + .checkFileHasContent() + .deleteDownloadedFile(); }); }); diff --git a/tests/NewCypress/cypress/pages/trusts/dataDowload.ts b/tests/NewCypress/cypress/pages/trusts/dataDownload.ts similarity index 55% rename from tests/NewCypress/cypress/pages/trusts/dataDowload.ts rename to tests/NewCypress/cypress/pages/trusts/dataDownload.ts index b8817b66c..86dd77fdd 100644 --- a/tests/NewCypress/cypress/pages/trusts/dataDowload.ts +++ b/tests/NewCypress/cypress/pages/trusts/dataDownload.ts @@ -1,41 +1,44 @@ -export class DataDownload { - private downloadButton: string; - private downloadFolder: string; - - constructor() { - this.downloadButton = '[data-testid="export-academy-data"]'; - this.downloadFolder = 'cypress/downloads'; - } - - clickDownloadButton(): void { - cy.get(this.downloadButton).click(); +class DataDownload { + elements = { + downloadButton: () => cy.get('[data-testid="export-academy-data"]'), + }; + + // Method to click the download button + public clickDownloadButton(): this { + this.elements.downloadButton().click(); + return this; } - findLatestDownloadedFile(): Cypress.Chainable { - - return cy.task('findLatestFile', this.downloadFolder).then((latestFile) => { + // Method to find the latest downloaded file + public findLatestDownloadedFile(): Cypress.Chainable { + return cy.task('findLatestFile', 'cypress/downloads').then((latestFile) => { if (!latestFile) { throw new Error('No downloaded file found'); } - return latestFile as string; + return latestFile as string; }); } - checkFileDownloaded(): void { + // Method to check if the file is downloaded + public checkFileDownloaded(): this { this.findLatestDownloadedFile().then((downloadFilePath) => { cy.readFile(downloadFilePath, 'binary', { timeout: 10000 }).should('exist'); }); + return this; } - checkFileHasContent(): void { + // Method to check if the file has content + public checkFileHasContent(): this { this.findLatestDownloadedFile().then((downloadFilePath) => { cy.readFile(downloadFilePath, 'binary', { timeout: 10000 }).then((fileContent) => { expect(fileContent.length).to.be.greaterThan(0); }); }); + return this; } - deleteDownloadedFile(): void { + // Method to delete the downloaded file + public deleteDownloadedFile(): this { this.findLatestDownloadedFile().then((downloadFilePath) => { cy.task<{ success: boolean; message?: string }>('deleteFile', downloadFilePath).then((result) => { if (!result.success) { @@ -43,6 +46,7 @@ export class DataDownload { } }); }); + return this; } }