Skip to content

Commit

Permalink
Merge pull request #510 from DFE-Digital/cypress-data-download
Browse files Browse the repository at this point in the history
Add test and supporting POM and Cy config for data download
  • Loading branch information
CMurrell148 authored Sep 23, 2024
2 parents e993381 + a72c6c3 commit 68f0260
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ zap-reports/
.auth/
cypress.env.json
**/cypress/screenshots/**
**/cypress/downloads/**

### Visual Studio and VSCode config
.vs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<section class="govuk-!-margin-top-2 govuk-!-margin-bottom-2 export-container">
<input type="hidden" name="Uid" value="@Model.TrustSummary.Uid" />
<a asp-page-handler="Export" asp-route-uid="@Model.TrustSummary.Uid"
class="govuk-button govuk-button--secondary export-button--with-icon">
class="govuk-button govuk-button--secondary export-button--with-icon" data-testid="export-academy-data">
<svg class="export-button__icon" width="20" height="20" viewBox="0 0 29 28" fill="none" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false">
<path d="M24.968 17.5V22.1667C24.968 22.7855 24.7222 23.379 24.2846 23.8166C23.847 24.2542 23.2535 24.5 22.6347 24.5H6.30135C5.68251 24.5 5.08902 24.2542 4.65143 23.8166C4.21385 23.379 3.96802 22.7855 3.96802 22.1667V17.5M8.63468 11.6667L14.468 17.5M14.468 17.5L20.3014 11.6667M14.468 17.5V3.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" />
</svg>
Expand Down
49 changes: 48 additions & 1 deletion tests/NewCypress/cypress.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,59 @@
const { defineConfig } = require("cypress");
const fs = require('fs');
const path = require('path');

module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
config.baseUrl = config.env.url;

// Custom task to find the most recent .xlsx file in the downloads folder
on('task', {
findLatestFile(folderPath) {
const files = fs.readdirSync(folderPath);
const xlsxFiles = files.filter(file => file.endsWith('.xlsx'));

if (xlsxFiles.length === 0) return null;

// Sort files by modified date, latest first
const latestFile = xlsxFiles
.map(fileName => ({
name: fileName,
time: fs.statSync(path.join(folderPath, fileName)).mtime.getTime()
}))
.sort((a, b) => b.time - a.time)[0];

return path.join(folderPath, latestFile.name);
},

// Custom task to delete a file
deleteFile(filePath) {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
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',
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import dataDownload from "../../../pages/trusts/dataDownload";

describe('Trust export and content verification', () => {
beforeEach(() => {
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 academies in trust data as an xlsx and verify it has downloaded and has content', () => {
dataDownload
.clickDownloadButton()
.checkFileDownloaded()
.checkFileHasContent()
.deleteDownloadedFile();
});
});
54 changes: 54 additions & 0 deletions tests/NewCypress/cypress/pages/trusts/dataDownload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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;
}

// Method to find the latest downloaded file
public findLatestDownloadedFile(): Cypress.Chainable<string> {
return cy.task('findLatestFile', 'cypress/downloads').then((latestFile) => {
if (!latestFile) {
throw new Error('No downloaded file found');
}
return latestFile as string;
});
}

// 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;
}

// 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;
}

// 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) {
cy.log(result.message || 'Failed to delete file');
}
});
});
return this;
}
}

const dataDownload = new DataDownload();
export default dataDownload;

0 comments on commit 68f0260

Please sign in to comment.