Skip to content

Commit

Permalink
feat: enable tab targets
Browse files Browse the repository at this point in the history
[Tab target is a new type of target in CDP](https://docs.google.com/document/d/14aeiC_zga2SS0OXJd6eIFj8N0o5LGwUpuqa4L8NKoR4/edit) that reflects the architectural changes in Chromium that were needed for features such as bfcache, portal and prerendering.

Previously, the `page` targets were the top-level targets representing the entire tab. If you navigate to a new URL, the page target would remain the same. With the introduction of the `tab` target, it's not possible to have multiple page targets active within one tab. For example, one page would be the currently shown page and the other one would be a [prerendered](https://github.com/WICG/nav-speculation/) page. When the user navigates to the prerendered page, the secondary page becomes the primary and the primary page target gets destroyed (the process is called activation).

This change enables tab targets and allows testing Chrome features related to prerendering.
Emulation and network manager domains should work with the prerendered targets. Some features
like JS/CSS coverage might not work over activation so you would need to disable activation to test those.

Use command line flags to disable prerendering in Chrome if you encounter issues. For example,
```
--disable-features=Prerender2
```
  • Loading branch information
OrKoN committed Oct 9, 2023
1 parent b1eee92 commit 88e118b
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 56 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ jobs:
- chrome-headful
- chrome-new-headless
- chrome-bidi
- chrome-new-headless-tab
shard:
- 1/2
- 2/2
Expand All @@ -159,10 +158,6 @@ jobs:
suite: chrome-bidi
- os: macos-latest
suite: chrome-headful
- os: windows-latest
suite: chrome-new-headless-tab
- os: macos-latest
suite: chrome-new-headless-tab
steps:
- name: Check out repository
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0
Expand Down
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"test-types": "tsd -t packages/puppeteer",
"test:chrome:headful": "wireit",
"test:chrome:new-headless": "wireit",
"test:chrome:new-headless-tab": "wireit",
"test:chrome:headless": "wireit",
"test:chrome:bidi": "wireit",
"test:chrome:bidi-local": "wireit",
Expand Down Expand Up @@ -76,9 +75,6 @@
"test:chrome:new-headless": {
"command": "npm test -- --test-suite chrome-new-headless"
},
"test:chrome:new-headless-tab": {
"command": "npm test -- --test-suite chrome-new-headless-tab"
},
"test:chrome:bidi": {
"command": "npm test -- --test-suite chrome-bidi"
},
Expand Down
5 changes: 0 additions & 5 deletions packages/puppeteer-core/src/cdp/Browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {CDPSessionEvent, type CDPSession} from '../api/CDPSession.js';
import type {Page} from '../api/Page.js';
import type {Target} from '../api/Target.js';
import type {Viewport} from '../common/Viewport.js';
import {USE_TAB_TARGET} from '../environment.js';
import {assert} from '../util/assert.js';

import {ChromeTargetManager} from './ChromeTargetManager.js';
Expand Down Expand Up @@ -64,7 +63,6 @@ export class CdpBrowser extends BrowserBase {
targetFilterCallback?: TargetFilterCallback,
isPageTargetCallback?: IsPageTargetCallback,
waitForInitiallyDiscoveredTargets = true,
useTabTarget = USE_TAB_TARGET
): Promise<CdpBrowser> {
const browser = new CdpBrowser(
product,
Expand All @@ -77,7 +75,6 @@ export class CdpBrowser extends BrowserBase {
targetFilterCallback,
isPageTargetCallback,
waitForInitiallyDiscoveredTargets,
useTabTarget
);
await browser._attach();
return browser;
Expand Down Expand Up @@ -108,7 +105,6 @@ export class CdpBrowser extends BrowserBase {
targetFilterCallback?: TargetFilterCallback,
isPageTargetCallback?: IsPageTargetCallback,
waitForInitiallyDiscoveredTargets = true,
useTabTarget = USE_TAB_TARGET
) {
super();
product = product || 'chrome';
Expand All @@ -135,7 +131,6 @@ export class CdpBrowser extends BrowserBase {
this.#createTarget,
this.#targetFilterCallback,
waitForInitiallyDiscoveredTargets,
useTabTarget
);
}
this.#defaultContext = new CdpBrowserContext(this.#connection, this);
Expand Down
25 changes: 9 additions & 16 deletions packages/puppeteer-core/src/cdp/ChromeTargetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,16 @@ export class ChromeTargetManager
#waitForInitiallyDiscoveredTargets = true;

// TODO: remove the flag once the testing/rollout is done.
#tabMode: boolean;
#discoveryFilter: Protocol.Target.FilterEntry[];

constructor(
connection: Connection,
targetFactory: TargetFactory,
targetFilterCallback?: TargetFilterCallback,
waitForInitiallyDiscoveredTargets = true,
useTabTarget = false
waitForInitiallyDiscoveredTargets = true
) {
super();
this.#tabMode = useTabTarget;
this.#discoveryFilter = this.#tabMode
? [{}]
: [{type: 'tab', exclude: true}, {}];
this.#discoveryFilter = [{}];
this.#connection = connection;
this.#targetFilterCallback = targetFilterCallback;
this.#targetFactory = targetFactory;
Expand Down Expand Up @@ -167,15 +162,13 @@ export class ChromeTargetManager
waitForDebuggerOnStart: true,
flatten: true,
autoAttach: true,
filter: this.#tabMode
? [
{
type: 'page',
exclude: true,
},
...this.#discoveryFilter,
]
: this.#discoveryFilter,
filter: [
{
type: 'page',
exclude: true,
},
...this.#discoveryFilter,
],
});
this.#finishInitializationIfReady();
await this.#initializeDeferred.valueOrThrow();
Expand Down
12 changes: 1 addition & 11 deletions packages/puppeteer-core/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,4 @@ export const DEFERRED_PROMISE_DEBUG_TIMEOUT =
typeof process !== 'undefined' &&
typeof process.env['PUPPETEER_DEFERRED_PROMISE_DEBUG_TIMEOUT'] !== 'undefined'
? Number(process.env['PUPPETEER_DEFERRED_PROMISE_DEBUG_TIMEOUT'])
: -1;

/**
* Only used for internal testing.
*
* @internal
*/
export const USE_TAB_TARGET =
typeof process !== 'undefined'
? process.env['PUPPETEER_INTERNAL_TAB_TARGET'] === 'true'
: false;
: -1;
5 changes: 0 additions & 5 deletions packages/puppeteer-core/src/node/ChromeLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {

import type {Browser} from '../api/Browser.js';
import {debugError} from '../common/util.js';
import {USE_TAB_TARGET} from '../environment.js';
import {assert} from '../util/assert.js';

import type {
Expand Down Expand Up @@ -175,10 +174,6 @@ export class ChromeLauncher extends ProductLauncher {
'OptimizationHints',
];

if (!USE_TAB_TARGET) {
disabledFeatures.push('Prerender2');
}

const chromeArguments = [
'--allow-pre-commit-input',
'--disable-background-networking',
Expand Down
11 changes: 1 addition & 10 deletions test/TestSuites.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@
"parameters": ["chrome", "new-headless", "cdp"],
"expectedLineCoverage": 93
},
{
"id": "chrome-new-headless-tab",
"platforms": ["linux"],
"parameters": ["chrome", "new-headless", "cdp", "tabTarget"],
"expectedLineCoverage": 93
},
{
"id": "firefox-headless",
"platforms": ["linux", "darwin"],
Expand Down Expand Up @@ -69,9 +63,6 @@
"webDriverBiDi": {
"PUPPETEER_PROTOCOL": "webDriverBiDi"
},
"cdp": {},
"tabTarget": {
"PUPPETEER_INTERNAL_TAB_TARGET": "true"
}
"cdp": {}
}
}

0 comments on commit 88e118b

Please sign in to comment.