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

feat(cli): share console api between cli and debug mode #4807

Merged
merged 1 commit into from
Dec 23, 2020
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
2 changes: 2 additions & 0 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import * as program from 'commander';
import * as os from 'os';
import * as fs from 'fs';
import { installBrowsersWithProgressBar } from '../install/installer';
import * as consoleApiSource from '../generated/consoleApiSource';

// TODO: we can import from '../..' instead, but that requires generating types
// before build, and currently type generator depends on the build.
Expand Down Expand Up @@ -284,6 +285,7 @@ async function openPage(context: BrowserContext, url: string | undefined): Promi

async function open(options: Options, url: string | undefined) {
const { context } = await launchContext(options, false);
context._extendInjectedScript(consoleApiSource.source);
await openPage(context, url);
if (process.env.PWCLI_EXIT_FOR_TEST)
await Promise.all(context.pages().map(p => p.close()));
Expand Down
5 changes: 5 additions & 0 deletions src/client/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Waiter } from './waiter';
import { URLMatch, Headers, WaitForEventOptions, BrowserContextOptions, StorageState } from './types';
import { isUnderTest, headersObjectToArray, mkdirIfNeeded } from '../utils/utils';
import { isSafeCloseError } from '../utils/errors';
import { serializeArgument } from './jsHandle';

const fsWriteFileAsync = util.promisify(fs.writeFile.bind(fs));
const fsReadFileAsync = util.promisify(fs.readFile.bind(fs));
Expand Down Expand Up @@ -253,6 +254,10 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel,
throw e;
}
}

async _extendInjectedScript<Arg>(source: string, arg?: Arg) {
await this._channel.extendInjectedScript({ source, arg: serializeArgument(arg) });
}
}

export async function prepareBrowserContextOptions(options: BrowserContextOptions): Promise<channels.BrowserNewContextOptions> {
Expand Down
1 change: 1 addition & 0 deletions src/client/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ export class Frame extends ChannelOwner<channels.FrameChannel, channels.FrameIni
});
}

