Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: access to fixtures from @test method #10

Merged
merged 6 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
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);
})
})