|
| 1 | +/** |
| 2 | + * Copyright 2017 Google Inc. All rights reserved. |
| 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 {EventEmitter} from '../common/EventEmitter.js'; |
| 18 | +import {Page} from './Page.js'; |
| 19 | +import {Target} from '../common/Target.js'; |
| 20 | +import type {Permission, Browser} from './Browser.js'; |
| 21 | + |
| 22 | +/** |
| 23 | + * BrowserContexts provide a way to operate multiple independent browser |
| 24 | + * sessions. When a browser is launched, it has a single BrowserContext used by |
| 25 | + * default. The method {@link Browser.newPage | Browser.newPage} creates a page |
| 26 | + * in the default browser context. |
| 27 | + * |
| 28 | + * @remarks |
| 29 | + * |
| 30 | + * The Browser class extends from Puppeteer's {@link EventEmitter} class and |
| 31 | + * will emit various events which are documented in the |
| 32 | + * {@link BrowserContextEmittedEvents} enum. |
| 33 | + * |
| 34 | + * If a page opens another page, e.g. with a `window.open` call, the popup will |
| 35 | + * belong to the parent page's browser context. |
| 36 | + * |
| 37 | + * Puppeteer allows creation of "incognito" browser contexts with |
| 38 | + * {@link Browser.createIncognitoBrowserContext | Browser.createIncognitoBrowserContext} |
| 39 | + * method. "Incognito" browser contexts don't write any browsing data to disk. |
| 40 | + * |
| 41 | + * @example |
| 42 | + * |
| 43 | + * ```ts |
| 44 | + * // Create a new incognito browser context |
| 45 | + * const context = await browser.createIncognitoBrowserContext(); |
| 46 | + * // Create a new page inside context. |
| 47 | + * const page = await context.newPage(); |
| 48 | + * // ... do stuff with page ... |
| 49 | + * await page.goto('https://example.com'); |
| 50 | + * // Dispose context once it's no longer needed. |
| 51 | + * await context.close(); |
| 52 | + * ``` |
| 53 | + * |
| 54 | + * @public |
| 55 | + */ |
| 56 | + |
| 57 | +export class BrowserContext extends EventEmitter { |
| 58 | + /** |
| 59 | + * @internal |
| 60 | + */ |
| 61 | + constructor() { |
| 62 | + super(); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * An array of all active targets inside the browser context. |
| 67 | + */ |
| 68 | + targets(): Target[] { |
| 69 | + throw new Error('Not implemented'); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * This searches for a target in this specific browser context. |
| 74 | + * |
| 75 | + * @example |
| 76 | + * An example of finding a target for a page opened via `window.open`: |
| 77 | + * |
| 78 | + * ```ts |
| 79 | + * await page.evaluate(() => window.open('https://www.example.com/')); |
| 80 | + * const newWindowTarget = await browserContext.waitForTarget( |
| 81 | + * target => target.url() === 'https://www.example.com/' |
| 82 | + * ); |
| 83 | + * ``` |
| 84 | + * |
| 85 | + * @param predicate - A function to be run for every target |
| 86 | + * @param options - An object of options. Accepts a timout, |
| 87 | + * which is the maximum wait time in milliseconds. |
| 88 | + * Pass `0` to disable the timeout. Defaults to 30 seconds. |
| 89 | + * @returns Promise which resolves to the first target found |
| 90 | + * that matches the `predicate` function. |
| 91 | + */ |
| 92 | + waitForTarget( |
| 93 | + predicate: (x: Target) => boolean | Promise<boolean>, |
| 94 | + options?: {timeout?: number} |
| 95 | + ): Promise<Target>; |
| 96 | + waitForTarget(): Promise<Target> { |
| 97 | + throw new Error('Not implemented'); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * An array of all pages inside the browser context. |
| 102 | + * |
| 103 | + * @returns Promise which resolves to an array of all open pages. |
| 104 | + * Non visible pages, such as `"background_page"`, will not be listed here. |
| 105 | + * You can find them using {@link Target.page | the target page}. |
| 106 | + */ |
| 107 | + pages(): Promise<Page[]> { |
| 108 | + throw new Error('Not implemented'); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns whether BrowserContext is incognito. |
| 113 | + * The default browser context is the only non-incognito browser context. |
| 114 | + * |
| 115 | + * @remarks |
| 116 | + * The default browser context cannot be closed. |
| 117 | + */ |
| 118 | + isIncognito(): boolean { |
| 119 | + throw new Error('Not implemented'); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @example |
| 124 | + * |
| 125 | + * ```ts |
| 126 | + * const context = browser.defaultBrowserContext(); |
| 127 | + * await context.overridePermissions('https://html5demos.com', [ |
| 128 | + * 'geolocation', |
| 129 | + * ]); |
| 130 | + * ``` |
| 131 | + * |
| 132 | + * @param origin - The origin to grant permissions to, e.g. "https://example.com". |
| 133 | + * @param permissions - An array of permissions to grant. |
| 134 | + * All permissions that are not listed here will be automatically denied. |
| 135 | + */ |
| 136 | + overridePermissions(origin: string, permissions: Permission[]): Promise<void>; |
| 137 | + overridePermissions(): Promise<void> { |
| 138 | + throw new Error('Not implemented'); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Clears all permission overrides for the browser context. |
| 143 | + * |
| 144 | + * @example |
| 145 | + * |
| 146 | + * ```ts |
| 147 | + * const context = browser.defaultBrowserContext(); |
| 148 | + * context.overridePermissions('https://example.com', ['clipboard-read']); |
| 149 | + * // do stuff .. |
| 150 | + * context.clearPermissionOverrides(); |
| 151 | + * ``` |
| 152 | + */ |
| 153 | + clearPermissionOverrides(): Promise<void> { |
| 154 | + throw new Error('Not implemented'); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Creates a new page in the browser context. |
| 159 | + */ |
| 160 | + newPage(): Promise<Page> { |
| 161 | + throw new Error('Not implemented'); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * The browser this browser context belongs to. |
| 166 | + */ |
| 167 | + browser(): Browser { |
| 168 | + throw new Error('Not implemented'); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Closes the browser context. All the targets that belong to the browser context |
| 173 | + * will be closed. |
| 174 | + * |
| 175 | + * @remarks |
| 176 | + * Only incognito browser contexts can be closed. |
| 177 | + */ |
| 178 | + close(): Promise<void> { |
| 179 | + throw new Error('Not implemented'); |
| 180 | + } |
| 181 | +} |
0 commit comments