feat(driver): report backend state (#16477)

This commit is contained in:
Pavel Feldman 2022-08-11 13:42:16 -07:00 committed by GitHub
parent 51076d55ad
commit fce45210c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 3 deletions

View File

@ -90,6 +90,38 @@ class ProtocolHandler {
constructor(playwright: Playwright) { constructor(playwright: Playwright) {
this._playwright = playwright; this._playwright = playwright;
playwright.instrumentation.addListener({
onPageOpen: () => this._sendSnapshot(),
onPageNavigated: () => this._sendSnapshot(),
onPageClose: () => this._sendSnapshot(),
}, null);
}
private _sendSnapshot() {
const browsers = [];
for (const browser of this._playwright.allBrowsers()) {
const b = {
name: browser.options.name,
guid: browser.guid,
contexts: [] as any[]
};
browsers.push(b);
for (const context of browser.contexts()) {
const c = {
guid: context.guid,
pages: [] as any[]
};
b.contexts.push(c);
for (const page of context.pages()) {
const p = {
guid: page.guid,
url: page.mainFrame().url()
};
c.pages.push(p);
}
}
}
process.send!({ method: 'browsersChanged', params: { browsers } });
} }
async resetForReuse() { async resetForReuse() {

View File

@ -239,7 +239,7 @@ export class FrameManager {
frame._onClearLifecycle(); frame._onClearLifecycle();
const navigationEvent: NavigationEvent = { url, name, newDocument: frame._currentDocument, isPublic: true }; const navigationEvent: NavigationEvent = { url, name, newDocument: frame._currentDocument, isPublic: true };
frame.emit(Frame.Events.InternalNavigation, navigationEvent); this._fireInternalFrameNavigation(frame, navigationEvent);
if (!initial) { if (!initial) {
debugLogger.log('api', ` navigated to "${url}"`); debugLogger.log('api', ` navigated to "${url}"`);
this._page.frameNavigatedToNewDocument(frame); this._page.frameNavigatedToNewDocument(frame);
@ -254,7 +254,7 @@ export class FrameManager {
return; return;
frame._url = url; frame._url = url;
const navigationEvent: NavigationEvent = { url, name: frame._name, isPublic: true }; const navigationEvent: NavigationEvent = { url, name: frame._name, isPublic: true };
frame.emit(Frame.Events.InternalNavigation, navigationEvent); this._fireInternalFrameNavigation(frame, navigationEvent);
debugLogger.log('api', ` navigated to "${url}"`); debugLogger.log('api', ` navigated to "${url}"`);
} }
@ -272,7 +272,7 @@ export class FrameManager {
isPublic: !(documentId && frame._redirectedNavigations.has(documentId)), isPublic: !(documentId && frame._redirectedNavigations.has(documentId)),
}; };
frame.setPendingDocument(undefined); frame.setPendingDocument(undefined);
frame.emit(Frame.Events.InternalNavigation, navigationEvent); this._fireInternalFrameNavigation(frame, navigationEvent);
} }
frameDetached(frameId: string) { frameDetached(frameId: string) {
@ -462,6 +462,12 @@ export class FrameManager {
if (ws) if (ws)
ws.error(errorMessage); ws.error(errorMessage);
} }
private _fireInternalFrameNavigation(frame: Frame, event: NavigationEvent) {
frame.emit(Frame.Events.InternalNavigation, event);
if (event.isPublic && !frame.parentFrame())
frame.instrumentation.onPageNavigated(frame._page, event.url);
}
} }
export class Frame extends SdkObject { export class Frame extends SdkObject {

View File

@ -63,6 +63,7 @@ export interface Instrumentation {
onAfterCall(sdkObject: SdkObject, metadata: CallMetadata): Promise<void>; onAfterCall(sdkObject: SdkObject, metadata: CallMetadata): Promise<void>;
onEvent(sdkObject: SdkObject, metadata: CallMetadata): void; onEvent(sdkObject: SdkObject, metadata: CallMetadata): void;
onPageOpen(page: Page): void; onPageOpen(page: Page): void;
onPageNavigated(page: Page, url: string): void;
onPageClose(page: Page): void; onPageClose(page: Page): void;
onBrowserOpen(browser: Browser): void; onBrowserOpen(browser: Browser): void;
onBrowserClose(browser: Browser): void; onBrowserClose(browser: Browser): void;
@ -75,6 +76,7 @@ export interface InstrumentationListener {
onAfterCall?(sdkObject: SdkObject, metadata: CallMetadata): Promise<void>; onAfterCall?(sdkObject: SdkObject, metadata: CallMetadata): Promise<void>;
onEvent?(sdkObject: SdkObject, metadata: CallMetadata): void; onEvent?(sdkObject: SdkObject, metadata: CallMetadata): void;
onPageOpen?(page: Page): void; onPageOpen?(page: Page): void;
onPageNavigated?(page: Page, url: string): void;
onPageClose?(page: Page): void; onPageClose?(page: Page): void;
onBrowserOpen?(browser: Browser): void; onBrowserOpen?(browser: Browser): void;
onBrowserClose?(browser: Browser): void; onBrowserClose?(browser: Browser): void;