Skip to content

Commit

Permalink
Add fixes for PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
CMurrell148 committed Sep 23, 2024
1 parent 00c8641 commit 78a8c3d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 24 deletions.
16 changes: 16 additions & 0 deletions tests/NewCypress/cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
});
Original file line number Diff line number Diff line change
@@ -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();
});
});
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
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<string> {

return cy.task('findLatestFile', this.downloadFolder).then((latestFile) => {
// 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;
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) {
cy.log(result.message || 'Failed to delete file');
}
});
});
return this;
}
}

Expand Down

0 comments on commit 78a8c3d

Please sign in to comment.