Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: issue with attaching screenshots for failed tests in subdirectories #724

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/cypressCucumber/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"cypress": "^13.16.0",
"cypress-cucumber-preprocessor": "^4.3.1",
"cypress-multi-reporters": "^1.6.3",
"cypress-qase-reporter": "2.2.5"
"cypress-qase-reporter": "^2.2.5"
},
"cypress-cucumber-preprocessor": {
"step_definitions": "cypress/step_definitions/"
Expand Down
36 changes: 33 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
@@ -1,19 +1,25 @@
import * as fs from 'fs';
import * as path from 'path';

export class FileSearcher {

Check warning on line 4 in qase-cypress/src/fileSearcher.ts

View workflow job for this annotation

GitHub Actions / Project qase-cypress - Node 16

Unexpected class with only static properties

Check warning on line 4 in qase-cypress/src/fileSearcher.ts

View workflow job for this annotation

GitHub Actions / Project qase-cypress - Node 16

Unexpected class with only static properties

Check warning on line 4 in qase-cypress/src/fileSearcher.ts

View workflow job for this annotation

GitHub Actions / Project qase-cypress - Node 18

Unexpected class with only static properties

Check warning on line 4 in qase-cypress/src/fileSearcher.ts

View workflow job for this annotation

GitHub Actions / Project qase-cypress - Node 18

Unexpected class with only static properties
/**
* 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 @@
}
};

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 @@

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 Expand Up @@ -224,7 +224,7 @@
}

const result: TestResultType = {
attachments: attachments ?? [],

Check warning on line 227 in qase-cypress/src/reporter.ts

View workflow job for this annotation

GitHub Actions / Project qase-cypress - Node 16

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined

Check warning on line 227 in qase-cypress/src/reporter.ts

View workflow job for this annotation

GitHub Actions / Project qase-cypress - Node 16

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined

Check warning on line 227 in qase-cypress/src/reporter.ts

View workflow job for this annotation

GitHub Actions / Project qase-cypress - Node 18

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined

Check warning on line 227 in qase-cypress/src/reporter.ts

View workflow job for this annotation

GitHub Actions / Project qase-cypress - Node 18

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined
author: null,
fields: metadata?.fields ?? {},
message: message,
Expand Down
Loading