|
15 | 15 | * limitations under the License.
|
16 | 16 | */
|
17 | 17 |
|
18 |
| -import { Page, PageBinding } from './page'; |
19 |
| -import * as network from './network'; |
20 |
| -import * as types from './types'; |
21 | 18 | import { helper } from './helper';
|
| 19 | +import * as network from './network'; |
| 20 | +import { Page, PageBinding } from './page'; |
| 21 | +import * as platform from './platform'; |
22 | 22 | import { TimeoutSettings } from './timeoutSettings';
|
| 23 | +import * as types from './types'; |
| 24 | +import { Events } from './events'; |
23 | 25 |
|
24 | 26 | export type BrowserContextOptions = {
|
25 | 27 | viewport?: types.Viewport | null,
|
@@ -50,15 +52,69 @@ export interface BrowserContext {
|
50 | 52 | setOffline(offline: boolean): Promise<void>;
|
51 | 53 | addInitScript(script: Function | string | { path?: string, content?: string }, ...args: any[]): Promise<void>;
|
52 | 54 | exposeFunction(name: string, playwrightFunction: Function): Promise<void>;
|
| 55 | + waitForEvent(event: string, optionsOrPredicate?: Function | (types.TimeoutOptions & { predicate?: Function })): Promise<any>; |
53 | 56 | close(): Promise<void>;
|
| 57 | +} |
54 | 58 |
|
55 |
| - _existingPages(): Page[]; |
56 |
| - readonly _timeoutSettings: TimeoutSettings; |
| 59 | +export abstract class BrowserContextBase extends platform.EventEmitter implements BrowserContext { |
| 60 | + readonly _timeoutSettings = new TimeoutSettings(); |
| 61 | + readonly _pageBindings = new Map<string, PageBinding>(); |
57 | 62 | readonly _options: BrowserContextOptions;
|
58 |
| - readonly _pageBindings: Map<string, PageBinding>; |
| 63 | + private _closePromise: Promise<Error> | undefined; |
| 64 | + |
| 65 | + constructor(options: BrowserContextOptions) { |
| 66 | + super(); |
| 67 | + this._options = options; |
| 68 | + } |
| 69 | + |
| 70 | + abstract _existingPages(): Page[]; |
| 71 | + |
| 72 | + // BrowserContext methods. |
| 73 | + abstract pages(): Promise<Page[]>; |
| 74 | + abstract newPage(): Promise<Page>; |
| 75 | + abstract cookies(...urls: string[]): Promise<network.NetworkCookie[]>; |
| 76 | + abstract setCookies(cookies: network.SetNetworkCookieParam[]): Promise<void>; |
| 77 | + abstract clearCookies(): Promise<void>; |
| 78 | + abstract setPermissions(origin: string, permissions: string[]): Promise<void>; |
| 79 | + abstract clearPermissions(): Promise<void>; |
| 80 | + abstract setGeolocation(geolocation: types.Geolocation | null): Promise<void>; |
| 81 | + abstract setExtraHTTPHeaders(headers: network.Headers): Promise<void>; |
| 82 | + abstract setOffline(offline: boolean): Promise<void>; |
| 83 | + abstract addInitScript(script: string | Function | { path?: string | undefined; content?: string | undefined; }, ...args: any[]): Promise<void>; |
| 84 | + abstract exposeFunction(name: string, playwrightFunction: Function): Promise<void>; |
| 85 | + abstract close(): Promise<void>; |
| 86 | + |
| 87 | + setDefaultNavigationTimeout(timeout: number) { |
| 88 | + this._timeoutSettings.setDefaultNavigationTimeout(timeout); |
| 89 | + } |
| 90 | + |
| 91 | + setDefaultTimeout(timeout: number) { |
| 92 | + this._timeoutSettings.setDefaultTimeout(timeout); |
| 93 | + } |
| 94 | + |
| 95 | + async waitForEvent(event: string, optionsOrPredicate?: Function | (types.TimeoutOptions & { predicate?: Function })): Promise<any> { |
| 96 | + if (!optionsOrPredicate) |
| 97 | + optionsOrPredicate = {}; |
| 98 | + if (typeof optionsOrPredicate === 'function') |
| 99 | + optionsOrPredicate = { predicate: optionsOrPredicate }; |
| 100 | + const { timeout = this._timeoutSettings.timeout(), predicate = () => true } = optionsOrPredicate; |
| 101 | + |
| 102 | + let abortPromise: Promise<Error>; |
| 103 | + if (event === Events.BrowserContext.Close) { |
| 104 | + abortPromise = new Promise<Error>(() => { }); |
| 105 | + } else { |
| 106 | + if (!this._closePromise) { |
| 107 | + this._closePromise = new Promise(fulfill => { |
| 108 | + this.once(Events.BrowserContext.Close, () => fulfill(new Error('Context closed'))); |
| 109 | + }); |
| 110 | + } |
| 111 | + abortPromise = this._closePromise; |
| 112 | + } |
| 113 | + return helper.waitForEvent(this, event, (...args: any[]) => !!predicate(...args), timeout, abortPromise); |
| 114 | + } |
59 | 115 | }
|
60 | 116 |
|
61 |
| -export function assertBrowserContextIsNotOwned(context: BrowserContext) { |
| 117 | +export function assertBrowserContextIsNotOwned(context: BrowserContextBase) { |
62 | 118 | const pages = context._existingPages();
|
63 | 119 | for (const page of pages) {
|
64 | 120 | if (page._ownedContext)
|
|
0 commit comments