Skip to content

Commit

Permalink
use pomFixtureName for decorator steps
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalets committed Nov 22, 2024
1 parent c73396d commit 1294179
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
31 changes: 25 additions & 6 deletions src/run/StepInvoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ class StepInvoker {
// This call must be exactly here to have correct call stack (before async calls)
const location = getLocationInFile(this.bddContext.testInfo.file);

const parameters = await this.getStepParameters(
const stepParameters = await this.getStepParameters(
matchedDefinition,
stepText,
argument || undefined,
);

const fixturesArg = Object.assign({}, stepFixtures, getBddAutoInjectFixtures(this.bddContext));

// this.bddContext.bddAnnotation?.registerStep(matchedDefinition, location);
const fixturesArg = this.getStepFixtures(stepFixtures || {});

await runStepWithLocation(this.bddContext.test, stepTextWithKeyword, location, () => {
// Although pw-style does not expect usage of world / this in steps,
Expand All @@ -62,7 +60,7 @@ class StepInvoker {
return matchedDefinition.definition.fn.call(
this.bddContext.world,
fixturesArg,
...parameters,
...stepParameters,
);
});
}
Expand Down Expand Up @@ -112,13 +110,34 @@ class StepInvoker {
return parameters;
}

private getStepFixtures(providedFixtures: Record<string, unknown>) {
const { pomFixtureName } = this.getBddStepData();
if (pomFixtureName) {
// for decorator steps keep only one fixture - POM instance.
const pomFixture = providedFixtures[pomFixtureName];
if (!pomFixture) throw new Error(`POM fixture not provided: ${pomFixtureName}`);
providedFixtures = { [pomFixtureName]: pomFixture };
}

return Object.assign({}, providedFixtures, getBddAutoInjectFixtures(this.bddContext));
}

private getStepTextWithKeyword(stepText: string) {
const { keywordOrig } = this.getBddStepData();
return getStepTextWithKeyword(keywordOrig, stepText);
}

private getBddStepData() {
const { stepIndex, bddTestData } = this.bddContext;
return bddTestData.steps[stepIndex];
const bddStepData = bddTestData.steps[stepIndex];
if (!bddStepData) {
throw new Error(
[
`bddStepData not found for step index: ${stepIndex}.`,
`Steps: ${JSON.stringify(bddTestData.steps)}`,
].join(' '),
);
}
return bddStepData;
}
}
4 changes: 2 additions & 2 deletions src/steps/decorators/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ function registerDecoratorStep(stepOptions: StepDefinitionOptions) {
registerStepDefinition({
...stepOptions,
fn: (fixturesArg: Record<string, unknown>, ...args: unknown[]) => {
const fixture = getFirstNonAutoInjectFixture(fixturesArg, stepOptions);
return fn.call(fixture, ...args);
const pomFixture = getFirstNonAutoInjectFixture(fixturesArg, stepOptions);
return fn.call(pomFixture, ...args);
},
});
}
Expand Down
12 changes: 12 additions & 0 deletions test/decorators/features/guess-bg-fixture-by-steps.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Feature: guess bg fixture by steps

Background:
Given BasePage: step

Scenario: scenario 1
Given TodoPage: step
Then used fixtures "TodoPage,TodoPage"

Scenario: scenario 2
Given TodoPage2: step
Then used fixtures "TodoPage2,TodoPage2"

0 comments on commit 1294179

Please sign in to comment.