// TODO: remove once playwright-cli does not use this one anymore.
async _extendInjectedScript<Arg>(source: string, arg?: Arg): Promise<JSHandle> {
const result = await this._channel.extendInjectedScript({ source, arg: serializeArgument(arg) });
return JSHandle.from(result.handle);
Expand Down
23 changes: 2 additions & 21 deletions src/debug/debugController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
*/

import { BrowserContext, ContextListener, contextListeners } from '../server/browserContext';
import * as frames from '../server/frames';
import { Page } from '../server/page';
import { isDebugMode } from '../utils/utils';
import * as debugScriptSource from '../generated/debugScriptSource';
import * as consoleApiSource from '../generated/consoleApiSource';

export function installDebugController() {
contextListeners.add(new DebugController());
Expand All @@ -27,25 +25,8 @@ export function installDebugController() {
class DebugController implements ContextListener {
async onContextCreated(context: BrowserContext): Promise<void> {
if (isDebugMode())
installDebugControllerInContext(context);
context.extendInjectedScript(consoleApiSource.source);
}
async onContextWillDestroy(context: BrowserContext): Promise<void> {}
async onContextDidDestroy(context: BrowserContext): Promise<void> {}
}

async function ensureInstalledInFrame(frame: frames.Frame) {
try {
await frame.extendInjectedScript(debugScriptSource.source);
} catch (e) {
}
}

async function installInPage(page: Page) {
page.on(Page.Events.FrameNavigated, ensureInstalledInFrame);
await Promise.all(page.frames().map(ensureInstalledInFrame));
}

export async function installDebugControllerInContext(context: BrowserContext) {
context.on(BrowserContext.Events.Page, installInPage);
await Promise.all(context.pages().map(installInPage));
}
4 changes: 4 additions & 0 deletions src/debug/injected/consoleApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export class ConsoleAPI {

constructor(injectedScript: InjectedScript) {
this._injectedScript = injectedScript;
if ((window as any).playwright)
return;
(window as any).playwright = {
$: (selector: string) => this._querySelector(selector),
$$: (selector: string) => this._querySelectorAll(selector),
Expand Down Expand Up @@ -58,3 +60,5 @@ export class ConsoleAPI {
return generateSelector(this._injectedScript, element).selector;
}
}

export default ConsoleAPI;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const path = require('path');
const InlineSource = require('../../server/injected/webpack-inline-source-plugin');

module.exports = {
entry: path.join(__dirname, 'debugScript.ts'),
entry: path.join(__dirname, 'consoleApi.ts'),
devtool: 'source-map',
module: {
rules: [
Expand All @@ -37,10 +37,10 @@ module.exports = {
},
output: {
libraryTarget: 'var',
filename: 'debugScriptSource.js',
filename: 'consoleApiSource.js',
path: path.resolve(__dirname, '../../../lib/server/injected/packed')
},
plugins: [
new InlineSource(path.join(__dirname, '..', '..', 'generated', 'debugScriptSource.ts')),
new InlineSource(path.join(__dirname, '..', '..', 'generated', 'consoleApiSource.ts')),
]
};
27 changes: 0 additions & 27 deletions src/debug/injected/debugScript.ts

This file was deleted.

5 changes: 5 additions & 0 deletions src/dispatchers/browserContextDispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as channels from '../protocol/channels';
import { RouteDispatcher, RequestDispatcher } from './networkDispatchers';
import { CRBrowserContext } from '../server/chromium/crBrowser';
import { CDPSessionDispatcher } from './cdpSessionDispatcher';
import { parseArgument } from './jsHandleDispatcher';

export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channels.BrowserContextInitializer> implements channels.BrowserContextChannel {
private _context: BrowserContext;
Expand Down Expand Up @@ -125,6 +126,10 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
await this._context.close();
}

async extendInjectedScript(params: channels.BrowserContextExtendInjectedScriptParams): Promise<void> {
await this._context.extendInjectedScript(params.source, parseArgument(params.arg));
}

async crNewCDPSession(params: channels.BrowserContextCrNewCDPSessionParams): Promise<channels.BrowserContextCrNewCDPSessionResult> {
if (this._object._browser._options.name !== 'chromium')
throw new Error(`CDP session is only available in Chromium`);
Expand Down
9 changes: 9 additions & 0 deletions src/protocol/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ export interface BrowserContextChannel extends Channel {
setNetworkInterceptionEnabled(params: BrowserContextSetNetworkInterceptionEnabledParams, metadata?: Metadata): Promise<BrowserContextSetNetworkInterceptionEnabledResult>;
setOffline(params: BrowserContextSetOfflineParams, metadata?: Metadata): Promise<BrowserContextSetOfflineResult>;
storageState(params?: BrowserContextStorageStateParams, metadata?: Metadata): Promise<BrowserContextStorageStateResult>;
extendInjectedScript(params: BrowserContextExtendInjectedScriptParams, metadata?: Metadata): Promise<BrowserContextExtendInjectedScriptResult>;
crNewCDPSession(params: BrowserContextCrNewCDPSessionParams, metadata?: Metadata): Promise<BrowserContextCrNewCDPSessionResult>;
}
export type BrowserContextBindingCallEvent = {
Expand Down Expand Up @@ -697,6 +698,14 @@ export type BrowserContextStorageStateResult = {
cookies: NetworkCookie[],
origins: OriginStorage[],
};
export type BrowserContextExtendInjectedScriptParams = {
source: string,
arg: SerializedArgument,
};
export type BrowserContextExtendInjectedScriptOptions = {

};
export type BrowserContextExtendInjectedScriptResult = void;
export type BrowserContextCrNewCDPSessionParams = {
page: PageChannel,
};
Expand Down
6 changes: 6 additions & 0 deletions src/protocol/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,12 @@ BrowserContext:
type: array
items: OriginStorage

extendInjectedScript:
experimental: True
parameters:
source: string
arg: SerializedArgument

crNewCDPSession:
parameters:
page: Page
Expand Down
4 changes: 4 additions & 0 deletions src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ export function createScheme(tChannel: (name: string) => Validator): Scheme {
offline: tBoolean,
});
scheme.BrowserContextStorageStateParams = tOptional(tObject({}));
scheme.BrowserContextExtendInjectedScriptParams = tObject({
source: tString,
arg: tType('SerializedArgument'),
});
scheme.BrowserContextCrNewCDPSessionParams = tObject({
page: tChannel('Page'),
});
Expand Down
10 changes: 10 additions & 0 deletions src/server/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,16 @@ export abstract class BrowserContext extends EventEmitter {
await page.close();
}
}

async extendInjectedScript(source: string, arg?: any) {
const installInFrame = (frame: frames.Frame) => frame.extendInjectedScript(source, arg).catch(e => {});
const installInPage = (page: Page) => {
page.on(Page.Events.FrameNavigated, installInFrame);
return Promise.all(page.frames().map(installInFrame));
};
this.on(BrowserContext.Events.Page, installInPage);
return Promise.all(this.pages().map(installInPage));
}
}

export function assertBrowserContextIsNotOwned(context: BrowserContext) {
Expand Down
6 changes: 3 additions & 3 deletions test/selector-generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import { folio } from './fixtures';
import * as path from 'path';
import type { Page, Frame } from '..';

const { installDebugControllerInContext } = require(path.join(__dirname, '..', 'lib', 'debug', 'debugController'));
const { source } = require(path.join(__dirname, '..', 'lib', 'generated', 'consoleApiSource'));

const fixtures = folio.extend();
fixtures.context.override(async ({ context, toImpl }, run) => {
await installDebugControllerInContext(toImpl(context));
fixtures.context.override(async ({ context }, run) => {
await (context as any)._extendInjectedScript(source);
await run(context);
});
const { describe, it, expect } = fixtures.build();
Expand Down
2 changes: 1 addition & 1 deletion utils/check_deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ DEPS['src/remote/'] = ['src/client/', 'src/debug/', 'src/dispatchers/', 'src/ser
DEPS['src/service.ts'] = ['src/remote/'];

// CLI should only use client-side features.
DEPS['src/cli/'] = ['src/client/**', 'src/install/**'];
DEPS['src/cli/'] = ['src/client/**', 'src/install/**', 'src/generated/'];

checkDeps().catch(e => {
console.error(e && e.stack ? e.stack : e);
Expand Down
2 changes: 1 addition & 1 deletion utils/runWebpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const path = require('path');
const files = [
path.join('src', 'server', 'injected', 'injectedScript.webpack.config.js'),
path.join('src', 'server', 'injected', 'utilityScript.webpack.config.js'),
path.join('src', 'debug', 'injected', 'debugScript.webpack.config.js'),
path.join('src', 'debug', 'injected', 'consoleApi.webpack.config.js'),
];

function runOne(runner, file) {
Expand Down