Skip to content

Commit 2ad2cc5

Browse files
AlexGalichenkoOleksandr_Halichenko
andauthored
added test case accessor to BeforeStep/AfterStep hooks (#15)
Co-authored-by: Oleksandr_Halichenko <[email protected]>
1 parent 86210c0 commit 2ad2cc5

File tree

5 files changed

+58
-38
lines changed

5 files changed

+58
-38
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
1010
:pencil: - chore
1111
:microscope: - experimental
1212

13+
## [0.6.0]
14+
- :rocket: added test case accessor to BeforeStep/AfterStep hooks
15+
1316
## [0.5.0]
1417
- :rocket: updated gherkin dependency
1518
- :rocket: added _expect_ to default world

package-lock.json

Lines changed: 26 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@qavajs/playwright-runner-adapter",
3-
"version": "0.5.0",
3+
"version": "0.6.0",
44
"description": "adapter for playwright test runner",
55
"scripts": {
66
"build": "tsc",
@@ -14,9 +14,9 @@
1414
"glob": "^11.0.0"
1515
},
1616
"devDependencies": {
17-
"@cucumber/cucumber": "^11.0.0",
18-
"@playwright/test": "^1.46.1",
19-
"@types/node": "^22.5.4",
20-
"typescript": "^5.5.4"
17+
"@cucumber/cucumber": "^11.0.1",
18+
"@playwright/test": "^1.48.1",
19+
"@types/node": "^22.7.7",
20+
"typescript": "^5.6.3"
2121
}
2222
}

src/adapter.spec.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ for (const feature of features) {
3434
for (const testCase of tests) {
3535
const tag = testCase.tags.map((tag: { name: string }) => tag.name);
3636
test(testCase.name, { tag }, async () => {
37+
const result: { status: string, error?: any } = { status: 'passed' };
3738
for (const beforeHook of supportCodeLibrary.beforeTestCaseHookDefinitions) {
3839
if (beforeHook.appliesToTestCase(testCase)) {
3940
await test.step('Before', async () => {
@@ -45,7 +46,7 @@ for (const feature of features) {
4546
await test.step(pickleStep.text, async () => {
4647
for (const beforeStep of supportCodeLibrary.beforeTestStepHookDefinitions) {
4748
if (beforeStep.appliesToTestCase(testCase)) {
48-
await beforeStep.code.apply(world);
49+
await beforeStep.code.apply(world, [testCase]);
4950
}
5051
}
5152
const steps = supportCodeLibrary.stepDefinitions
@@ -54,14 +55,12 @@ for (const feature of features) {
5455
if (steps.length > 1) throw new Error(`'${pickleStep.text}' matches multiple step definitions`);
5556
const [step] = steps;
5657
const { parameters} = await step.getInvocationParameters({
57-
// @ts-ignore
5858
step: {
5959
text: pickleStep.text,
6060
argument: pickleStep.argument
6161
},
6262
world
63-
});
64-
const result: { status: string, error?: any } = { status: 'passed' };
63+
} as any);
6564
try {
6665
await step.code.apply(world, parameters);
6766
} catch (err) {
@@ -70,7 +69,7 @@ for (const feature of features) {
7069
}
7170
for (const afterStep of supportCodeLibrary.afterTestStepHookDefinitions) {
7271
if (afterStep.appliesToTestCase(testCase)) {
73-
await afterStep.code.apply(world, [{result}]);
72+
await afterStep.code.apply(world, [{...testCase, result}]);
7473
}
7574
}
7675
if (result.error) throw result.error;
@@ -79,7 +78,7 @@ for (const feature of features) {
7978
for (const afterHook of supportCodeLibrary.afterTestCaseHookDefinitions) {
8079
if (afterHook.appliesToTestCase(testCase)) {
8180
await test.step('After', async () => {
82-
await afterHook.code.apply(world, [testCase]);
81+
await afterHook.code.apply(world, [{...testCase, result}]);
8382
});
8483
}
8584
}

test/step_definitions/steps.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Given, When, setWorldConstructor, DataTable } from '@cucumber/cucumber';
1+
import { Given, When, setWorldConstructor, DataTable, Before, After, BeforeStep, AfterStep } from '@cucumber/cucumber';
22
import { test as base, expect as baseExpect, Page, Locator } from '@playwright/test';
33
import { PlaywrightWorld } from '../../src/PlaywrightWorld';
44

@@ -68,3 +68,21 @@ When('custom fixture', async function (this: ExtendedPlaywrightWorld) {
6868
When('custom expect', async function (this: ExtendedPlaywrightWorld) {
6969
this.expect(this.page.locator('body')).toAlwaysPass();
7070
});
71+
72+
Before(async function (this: ExtendedPlaywrightWorld, testCase) {
73+
this.expect(testCase).toBeTruthy();
74+
});
75+
76+
After(async function (this: ExtendedPlaywrightWorld, testCase) {
77+
this.expect(testCase).toBeTruthy();
78+
this.expect(testCase.result).toBeTruthy();
79+
});
80+
81+
BeforeStep(async function (this: ExtendedPlaywrightWorld, testCase) {
82+
this.expect(testCase).toBeTruthy();
83+
});
84+
85+
AfterStep(async function (this: ExtendedPlaywrightWorld, testCase) {
86+
this.expect(testCase).toBeTruthy();
87+
this.expect(testCase.result).toBeTruthy();
88+
});

0 commit comments

Comments
 (0)