-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #510 from DFE-Digital/cypress-data-download
Add test and supporting POM and Cy config for data download
- Loading branch information
Showing
5 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
}); |
23 changes: 23 additions & 0 deletions
23
tests/NewCypress/cypress/e2e/regression/trusts/data-download-trust.cy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |