Skip to content

Commit

Permalink
feature: the ability to add steps in tests
Browse files Browse the repository at this point in the history
- `qase.step` - add a step to the test

```ts
it('test', () => {
  qase.step('Step 1', () => {
    cy.visit('https://example.com');
  });
});
```
  • Loading branch information
gibiw committed Sep 17, 2024
1 parent fad87cc commit c3529b6
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 5 deletions.
5 changes: 3 additions & 2 deletions qase-cypress/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ parameterize your tests.
- `qase.parameters` - set the parameters of the test case
- `qase.groupParameters` - set the group parameters of the test case
- `qase.ignore` - ignore the test case in Qase. The test will be executed, but the results will not be sent to Qase.
- `qase.step` - create a step in the test case

For example:

Expand Down Expand Up @@ -206,8 +207,8 @@ module.exports = cypress.defineConfig({
video: false,
e2e: {
setupNodeEvents(on, config) {
require('cypress-qase-reporter/plugin')(on, config)
require('cypress-qase-reporter/metadata')(on)
require('cypress-qase-reporter/plugin')(on, config)
require('cypress-qase-reporter/metadata')(on)
},
},
});
Expand Down
16 changes: 16 additions & 0 deletions qase-cypress/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# [email protected]

## What's new

Added the ability to add steps in tests:

- `qase.step` - add a step to the test

```ts
it('test', () => {
qase.step('Step 1', () => {
cy.visit('https://example.com');
});
});
```

# [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.0-beta.1",
"version": "2.2.0-beta.2",
"description": "Qase Cypress Reporter",
"homepage": "https://github.com/qase-tms/qase-javascript",
"sideEffects": false,
Expand Down
14 changes: 14 additions & 0 deletions qase-cypress/src/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,18 @@ module.exports = function(on) {
return null;
},
});

on('task', {
qaseStepStart(value) {
MetadataManager.addStepStart(value);
return null;
},
});

on('task', {
qaseStepEnd(value) {
MetadataManager.addStepEnd(value);
return null;
},
});
};
39 changes: 38 additions & 1 deletion qase-cypress/src/metadata/manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Metadata } from './models';
import { Metadata, StepStart } from './models';
import { readFileSync, existsSync, unlinkSync, writeFileSync } from 'fs';
import { v4 as uuidv4 } from 'uuid';

const metadataPath = 'qaseMetadata';

Expand All @@ -18,6 +19,9 @@ export class MetadataManager {
ignore: false,
suite: undefined,
comment: undefined,
steps: [],
currentStepId: undefined,
firstStepName: undefined,
};

try {
Expand All @@ -38,6 +42,39 @@ export class MetadataManager {
this.setMetadata(metadata);
}

public static addStepStart(name: string): void {
const metadata = this.getMetadata() ?? {};

if (metadata.firstStepName === name) {
return;
}

if (!metadata.steps) {
metadata.steps = [];
}
const id = uuidv4();
const parentId = metadata.currentStepId ?? undefined;
metadata.steps.push({ timestamp: Date.now(), name, id: id, parentId: parentId });
metadata.currentStepId = id;

if (!metadata.firstStepName) {
metadata.firstStepName = name;
}

this.setMetadata(metadata);
}

public static addStepEnd(status: string): void {
const metadata = this.getMetadata() ?? {};
if (!metadata.steps || !metadata.currentStepId) {
return;
}
const parentId = metadata.steps.reverse().find((step): step is StepStart => step.id === metadata.currentStepId)?.parentId;
metadata.steps.push({ timestamp: Date.now(), status, id: metadata.currentStepId });
metadata.currentStepId = parentId;
this.setMetadata(metadata);
}

public static setSuite(suite: string): void {
const metadata = this.getMetadata() ?? {};
metadata.suite = suite;
Expand Down
16 changes: 16 additions & 0 deletions qase-cypress/src/metadata/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,20 @@ export interface Metadata {
ignore?: boolean;
suite?: string | undefined;
comment?: string | undefined;
steps?: (StepStart | StepEnd)[];
currentStepId?: string | undefined;
firstStepName?: string | undefined;
}

export interface StepStart {
id: string;
timestamp: number;
name: string;
parentId: string | undefined;
}

export interface StepEnd {
id: string;
timestamp: number;
status: string;
}
22 changes: 22 additions & 0 deletions qase-cypress/src/mocha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,25 @@ qase.comment = (
//
});
};

/**
* Add a step to the test case
* @param {string} name
* @param {() => T | PromiseLike<T>} body
* @example
* it('test', () => {
* qase.step("Some step", () => {
* // some actions
* });
* cy.visit('https://example.com');
* });
*/
qase.step = <T = void>(name: string, body: () => T | PromiseLike<T>) => {
return cy.task('qaseStepStart', name).then(() => {
return Cypress.Promise.resolve(body());
}).then(() => {
cy.task('qaseStepEnd', 'passed').then(() => {
//
});
});
};
45 changes: 44 additions & 1 deletion qase-cypress/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ import {
Attachment,
FrameworkOptionsType,
ConfigType,
TestStepType,
StepStatusEnum,
} from 'qase-javascript-commons';

import { traverseDir } from './utils/traverse-dir';
import { configSchema } from './configSchema';
import { ReporterOptionsType } from './options';
import { MetadataManager } from './metadata/manager';
import { StepEnd, StepStart } from './metadata/models';

const {
EVENT_TEST_FAIL,
Expand Down Expand Up @@ -232,7 +235,7 @@ export class CypressQaseReporter extends reporters.Base {
relations: relations,
run_id: null,
signature: this.getSignature(test, ids),
steps: [],
steps: metadata?.steps ? this.getSteps(metadata.steps) : [],
id: uuidv4(),
execution: {
status: test.state
Expand Down Expand Up @@ -296,4 +299,44 @@ export class CypressQaseReporter extends reporters.Base {

return undefined;
}

private getSteps(steps: (StepStart | StepEnd)[]): TestStepType[] {
const result: TestStepType[] = [];
const stepMap = new Map<string, TestStepType>();

for (const step of steps.sort((a, b) => a.timestamp - b.timestamp)) {
if (!('status' in step)) {
const newStep = new TestStepType();
newStep.id = step.id;
newStep.execution.status = StepStatusEnum.failed;
newStep.execution.start_time = step.timestamp;
newStep.execution.end_time = Date.now();
newStep.data = {
action: step.name,
expected_result: null,
};

const parentId = step.parentId;
if (parentId) {
newStep.parent_id = parentId;
const parent = stepMap.get(parentId);
if (parent) {
parent.steps.push(newStep);
}
} else {
result.push(newStep);
}

stepMap.set(step.id, newStep);
} else {
const stepType = stepMap.get(step.id);
if (stepType) {
stepType.execution.status = step.status as StepStatusEnum;
stepType.execution.end_time = step.timestamp;
}
}
}

return result;
}
}

0 comments on commit c3529b6

Please sign in to comment.