Skip to content

Commit 6209d14

Browse files
authored
chore: misc test fixes (#2888)
1 parent c3ac037 commit 6209d14

File tree

11 files changed

+125
-34
lines changed

11 files changed

+125
-34
lines changed

src/rpc/channels.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ export interface Channel extends EventEmitter {
2323
}
2424

2525

26+
export interface PlaywrightChannel extends Channel {
27+
}
28+
export type PlaywrightInitializer = {
29+
chromium: BrowserTypeChannel,
30+
firefox: BrowserTypeChannel,
31+
webkit: BrowserTypeChannel,
32+
deviceDescriptors: types.Devices,
33+
};
34+
35+
2636
export interface BrowserTypeChannel extends Channel {
2737
connect(params: types.ConnectOptions): Promise<BrowserChannel>;
2838
launch(params: types.LaunchOptions): Promise<BrowserChannel>;

src/rpc/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import { Transport } from './transport';
2626
connection.onmessage = message => transport.send(message);
2727
transport.onmessage = message => connection.dispatch(message);
2828

29-
const chromium = await connection.waitForObjectWithKnownName('chromium');
30-
const browser = await chromium.launch({ headless: false });
29+
const playwright = await connection.waitForObjectWithKnownName('playwright');
30+
const browser = await playwright.chromium.launch({ headless: false });
3131
const page = await browser.newPage();
3232
await page.goto('https://example.com');
3333
})();

src/rpc/client/browserType.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ import { ConnectionScope } from './connection';
2323
import { BrowserServer } from './browserServer';
2424

