Skip to content

Commit

Permalink
docs: custom fixtures in cucumber style steps
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalets committed Oct 23, 2023
1 parent 2595bb1 commit f186f5f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## dev
* support custom fixtures in cucumber-style steps

## 5.4.0
* i18n: Generate scenario outlines correctly [#60](https://github.com/vitalets/playwright-bdd/issues/60).
* Check for duplicate fixture names [#52](https://github.com/vitalets/playwright-bdd/issues/52)
Expand Down
30 changes: 30 additions & 0 deletions docs/writing-steps.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,33 @@ setWorldConstructor(CustomWorld);
> Consider asynchronous setup and teardown of `BddWorld` using `init()` / `destroy()` methods.

See [full example of Cucumber-style](https://github.com/vitalets/playwright-bdd/tree/main/examples/cucumber-style).

### Custom fixtures
Along with built-in fixtures you can use any custom fixture in cucumber-style steps.
To get fixture call `this.useFixture(fixtureName)` method inside step body.

For example:
```js
When('I open todo page', async function () {
const todoPage = this.useFixture('todoPage');
await todoPage.open();
});
```

For **TypeScript** you can pass `typeof test` as a second generic parameter to `BddWorld`:

```ts
type MyWorld = BddWorld<object, typeof test>;
When<MyWorld>('I open todo page', async function () {
const todoPage = this.useFixture('todoPage');
await todoPage.open();
});
```

> Please note that **you can only pass static strings** to `this.useFixture()`. Function body is analyzed to find used fixtures. Below **will not work**:
```ts
// will not work!
const fixtureName = 'todoPage';
const todoPage = this.useFixture(fixtureName);
```

0 comments on commit f186f5f

Please sign in to comment.