-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathbrowser.test.ts
More file actions
99 lines (94 loc) · 2.7 KB
/
browser.test.ts
File metadata and controls
99 lines (94 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import assert from 'node:assert';
import os from 'node:os';
import path from 'node:path';
import {describe, it} from 'node:test';
import {executablePath} from 'puppeteer';
import {ensureBrowserConnected, launch} from '../src/browser.js';
describe('browser', () => {
it('cannot launch multiple times with the same profile', async () => {
const tmpDir = os.tmpdir();
const folderPath = path.join(tmpDir, `temp-folder-${crypto.randomUUID()}`);
const browser1 = await launch({
headless: true,
isolated: false,
userDataDir: folderPath,
executablePath: executablePath(),
devtools: false,
});
try {
try {
const browser2 = await launch({
headless: true,
isolated: false,
userDataDir: folderPath,
executablePath: executablePath(),
devtools: false,
});
await browser2.close();
assert.fail('not reached');
} catch (err) {
assert.strictEqual(
err.message,
`The browser is already running for ${folderPath}. Use --isolated to run multiple browser instances.`,
);
}
} finally {
await browser1.close();
}
});
it('launches with the initial viewport', async () => {
const tmpDir = os.tmpdir();
const folderPath = path.join(tmpDir, `temp-folder-${crypto.randomUUID()}`);
const browser = await launch({
headless: true,
isolated: false,
userDataDir: folderPath,
executablePath: executablePath(),
viewport: {
width: 1501,
height: 801,
},
devtools: false,
});
try {
const [page] = await browser.pages();
const result = await page.evaluate(() => {
return {width: window.innerWidth, height: window.innerHeight};
});
assert.deepStrictEqual(result, {
width: 1501,
height: 801,
});
} finally {
await browser.close();
}
});
it('connects to an existing browser with userDataDir', async () => {
const tmpDir = os.tmpdir();
const folderPath = path.join(tmpDir, `temp-folder-${crypto.randomUUID()}`);
const browser = await launch({
headless: true,
isolated: false,
userDataDir: folderPath,
executablePath: executablePath(),
devtools: false,
args: ['--remote-debugging-port=0'],
});
try {
const connectedBrowser = await ensureBrowserConnected({
userDataDir: folderPath,
devtools: false,
});
assert.ok(connectedBrowser);
assert.ok(connectedBrowser.connected);
connectedBrowser.disconnect();
} finally {
await browser.close();
}
});
});