Skip to content

Commit

Permalink
fix: resolved issue with attaching screenshots for failed tests in su…
Browse files Browse the repository at this point in the history
…bdirectories

- Added recursive folder search by a specified name.
- Screenshots for failed tests are now correctly attached, even if the tests are located in subdirectories.
  • Loading branch information
gibiw committed Dec 18, 2024
1 parent 20c04d3 commit eb22c5a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
6 changes: 6 additions & 0 deletions qase-cypress/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# [email protected]

## What's new

Fixed an issue where screenshots for failed tests were not attached if the tests were located in subdirectories.

# [email protected]

## What's new
Expand Down
2 changes: 1 addition & 1 deletion qase-cypress/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cypress-qase-reporter",
"version": "2.2.5",
"version": "2.2.6",
"description": "Qase Cypress Reporter",
"homepage": "https://github.com/qase-tms/qase-javascript",
"sideEffects": false,
Expand Down
40 changes: 36 additions & 4 deletions qase-cypress/src/fileSearcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ export class FileSearcher {
* Finds all files in the given directory and its subdirectories
* that were created after the specified time.
*
* @param folderPath Relative path to the folder.
* @param screenshotFolderPath Path to the folder with screenshots.
* @param specFileName Name of the spec file.
* @param time Time threshold as a Date object.
* @returns Array of absolute paths to the matching files.
*/
public static findFilesBeforeTime(folderPath: string, time: Date): string[] {
const absolutePath = path.resolve(process.cwd(), folderPath);
public static findFilesBeforeTime(screenshotFolderPath: string, specFileName: string, time: Date): string[] {
const absolutePath = path.resolve(process.cwd(), screenshotFolderPath);
const result: string[] = [];

const paths = this.findFolderByName(absolutePath, specFileName);
if (paths.length === 0) {
return result;
}

const searchFiles = (dir: string) => {
if (!fs.existsSync(dir)) {
return;
Expand All @@ -34,7 +40,33 @@ export class FileSearcher {
}
};

searchFiles(absolutePath);
for (const path of paths) {
searchFiles(path);
}

return result;
}

private static findFolderByName(startPath: string, folderName: string): string[] {
const result: string[] = [];

function searchDirectory(currentPath: string): void {
const items = fs.readdirSync(currentPath, { withFileTypes: true });

for (const item of items) {
const itemPath = path.join(currentPath, item.name);

if (item.isDirectory()) {
if (item.name === folderName) {
result.push(itemPath);
} else {
searchDirectory(itemPath);
}
}
}
}

searchDirectory(startPath);
return result;
}
}
2 changes: 1 addition & 1 deletion qase-cypress/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class CypressQaseReporter extends reporters.Base {

const testFileName = this.getTestFileName(test);
const files = this.screenshotsFolder ?
FileSearcher.findFilesBeforeTime(path.join(this.screenshotsFolder, testFileName), new Date(this.testBeginTime))
FileSearcher.findFilesBeforeTime(this.screenshotsFolder, testFileName, new Date(this.testBeginTime))
: [];

const attachments = files.map((file) => ({
Expand Down

0 comments on commit eb22c5a

Please sign in to comment.