Skip to content

Commit 86210c0

Browse files
added _expect_ to default world (#14)
1 parent cd73d67 commit 86210c0

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
1212

1313
## [0.5.0]
1414
- :rocket: updated gherkin dependency
15+
- :rocket: added _expect_ to default world
1516

1617
## [0.4.0]
1718
- :rocket: added support of _requireModules_ option

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default defineConfig({
3535
Custom test instance can be passed to world constructor as _test_ property.
3636
And then fixtures can be connected with world instance via _init_ property.
3737
```typescript
38-
import { test as base } from '@playwright/test';
38+
import { test as base, expect as baseExpect } from '@playwright/test';
3939
import { SettingsPage } from './settings-page';
4040
import { setWorldConstructor } from '@cucumber/cucumber';
4141
import { PlaywrightWorld } from '@qavajs/playwright-runner-adapter/PlaywrightWorld';
@@ -50,6 +50,12 @@ const customTest = base.extend<MyFixtures>({
5050
},
5151
});
5252

53+
const customExpect = baseExpect.extend({
54+
async customMatcher() {
55+
// implementation
56+
}
57+
});
58+
5359
class ExtendedPlaywrightWorld extends PlaywrightWorld {
5460
settingsPage: SettingsPage;
5561
constructor(options: any) {
@@ -58,6 +64,7 @@ class ExtendedPlaywrightWorld extends PlaywrightWorld {
5864

5965
// set test property with extened one
6066
test = customTest;
67+
expect = customExpect;
6168

6269
// init arrow function connects fixtures with Cucumber world instance
6370
init = ({ settingsPage }) => {

src/PlaywrightWorld.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { APIRequestContext, Browser, BrowserContext, Page, test } from '@playwright/test';
1+
import { APIRequestContext, Browser, BrowserContext, Page, test, expect } from '@playwright/test';
22

33
/**
44
* Cucumber world for playwright adapter
@@ -13,6 +13,7 @@ export class PlaywrightWorld {
1313
attach!: (data: any, details?: { fileName?: string, mediaType: string }) => void;
1414
parameters!: string;
1515
test = test;
16+
expect = expect;
1617

1718
constructor(options: any) {
1819
this.log = options.log;

test/features/feature.feature

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,6 @@ Feature: test feature
4040

4141
Scenario: custom fixture
4242
Given custom fixture
43+
44+
Scenario: custom expect
45+
Given custom expect

test/step_definitions/steps.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Given, When, setWorldConstructor, DataTable } from '@cucumber/cucumber';
2-
import { test as base, expect, Page } from '@playwright/test';
2+
import { test as base, expect as baseExpect, Page, Locator } from '@playwright/test';
33
import { PlaywrightWorld } from '../../src/PlaywrightWorld';
44

55
const fixture = base.extend({
@@ -8,13 +8,26 @@ const fixture = base.extend({
88
}
99
});
1010

11+
const customExpect = baseExpect.extend({
12+
toAlwaysPass(locator: Locator) {
13+
return {
14+
message: () => 'pass',
15+
pass: true,
16+
name: 'toAlwaysPass',
17+
expected: 'foo',
18+
actual: 'foo',
19+
}
20+
}
21+
});
22+
1123
class ExtendedPlaywrightWorld extends PlaywrightWorld {
1224
constructor(options: any) {
1325
super(options);
1426
}
1527

1628
customFixture!: number;
1729
test = fixture;
30+
expect = customExpect;
1831

1932
init = ({ page, customFixture }: { page: Page, customFixture: number }) => {
2033
this.page = page;
@@ -33,11 +46,11 @@ When(/^simple step$/, async function () {
3346
});
3447

3548
When('data table step', async function (dataTable: DataTable) {
36-
expect(dataTable.raw()).toEqual([['1'], ['2']])
49+
this.expect(dataTable.raw()).toEqual([['1'], ['2']])
3750
});
3851

3952
When('multiline step', async function (multiline: string) {
40-
expect(multiline).toEqual('first\nsecond')
53+
this.expect(multiline).toEqual('first\nsecond')
4154
});
4255

4356
When('log', async function () {
@@ -49,5 +62,9 @@ When('attach', async function () {
4962
});
5063

5164
When('custom fixture', async function (this: ExtendedPlaywrightWorld) {
52-
expect(this.customFixture).toEqual(42);
65+
this.expect(this.customFixture).toEqual(42);
66+
});
67+
68+
When('custom expect', async function (this: ExtendedPlaywrightWorld) {
69+
this.expect(this.page.locator('body')).toAlwaysPass();
5370
});

0 commit comments

Comments
 (0)