2525
export class BrowserType extends ChannelOwner<BrowserTypeChannel, BrowserTypeInitializer> {
26+
27+
static from(browserTyep: BrowserTypeChannel): BrowserType {
28+
return (browserTyep as any)._object;
29+
}
30+
2631
constructor(scope: ConnectionScope, guid: string, initializer: BrowserTypeInitializer) {
2732
super(scope, guid, initializer);
2833
}

src/rpc/client/connection.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { Download } from './download';
3131
import { parseError } from '../serializers';
3232
import { BrowserServer } from './browserServer';
3333
import { CDPSession } from './cdpSession';
34+
import { Playwright } from './playwright';
3435

3536
export class Connection {
3637
readonly _objects = new Map<string, ChannelOwner<any, any>>();
@@ -221,6 +222,9 @@ export class ConnectionScope {
221222
case 'page':
222223
result = new Page(this, guid, initializer);
223224
break;
225+
case 'playwright':
226+
result = new Playwright(this, guid, initializer);
227+
break;
224228
case 'request':
225229
result = new Request(this, guid, initializer);
226230
break;

src/rpc/client/playwright.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { PlaywrightChannel, PlaywrightInitializer } from '../channels';
18+
import * as types from '../../types';
19+
import { BrowserType } from './browserType';
20+
import { ChannelOwner } from './channelOwner';
21+
import { ConnectionScope } from './connection';
22+
23+
export class Playwright extends ChannelOwner<PlaywrightChannel, PlaywrightInitializer> {
24+
chromium: BrowserType;
25+
firefox: BrowserType;
26+
webkit: BrowserType;
27+
devices: types.Devices;
28+
29+
constructor(scope: ConnectionScope, guid: string, initializer: PlaywrightInitializer) {
30+
super(scope, guid, initializer);
31+
this.chromium = BrowserType.from(initializer.chromium);
32+
this.firefox = BrowserType.from(initializer.firefox);
33+
this.webkit = BrowserType.from(initializer.webkit);
34+
this.devices = initializer.deviceDescriptors;
35+
}
36+
}

src/rpc/server.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
import { Transport } from './transport';
1818
import { DispatcherConnection } from './server/dispatcher';
1919
import { Playwright } from '../server/playwright';
20-
import { BrowserTypeDispatcher } from './server/browserTypeDispatcher';
20+
import { PlaywrightDispatcher } from './server/playwrightDispatcher';
2121

2222
const dispatcherConnection = new DispatcherConnection();
2323
const transport = new Transport(process.stdout, process.stdin);
2424
transport.onmessage = message => dispatcherConnection.dispatch(message);
2525
dispatcherConnection.onmessage = message => transport.send(message);
2626

2727
const playwright = new Playwright(__dirname, require('../../browsers.json')['browsers']);
28-
new BrowserTypeDispatcher(dispatcherConnection.rootScope(), playwright.chromium!);
29-
new BrowserTypeDispatcher(dispatcherConnection.rootScope(), playwright.firefox!);
30-
new BrowserTypeDispatcher(dispatcherConnection.rootScope(), playwright.webkit!);
28+
new PlaywrightDispatcher(dispatcherConnection.rootScope(), playwright);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the 'License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { Playwright } from '../../server/playwright';
18+
import { PlaywrightChannel, PlaywrightInitializer } from '../channels';
19+
import { BrowserTypeDispatcher } from './browserTypeDispatcher';
20+
import { Dispatcher, DispatcherScope } from './dispatcher';
21+
22+
export class PlaywrightDispatcher extends Dispatcher<Playwright, PlaywrightInitializer> implements PlaywrightChannel {
23+
constructor(scope: DispatcherScope, playwright: Playwright) {
24+
super(scope, playwright, 'playwright', {
25+
chromium: new BrowserTypeDispatcher(scope, playwright.chromium!),
26+
firefox: new BrowserTypeDispatcher(scope, playwright.firefox!),
27+
webkit: new BrowserTypeDispatcher(scope, playwright.webkit!),
28+
deviceDescriptors: playwright.devices
29+
}, false, 'playwright');
30+
}
31+
}

test/channels.spec.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ describe.skip(!CHANNEL)('Channels', function() {
2424

2525
it('should scope context handles', async({browser, server}) => {
2626
const GOLDEN_PRECONDITION = {
27-
objects: [ 'chromium', 'browser' ],
27+
objects: [ 'chromium', 'firefox', 'webkit', 'playwright', 'browser' ],
2828
scopes: [
29-
{ _guid: '', objects: [ 'chromium', 'browser' ] },
29+
{ _guid: '', objects: [ 'chromium', 'firefox', 'webkit', 'playwright', 'browser' ] },
3030
{ _guid: 'browser', objects: [] }
3131
]
3232
};
@@ -36,9 +36,9 @@ describe.skip(!CHANNEL)('Channels', function() {
3636
const page = await context.newPage();
3737
await page.goto(server.EMPTY_PAGE);
3838
await expectScopeState(browser, {
39-
objects: [ 'chromium', 'browser', 'context', 'frame', 'page', 'request', 'response' ],
39+
objects: [ 'chromium', 'firefox', 'webkit', 'playwright', 'browser', 'context', 'frame', 'page', 'request', 'response' ],
4040
scopes: [
41-
{ _guid: '', objects: [ 'chromium', 'browser' ] },
41+
{ _guid: '', objects: [ 'chromium', 'firefox', 'webkit', 'playwright', 'browser' ] },
4242
{ _guid: 'browser', objects: ['context'] },
4343
{ _guid: 'context', objects: ['frame', 'page', 'request', 'response'] }
4444
]
@@ -50,19 +50,19 @@ describe.skip(!CHANNEL)('Channels', function() {
5050

5151
it('should scope CDPSession handles', async({browserType, browser, server}) => {
5252
const GOLDEN_PRECONDITION = {
53-
objects: [ 'chromium', 'browser' ],
53+
objects: [ 'chromium', 'firefox', 'webkit', 'playwright', 'browser' ],
5454
scopes: [
55-
{ _guid: '', objects: [ 'chromium', 'browser' ] },
55+
{ _guid: '', objects: [ 'chromium', 'firefox', 'webkit', 'playwright', 'browser' ] },
5656
{ _guid: 'browser', objects: [] }
5757
]
5858
};
5959
await expectScopeState(browserType, GOLDEN_PRECONDITION);
6060

6161
const session = await browser.newBrowserCDPSession();
6262
await expectScopeState(browserType, {
63-
objects: [ 'chromium', 'browser', 'cdpSession' ],
63+
objects: [ 'chromium', 'firefox', 'webkit', 'playwright', 'browser', 'cdpSession' ],
6464
scopes: [
65-
{ _guid: '', objects: [ 'chromium', 'browser' ] },
65+
{ _guid: '', objects: [ 'chromium', 'firefox', 'webkit', 'playwright', 'browser' ] },
6666
{ _guid: 'browser', objects: ['cdpSession'] },
6767
{ _guid: 'cdpSession', objects: [] },
6868
]
@@ -74,9 +74,9 @@ describe.skip(!CHANNEL)('Channels', function() {
7474

7575
it('should scope browser handles', async({browserType, defaultBrowserOptions}) => {
7676
const GOLDEN_PRECONDITION = {
77-
objects: [ 'chromium', 'browser' ],
77+
objects: [ 'chromium', 'firefox', 'webkit', 'playwright', 'browser' ],
7878
scopes: [
79-
{ _guid: '', objects: [ 'chromium', 'browser' ] },
79+
{ _guid: '', objects: [ 'chromium', 'firefox', 'webkit', 'playwright', 'browser' ] },
8080
{ _guid: 'browser', objects: [] }
8181
]
8282
};
@@ -85,9 +85,9 @@ describe.skip(!CHANNEL)('Channels', function() {
8585
const browser = await browserType.launch(defaultBrowserOptions);
8686
await browser.newContext();
8787
await expectScopeState(browserType, {
88-
objects: [ 'chromium', 'browser', 'browser', 'context' ],
88+
objects: [ 'chromium', 'firefox', 'webkit', 'playwright', 'browser', 'browser', 'context' ],
8989
scopes: [
90-
{ _guid: '', objects: [ 'chromium', 'browser', 'browser' ] },
90+
{ _guid: '', objects: [ 'chromium', 'firefox', 'webkit', 'playwright', 'browser', 'browser' ] },
9191
{ _guid: 'browser', objects: [] },
9292
{ _guid: 'browser', objects: ['context'] },
9393
{ _guid: 'context', objects: [] },

test/environments.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const rm = require('rimraf').sync;
2222
const {TestServer} = require('../utils/testserver/');
2323
const { DispatcherConnection } = require('../lib/rpc/server/dispatcher');
2424
const { Connection } = require('../lib/rpc/client/connection');
25-
const { BrowserTypeDispatcher } = require('../lib/rpc/server/browserTypeDispatcher');
25+
const { PlaywrightDispatcher } = require('../lib/rpc/server/playwrightDispatcher');
2626

2727
class ServerEnvironment {
2828
async beforeAll(state) {
@@ -159,18 +159,10 @@ class PlaywrightEnvironment {
159159
}
160160

161161
name() { return 'Playwright'; };
162-
beforeAll(state) { state.playwright = this._playwright; }
163-
afterAll(state) { delete state.playwright; }
164-
}
165-
166-
class BrowserTypeEnvironment {
167-
constructor(browserType) {
168-
this._browserType = browserType;
169-
}
170162

171163
async beforeAll(state) {
172164
// Channel substitute
173-
let overridenBrowserType = this._browserType;
165+
this.overriddenPlaywright = this._playwright;
174166
if (process.env.PWCHANNEL) {
175167
const dispatcherConnection = new DispatcherConnection();
176168
const connection = new Connection();
@@ -182,10 +174,24 @@ class BrowserTypeEnvironment {
182174
await new Promise(f => setImmediate(f));
183175
return result;
184176
};
185-
new BrowserTypeDispatcher(dispatcherConnection.rootScope(), this._browserType);
186-
overridenBrowserType = await connection.waitForObjectWithKnownName(this._browserType.name());
177+
new PlaywrightDispatcher(dispatcherConnection.rootScope(), this._playwright);
178+
this.overriddenPlaywright = await connection.waitForObjectWithKnownName('playwright');
187179
}
188-
state.browserType = overridenBrowserType;
180+
state.playwright = this.overriddenPlaywright;
181+
}
182+
183+
async afterAll(state) {
184+
delete state.playwright;
185+
}
186+
}
187+
188+
class BrowserTypeEnvironment {
189+
constructor(browserName) {
190+
this._browserName = browserName;
191+
}
192+
193+
async beforeAll(state) {
194+
state.browserType = state.playwright[this._browserName];
189195
}
190196

191197
async afterAll(state) {

test/jest-runner.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function makeTestRunnerInfo() {
158158
const browserInfo = browserNames.map(browserName => {
159159
const browserType = playwright[browserName];
160160

161-
const browserTypeEnvironment = new BrowserTypeEnvironment(browserType);
161+
const browserTypeEnvironment = new BrowserTypeEnvironment(browserName);
162162

163163
// TODO: maybe launch options per browser?
164164
const launchOptions = {

0 commit comments

Comments
 (0)