Bug: this keyword scope is not working with version greater than 6.6.0
magarwal19 opened this issue · 6 comments
Given
I create a step with say
When("I login to application",async function ({ context, request}) {
this.loggedInUser = "test123";
}
When
I try to use the value stored with this keyword in last step to new step, then it is showing unidentified
e.g
Then("I validate logged in user",async function ({ context, request}) {
console.log(this.loggedInUser)
}
Then
The value stored in first step is not accessible in next step. It is working on version 6.6.0
But I expect
we should be able to access value in next steps
Environment
platform: darwin
node: v16.20.0
playwright-bdd: v6.6.0
@playwright/test: v1.46.0
@cucumber/cucumber: v10.8.0
Playwright config file: playwright.config.js
Hi @magarwal19 ! What steps style do you use?
From the provided code it's kind of mixed.
- If you use
{ context, request }
as a first argument of the step function, it is Playwright-style, so there is nothis
as a world. I've checked - you are right, it worked before, but it was never documented. - if you use
this
in the step function, it is Cucumber-style -> arguments should contain only step parameters (empty in your example):When("I login to application", async function () { this.loggedInUser = "test123"; }
Hi @vitalets
mostly we have playwright style steps where either we use context/browser/page/request
yes it was working before as all my tests are using this functionality and when updating to latest version it is breaking
Got it. Then I'd suggest to move to either pure Playwright-style or pure Cucumber-style.
For pure Playwright-style you can define world
fixture that holds loggedInUser
:
type MyWorld = {
loggedInUser: string;
};
export const test = base.extend<{ world: MyWorld }>({
world: ({}, use) => use({ loggedInUser: '' }),
});
Then in steps use world
instead of this
:
When("I login to application", async ({ context, request, world }) => {
world.loggedInUser = "test123";
});
Then("I validate logged in user", async ({ context, request, world }) => {
console.log(world.loggedInUser)
}):
For pure Cucumber-style you should also define world
fixture, but it holds everything needed in steps: loggedInUser
, request
, context
other fixtures:
type MyWorld = {
loggedInUser: string;
context: BrowserContext;
request: ApiRequest;
};
export const test = base.extend<{ world: MyWorld }>({
world: ({ request, context }, use) => use({ request, context, loggedInUser: '' }),
});
export const { Given, When, Then } = createBdd(test, { worldFixture: 'world' });
Then in steps you can access everything from this
:
When("I login to application", async function () {
// this.context.newPage();
this.loggedInUser = "test123";
});
Then("I validate logged in user", async function () {
console.log(this.loggedInUser);
}):
so we are completely on playwright based steps..
but having world fixture would increase work for us as it is not a single variable we store..
i have more than 200 tests in which we have different variables shared among steps...
if we need to add this, then i will need to update all my steps to include world fixture and find all the variables which are shared and add to world fixture...
can we not fix it as it was working till 6.6.0 ?
@magarwal19 could you try with just released v7.2.2?
I've added empty world {}
to playwright-style steps.
Thank you for fixing this quickly!!
it is working now :)