Skip to content

Commit

Permalink
fix: access to fixtures from @test method (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianSedzik committed Jul 12, 2023
1 parent 8a72868 commit 136e53b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
node-version: '18.x'
cache: 'npm'
- run: npm ci
- run: npx playwright install
- run: npm run build
- run: npm run lint
- run: npm test
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { suite, test } from 'playwright-decorators';
@suite() // <-- Decorate class with @suite
class MyTestSuite {
@test() // <-- Decorate test method with @test
async myTest() {
async myTest({ page }) {
// ...
}
}
Expand Down
21 changes: 6 additions & 15 deletions lib/test.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,12 @@ class TestDecorator implements TestDecoratorOptions {
Object.assign(this, options);
}

private wrapTest(testCode: () => Promise<any>, wrapperCode: (args: (() => Promise<any>)) => Promise<any>) {
return new Proxy(testCode, {
apply: (target: any, thisArg: any, argArray: any[]) =>
wrapperCode(() => target.apply(thisArg, argArray))
})
}

private runTest(userTestCode: () => Promise<any>) {
return userTestCode();
}

run(executionContext: any) {

Check warning on line 19 in lib/test.decorator.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const testCallback = this.wrapTest(this.testMethod, this.runTest).bind(executionContext);

playwright(this.name, testCallback);
// playwright function do not accept ...rest arguments, so we need to request all of them and pass to the testMethod manually
playwright(this.name, ({playwright, context, browserName, browser, contextOptions, connectOptions, page, testIdAttribute, launchOptions, defaultBrowserType, baseURL, channel, acceptDownloads, bypassCSP, deviceScaleFactor, extraHTTPHeaders, httpCredentials, ignoreHTTPSErrors, geolocation, hasTouch, headless, isMobile, javaScriptEnabled, locale, navigationTimeout, actionTimeout, offline, permissions, proxy, request, serviceWorkers, screenshot, trace, storageState, timezoneId, video, viewport, userAgent, colorScheme
}, ...args) => {
return this.testMethod.call(executionContext, {playwright, context, browserName, browser, contextOptions, connectOptions, page, testIdAttribute, launchOptions, defaultBrowserType, baseURL, channel, acceptDownloads, bypassCSP, deviceScaleFactor, extraHTTPHeaders, httpCredentials, ignoreHTTPSErrors, geolocation, hasTouch, headless, isMobile, javaScriptEnabled, locale, navigationTimeout, actionTimeout, offline, permissions, proxy, request, serviceWorkers, screenshot, trace, storageState, timezoneId, video, viewport, userAgent, colorScheme}, ...args);
})
}
}

Expand All @@ -44,7 +35,7 @@ export const test = (options: TestDecoratorOptions = {}) => function(originalMet
const testDecorator = new TestDecorator(originalMethod, options);

Object.assign(originalMethod, { testDecorator });

(context as ClassMemberDecoratorContext ).addInitializer(function () {
testDecorator.run(this);
});
Expand Down
14 changes: 12 additions & 2 deletions tests/test.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ playwright.describe('@test decorator', () => {
called.push('testThisContext');
expect(this instanceof ExampleSuite).toBeTruthy();
}

@test()
testShouldHaveAccessToPage({ page }) {
called.push('testShouldHaveAccessToPage');
expect(page).not.toBeUndefined();
}
}

playwright('Methods with @test should be run', () => {
expect(called).toEqual(expect.arrayContaining(['testMethod', 'testMethod2', 'testThisContext']));
expect(called).toEqual(['testMethod', 'testMethod2', 'testThisContext', 'testShouldHaveAccessToPage']);
});

playwright('Methods without @test should not be run', () => {
expect(called).toEqual(expect.not.arrayContaining(['notTestMethod']));
});

playwright.afterAll(() => {
expect(called.length).toEqual(4);
})
})

0 comments on commit 136e53b

Please sign in to comment.