cdp-tunnel 1.0.13 → 1.0.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/index.js +65 -0
- package/extension-new/background.js +6 -1
- package/extension-new/cdp/handler/special.js +47 -27
- package/extension-new/core/state.js +64 -6
- package/extension-new/core/websocket.js +124 -19
- package/package.json +9 -2
- package/server/proxy-server.js +45 -34
- package/.github/workflows/publish.yml +0 -92
- package/.github/workflows/release-assets.yml +0 -50
- package/PUBLISH.md +0 -65
- package/console-test.js +0 -52
- package/docs/README_CN.md +0 -204
- package/docs/config-page-screenshot.png +0 -0
- package/final-console-test.js +0 -105
- package/simple-tab-group-test.js +0 -56
- package/test-cdp-connection.js +0 -85
- package/test-cdp-groups.js +0 -71
- package/test-check-newtab.js +0 -144
- package/test-chrome-native.js +0 -140
- package/test-client-connected.js +0 -99
- package/test-compare-formats.js +0 -88
- package/test-context-features.js +0 -113
- package/test-create-tab.js +0 -113
- package/test-debug-broadcast.js +0 -52
- package/test-debug-targets.js +0 -127
- package/test-expose-newtab.js +0 -164
- package/test-expose-shared.js +0 -189
- package/test-final-logs.js +0 -110
- package/test-fresh-chromium.js +0 -153
- package/test-init-script.js +0 -128
- package/test-keepalive.js +0 -89
- package/test-launch-chromium.js +0 -140
- package/test-launch-vs-connect.js +0 -149
- package/test-listen-events.js +0 -102
- package/test-monitor.js +0 -83
- package/test-multiple-cdp-groups.js +0 -78
- package/test-native.js +0 -96
- package/test-page-connection.js +0 -74
- package/test-playwright-connection.js +0 -45
- package/test-playwright-groups.js +0 -47
- package/test-playwright-pages.js +0 -47
- package/test-playwright-sequence.js +0 -81
- package/test-proper-context.js +0 -129
- package/test-real-final.js +0 -251
- package/test-real-scenario-v2.js +0 -166
- package/test-real-scenario-v3.js +0 -231
- package/test-real-scenario.js +0 -104
- package/test-server-logs.js +0 -98
- package/test-session-id.js +0 -91
- package/test-simple-cdp-groups.js +0 -44
- package/test-simple-context.js +0 -137
- package/test-tab-group-simple.js +0 -58
- package/test-tab-grouping.js +0 -48
- package/test-three-pages.js +0 -192
- package/test-wait-for-page.js +0 -95
- package/test-with-logs.js +0 -118
- package/test-ws-groups.js +0 -59
- package/tests/e2e-auto-test.js +0 -304
- package/tests/iframe-test-page.html +0 -89
- package/tests/playwright-demo.js +0 -45
- package/tests/playwright-interactive.js +0 -261
- package/tests/playwright-multi-demo.js +0 -60
- package/tests/playwright-multi.js +0 -85
- package/tests/playwright-single.js +0 -41
- package/tests/screenshot-config.js +0 -35
- package/tests/test-client.js +0 -89
- package/tests/test-douyin-iframe.js +0 -171
- package/tests/test-iframe-debug.js +0 -204
- package/tests/test-multi-client.js +0 -129
package/test-launch-chromium.js
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
const { chromium } = require('playwright');
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
|
|
5
|
-
let serverPort = 9100;
|
|
6
|
-
|
|
7
|
-
async function testWithLaunch(label) {
|
|
8
|
-
serverPort++;
|
|
9
|
-
const currentPort = serverPort;
|
|
10
|
-
|
|
11
|
-
console.log(`\n${'='.repeat(60)}`);
|
|
12
|
-
console.log(`Testing ${label} - chromium.launch()`);
|
|
13
|
-
console.log('='.repeat(60));
|
|
14
|
-
|
|
15
|
-
try {
|
|
16
|
-
const sharedState = { counter: 0 };
|
|
17
|
-
|
|
18
|
-
const htmlContent1 = `
|
|
19
|
-
<!DOCTYPE html>
|
|
20
|
-
<html>
|
|
21
|
-
<head><title>Page 1</title></head>
|
|
22
|
-
<body>
|
|
23
|
-
<h1>Page 1</h1>
|
|
24
|
-
<a href="page2.html" target="_blank" id="link1">Open New Tab</a>
|
|
25
|
-
</body>
|
|
26
|
-
</html>
|
|
27
|
-
`;
|
|
28
|
-
|
|
29
|
-
const htmlContent2 = `
|
|
30
|
-
<!DOCTYPE html>
|
|
31
|
-
<html>
|
|
32
|
-
<head><title>Page 2</title></head>
|
|
33
|
-
<body>
|
|
34
|
-
<h1>Page 2 - New Tab</h1>
|
|
35
|
-
</body>
|
|
36
|
-
</html>
|
|
37
|
-
`;
|
|
38
|
-
|
|
39
|
-
const serverDir = '/tmp/test-launch-chromium';
|
|
40
|
-
if (!fs.existsSync(serverDir)) {
|
|
41
|
-
fs.mkdirSync(serverDir, { recursive: true });
|
|
42
|
-
}
|
|
43
|
-
fs.writeFileSync(path.join(serverDir, 'page1.html'), htmlContent1);
|
|
44
|
-
fs.writeFileSync(path.join(serverDir, 'page2.html'), htmlContent2);
|
|
45
|
-
|
|
46
|
-
const http = require('http');
|
|
47
|
-
const server = http.createServer((req, res) => {
|
|
48
|
-
let filePath = path.join(serverDir, req.url === '/' ? 'page1.html' : req.url);
|
|
49
|
-
if (fs.existsSync(filePath)) {
|
|
50
|
-
const content = fs.readFileSync(filePath);
|
|
51
|
-
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
52
|
-
res.end(content);
|
|
53
|
-
} else {
|
|
54
|
-
res.writeHead(404);
|
|
55
|
-
res.end('Not found');
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
await new Promise(resolve => server.listen(currentPort, resolve));
|
|
60
|
-
console.log(`[${label}] Server on port ${currentPort}`);
|
|
61
|
-
|
|
62
|
-
console.log(`[${label}] Launching Chromium...`);
|
|
63
|
-
const browser = await chromium.launch({
|
|
64
|
-
executablePath: '/Applications/Chromium.app/Contents/MacOS/Chromium',
|
|
65
|
-
headless: false
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
console.log(`[${label}] Launched!`);
|
|
69
|
-
|
|
70
|
-
console.log(`[${label}] Creating context...`);
|
|
71
|
-
const context = await browser.newContext();
|
|
72
|
-
|
|
73
|
-
console.log(`[${label}] exposeFunction getCounter...`);
|
|
74
|
-
await context.exposeFunction('getCounter', () => {
|
|
75
|
-
sharedState.counter += 1;
|
|
76
|
-
console.log(`[${label}] getCounter: ${sharedState.counter}`);
|
|
77
|
-
return sharedState.counter;
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
console.log(`[${label}] Creating first page...`);
|
|
81
|
-
const page1 = await context.newPage();
|
|
82
|
-
await page1.goto(`http://localhost:${currentPort}/page1.html`, { waitUntil: 'domcontentloaded' });
|
|
83
|
-
|
|
84
|
-
console.log(`[${label}] Testing first page...`);
|
|
85
|
-
const r1 = await page1.evaluate(() => window.getCounter());
|
|
86
|
-
console.log(`[${label}] First page: ${r1}`);
|
|
87
|
-
|
|
88
|
-
console.log(`[${label}] Clicking link...`);
|
|
89
|
-
await page1.click('#link1');
|
|
90
|
-
|
|
91
|
-
console.log(`[${label}] Waiting 3 seconds...`);
|
|
92
|
-
await new Promise(r => setTimeout(r, 3000));
|
|
93
|
-
|
|
94
|
-
console.log(`[${label}] Checking pages...`);
|
|
95
|
-
const allPages = context.pages();
|
|
96
|
-
console.log(`[${label}] Total pages: ${allPages.length}`);
|
|
97
|
-
for (let i = 0; i < allPages.length; i++) {
|
|
98
|
-
console.log(`[${label}] Page ${i}: ${allPages[i].url()}`);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (allPages.length >= 2) {
|
|
102
|
-
console.log(`[${label}] Testing new tab...`);
|
|
103
|
-
const page2 = allPages[1];
|
|
104
|
-
const r2 = await page2.evaluate(() => window.getCounter());
|
|
105
|
-
console.log(`[${label}] New tab: ${r2}`);
|
|
106
|
-
|
|
107
|
-
console.log(`[${label}] First page again...`);
|
|
108
|
-
const r1a = await page1.evaluate(() => window.getCounter());
|
|
109
|
-
console.log(`[${label}] First page again: ${r1a}`);
|
|
110
|
-
|
|
111
|
-
console.log(`[${label}] New tab again...`);
|
|
112
|
-
const r2a = await page2.evaluate(() => window.getCounter());
|
|
113
|
-
console.log(`[${label}] New tab again: ${r2a}`);
|
|
114
|
-
|
|
115
|
-
const success = r1 === 1 && r2 === 2 && r1a === 3 && r2a === 4;
|
|
116
|
-
if (success) {
|
|
117
|
-
console.log(`[${label}] ✓ PASS! Counter: 1 -> 2 -> 3 -> 4`);
|
|
118
|
-
} else {
|
|
119
|
-
console.log(`[${label}] ✗ FAIL! Expected: 1,2,3,4 Got: ${r1},${r2},${r1a},${r2a}`);
|
|
120
|
-
}
|
|
121
|
-
} else {
|
|
122
|
-
console.log(`[${label}] ✗ No new tab found!`);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
server.close();
|
|
126
|
-
await browser.close();
|
|
127
|
-
|
|
128
|
-
} catch (error) {
|
|
129
|
-
console.error(`[${label}] Error:`, error.message);
|
|
130
|
-
console.error(error.stack);
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
async function main() {
|
|
135
|
-
console.log('Testing with chromium.launch() using system Chromium\n');
|
|
136
|
-
await testWithLaunch('Native Chromium');
|
|
137
|
-
process.exit(0);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
main();
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
const { chromium } = require('playwright');
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
|
|
5
|
-
let serverPort = 9000;
|
|
6
|
-
|
|
7
|
-
async function testWithLaunchBrowser(port, label, useConnect) {
|
|
8
|
-
serverPort++;
|
|
9
|
-
const currentPort = serverPort;
|
|
10
|
-
|
|
11
|
-
console.log(`\n${'='.repeat(60)}`);
|
|
12
|
-
console.log(`Testing ${label}`);
|
|
13
|
-
console.log(`Mode: ${useConnect ? 'connectOverCDP' : 'launch'}`);
|
|
14
|
-
console.log('='.repeat(60));
|
|
15
|
-
|
|
16
|
-
try {
|
|
17
|
-
const sharedState = { counter: 0 };
|
|
18
|
-
|
|
19
|
-
const htmlContent1 = `
|
|
20
|
-
<!DOCTYPE html>
|
|
21
|
-
<html>
|
|
22
|
-
<head><title>Page 1</title></head>
|
|
23
|
-
<body>
|
|
24
|
-
<h1>Page 1</h1>
|
|
25
|
-
<a href="page2.html" target="_blank" id="link1">Open New Tab</a>
|
|
26
|
-
</body>
|
|
27
|
-
</html>
|
|
28
|
-
`;
|
|
29
|
-
|
|
30
|
-
const htmlContent2 = `
|
|
31
|
-
<!DOCTYPE html>
|
|
32
|
-
<html>
|
|
33
|
-
<head><title>Page 2</title></head>
|
|
34
|
-
<body>
|
|
35
|
-
<h1>Page 2 - New Tab</h1>
|
|
36
|
-
</body>
|
|
37
|
-
</html>
|
|
38
|
-
`;
|
|
39
|
-
|
|
40
|
-
const serverDir = '/tmp/test-launch';
|
|
41
|
-
if (!fs.existsSync(serverDir)) {
|
|
42
|
-
fs.mkdirSync(serverDir, { recursive: true });
|
|
43
|
-
}
|
|
44
|
-
fs.writeFileSync(path.join(serverDir, 'page1.html'), htmlContent1);
|
|
45
|
-
fs.writeFileSync(path.join(serverDir, 'page2.html'), htmlContent2);
|
|
46
|
-
|
|
47
|
-
const http = require('http');
|
|
48
|
-
const server = http.createServer((req, res) => {
|
|
49
|
-
let filePath = path.join(serverDir, req.url === '/' ? 'page1.html' : req.url);
|
|
50
|
-
if (fs.existsSync(filePath)) {
|
|
51
|
-
const content = fs.readFileSync(filePath);
|
|
52
|
-
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
53
|
-
res.end(content);
|
|
54
|
-
} else {
|
|
55
|
-
res.writeHead(404);
|
|
56
|
-
res.end('Not found');
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
await new Promise(resolve => server.listen(currentPort, resolve));
|
|
61
|
-
console.log(`[${label}] Server on port ${currentPort}`);
|
|
62
|
-
|
|
63
|
-
let browser;
|
|
64
|
-
if (useConnect) {
|
|
65
|
-
console.log(`[${label}] Connecting to existing browser on port ${port}...`);
|
|
66
|
-
browser = await chromium.connectOverCDP(`http://localhost:${port}`);
|
|
67
|
-
} else {
|
|
68
|
-
console.log(`[${label}] Launching new browser...`);
|
|
69
|
-
browser = await chromium.launch({
|
|
70
|
-
headless: false,
|
|
71
|
-
args: [`--remote-debugging-port=${port}`]
|
|
72
|
-
});
|
|
73
|
-
await new Promise(r => setTimeout(r, 2000));
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
console.log(`[${label}] Connected/Launched!`);
|
|
77
|
-
|
|
78
|
-
console.log(`[${label}] Creating context...`);
|
|
79
|
-
const context = await browser.newContext();
|
|
80
|
-
|
|
81
|
-
console.log(`[${label}] exposeFunction getCounter...`);
|
|
82
|
-
await context.exposeFunction('getCounter', () => {
|
|
83
|
-
sharedState.counter += 1;
|
|
84
|
-
console.log(`[${label}] getCounter: ${sharedState.counter}`);
|
|
85
|
-
return sharedState.counter;
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
console.log(`[${label}] Creating first page...`);
|
|
89
|
-
const page1 = await context.newPage();
|
|
90
|
-
await page1.goto(`http://localhost:${currentPort}/page1.html`, { waitUntil: 'domcontentloaded' });
|
|
91
|
-
|
|
92
|
-
console.log(`[${label}] Testing first page...`);
|
|
93
|
-
try {
|
|
94
|
-
const r1 = await page1.evaluate(() => window.getCounter());
|
|
95
|
-
console.log(`[${label}] First page: ${r1}`);
|
|
96
|
-
} catch (e) {
|
|
97
|
-
console.log(`[${label}] First page ERROR: ${e.message}`);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
console.log(`[${label}] Clicking link...`);
|
|
101
|
-
await page1.click('#link1');
|
|
102
|
-
|
|
103
|
-
console.log(`[${label}] Waiting 3 seconds...`);
|
|
104
|
-
await new Promise(r => setTimeout(r, 3000));
|
|
105
|
-
|
|
106
|
-
console.log(`[${label}] Checking pages...`);
|
|
107
|
-
const allPages = context.pages();
|
|
108
|
-
console.log(`[${label}] Total pages: ${allPages.length}`);
|
|
109
|
-
for (let i = 0; i < allPages.length; i++) {
|
|
110
|
-
console.log(`[${label}] Page ${i}: ${allPages[i].url()}`);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
server.close();
|
|
114
|
-
await browser.close();
|
|
115
|
-
|
|
116
|
-
} catch (error) {
|
|
117
|
-
console.error(`[${label}] Error:`, error.message);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async function main() {
|
|
122
|
-
console.log('='.repeat(60));
|
|
123
|
-
console.log('Test 1: Playwright launch() - should work perfectly');
|
|
124
|
-
console.log('Test 2: connectOverCDP() - for comparison');
|
|
125
|
-
console.log('='.repeat(60));
|
|
126
|
-
|
|
127
|
-
console.log('\n### Test 1: chromium.launch() ###');
|
|
128
|
-
await testWithLaunchBrowser(0, 'Launch', false);
|
|
129
|
-
|
|
130
|
-
await new Promise(r => setTimeout(r, 2000));
|
|
131
|
-
|
|
132
|
-
console.log('\n### Test 2: chromium.connectOverCDP() to fresh browser ###');
|
|
133
|
-
const p = require('child_process').spawn('/Applications/Chromium.app/Contents/MacOS/Chromium', [
|
|
134
|
-
'--remote-debugging-port=9240',
|
|
135
|
-
'--user-data-dir=/tmp/chromium-connect-test',
|
|
136
|
-
'--no-first-run',
|
|
137
|
-
'--no-default-browser-check'
|
|
138
|
-
], { detached: true, stdio: 'ignore' });
|
|
139
|
-
|
|
140
|
-
await new Promise(r => setTimeout(r, 3000));
|
|
141
|
-
|
|
142
|
-
await testWithLaunchBrowser(9240, 'Connect', true);
|
|
143
|
-
|
|
144
|
-
try { process.kill(-p.pid); } catch(e) {}
|
|
145
|
-
|
|
146
|
-
process.exit(0);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
main();
|
package/test-listen-events.js
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
const { chromium } = require('playwright');
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const http = require('http');
|
|
5
|
-
|
|
6
|
-
let serverPort = 10000;
|
|
7
|
-
|
|
8
|
-
async function test() {
|
|
9
|
-
serverPort++;
|
|
10
|
-
const currentPort = serverPort;
|
|
11
|
-
|
|
12
|
-
const htmlContent1 = `
|
|
13
|
-
<!DOCTYPE html>
|
|
14
|
-
<html>
|
|
15
|
-
<head><title>Page 1</title></head>
|
|
16
|
-
<body>
|
|
17
|
-
<h1>Page 1</h1>
|
|
18
|
-
<a href="page2.html" target="_blank" id="link1">Open New Tab</a>
|
|
19
|
-
</body>
|
|
20
|
-
</html>
|
|
21
|
-
`;
|
|
22
|
-
|
|
23
|
-
const htmlContent2 = `
|
|
24
|
-
<!DOCTYPE html>
|
|
25
|
-
<html>
|
|
26
|
-
<head><title>Page 2</title></head>
|
|
27
|
-
<body>
|
|
28
|
-
<h1>Page 2 - New Tab</h1>
|
|
29
|
-
</body>
|
|
30
|
-
</html>
|
|
31
|
-
`;
|
|
32
|
-
|
|
33
|
-
const serverDir = '/tmp/test-listen';
|
|
34
|
-
if (!fs.existsSync(serverDir)) fs.mkdirSync(serverDir, { recursive: true });
|
|
35
|
-
fs.writeFileSync(path.join(serverDir, 'page1.html'), htmlContent1);
|
|
36
|
-
fs.writeFileSync(path.join(serverDir, 'page2.html'), htmlContent2);
|
|
37
|
-
|
|
38
|
-
const server = http.createServer((req, res) => {
|
|
39
|
-
let filePath = path.join(serverDir, req.url === '/' ? 'page1.html' : req.url);
|
|
40
|
-
if (fs.existsSync(filePath)) {
|
|
41
|
-
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
42
|
-
res.end(fs.readFileSync(filePath));
|
|
43
|
-
} else {
|
|
44
|
-
res.writeHead(404);
|
|
45
|
-
res.end('Not found');
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
await new Promise(resolve => server.listen(currentPort, resolve));
|
|
50
|
-
console.log(`Server: http://localhost:${currentPort}`);
|
|
51
|
-
|
|
52
|
-
console.log('\n[TEST] Connecting...');
|
|
53
|
-
const browser = await chromium.connectOverCDP('http://localhost:9221');
|
|
54
|
-
console.log('[TEST] Connected!');
|
|
55
|
-
|
|
56
|
-
// 监听 Target.targetCreated 事件
|
|
57
|
-
console.log('[TEST] Setting up Target.targetCreated listener...');
|
|
58
|
-
const session = await browser.newBrowserCDPSession();
|
|
59
|
-
|
|
60
|
-
let targetCreatedReceived = false;
|
|
61
|
-
let targetCreatedCount = 0;
|
|
62
|
-
|
|
63
|
-
session.on('Target.targetCreated', (params) => {
|
|
64
|
-
targetCreatedReceived = true;
|
|
65
|
-
targetCreatedCount++;
|
|
66
|
-
console.log('[EVENT] Target.targetCreated:', {
|
|
67
|
-
targetId: params.targetInfo?.targetId?.substring(0, 8),
|
|
68
|
-
type: params.targetInfo?.type,
|
|
69
|
-
url: params.targetInfo?.url?.substring(0, 50)
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
// 启用 Target 域
|
|
74
|
-
await session.send('Target.setDiscoverTargets', { discover: true });
|
|
75
|
-
console.log('[TEST] Target.setDiscoverTargets sent');
|
|
76
|
-
|
|
77
|
-
console.log('[TEST] Creating context...');
|
|
78
|
-
const context = await browser.newContext();
|
|
79
|
-
|
|
80
|
-
console.log('[TEST] Creating page...');
|
|
81
|
-
const page1 = await context.newPage();
|
|
82
|
-
await page1.goto(`http://localhost:${currentPort}/page1.html`, { waitUntil: 'domcontentloaded' });
|
|
83
|
-
console.log('[TEST] Page loaded');
|
|
84
|
-
|
|
85
|
-
console.log('[TEST] Clicking link...');
|
|
86
|
-
await page1.click('#link1');
|
|
87
|
-
|
|
88
|
-
console.log('[TEST] Waiting 5 seconds...');
|
|
89
|
-
await new Promise(r => setTimeout(r, 5000));
|
|
90
|
-
|
|
91
|
-
console.log('\n[TEST] Results:');
|
|
92
|
-
console.log(` Target.targetCreated received: ${targetCreatedReceived}`);
|
|
93
|
-
console.log(` Target.targetCreated count: ${targetCreatedCount}`);
|
|
94
|
-
|
|
95
|
-
const allPages = context.pages();
|
|
96
|
-
console.log(` Pages in context: ${allPages.length}`);
|
|
97
|
-
|
|
98
|
-
server.close();
|
|
99
|
-
await browser.close();
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
test().then(() => process.exit(0));
|
package/test-monitor.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
const { chromium } = require('playwright');
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const http = require('http');
|
|
5
|
-
|
|
6
|
-
let serverPort = 9800;
|
|
7
|
-
|
|
8
|
-
async function test() {
|
|
9
|
-
serverPort++;
|
|
10
|
-
const currentPort = serverPort;
|
|
11
|
-
|
|
12
|
-
const htmlContent1 = `
|
|
13
|
-
<!DOCTYPE html>
|
|
14
|
-
<html>
|
|
15
|
-
<head><title>Page 1</title></head>
|
|
16
|
-
<body>
|
|
17
|
-
<h1>Page 1</h1>
|
|
18
|
-
<a href="page2.html" target="_blank" id="link1">Open New Tab</a>
|
|
19
|
-
</body>
|
|
20
|
-
</html>
|
|
21
|
-
`;
|
|
22
|
-
|
|
23
|
-
const htmlContent2 = `
|
|
24
|
-
<!DOCTYPE html>
|
|
25
|
-
<html>
|
|
26
|
-
<head><title>Page 2</title></head>
|
|
27
|
-
<body>
|
|
28
|
-
<h1>Page 2 - New Tab</h1>
|
|
29
|
-
</body>
|
|
30
|
-
</html>
|
|
31
|
-
`;
|
|
32
|
-
|
|
33
|
-
const serverDir = '/tmp/test-monitor';
|
|
34
|
-
if (!fs.existsSync(serverDir)) fs.mkdirSync(serverDir, { recursive: true });
|
|
35
|
-
fs.writeFileSync(path.join(serverDir, 'page1.html'), htmlContent1);
|
|
36
|
-
fs.writeFileSync(path.join(serverDir, 'page2.html'), htmlContent2);
|
|
37
|
-
|
|
38
|
-
const server = http.createServer((req, res) => {
|
|
39
|
-
let filePath = path.join(serverDir, req.url === '/' ? 'page1.html' : req.url);
|
|
40
|
-
if (fs.existsSync(filePath)) {
|
|
41
|
-
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
42
|
-
res.end(fs.readFileSync(filePath));
|
|
43
|
-
} else {
|
|
44
|
-
res.writeHead(404);
|
|
45
|
-
res.end('Not found');
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
await new Promise(resolve => server.listen(currentPort, resolve));
|
|
50
|
-
console.log(`Server: http://localhost:${currentPort}`);
|
|
51
|
-
|
|
52
|
-
console.log('\n[TEST] Connecting to CDP Tunnel...');
|
|
53
|
-
const browser = await chromium.connectOverCDP('http://localhost:9221');
|
|
54
|
-
console.log('[TEST] Connected!');
|
|
55
|
-
|
|
56
|
-
console.log('[TEST] Creating context...');
|
|
57
|
-
const context = await browser.newContext();
|
|
58
|
-
|
|
59
|
-
console.log('[TEST] Creating page...');
|
|
60
|
-
const page1 = await context.newPage();
|
|
61
|
-
await page1.goto(`http://localhost:${currentPort}/page1.html`, { waitUntil: 'domcontentloaded' });
|
|
62
|
-
console.log('[TEST] Page loaded');
|
|
63
|
-
|
|
64
|
-
console.log('[TEST] Waiting 3 seconds...');
|
|
65
|
-
await new Promise(r => setTimeout(r, 3000));
|
|
66
|
-
|
|
67
|
-
console.log('[TEST] Clicking link to open new tab...');
|
|
68
|
-
await page1.click('#link1');
|
|
69
|
-
|
|
70
|
-
console.log('[TEST] Waiting 5 seconds...');
|
|
71
|
-
await new Promise(r => setTimeout(r, 5000));
|
|
72
|
-
|
|
73
|
-
console.log('[TEST] Checking pages...');
|
|
74
|
-
const allPages = context.pages();
|
|
75
|
-
console.log(`[TEST] Total pages: ${allPages.length}`);
|
|
76
|
-
|
|
77
|
-
server.close();
|
|
78
|
-
await browser.close();
|
|
79
|
-
|
|
80
|
-
console.log('[TEST] Done!');
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
test().then(() => process.exit(0));
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
const { chromium } = require('playwright');
|
|
2
|
-
|
|
3
|
-
async function testMultipleCDPGroups() {
|
|
4
|
-
console.log('=== 测试多个CDP连接的标签分组功能 ===\n');
|
|
5
|
-
|
|
6
|
-
try {
|
|
7
|
-
// 第一个CDP连接
|
|
8
|
-
console.log('创建第一个CDP连接...');
|
|
9
|
-
const browser1 = await chromium.connectOverCDP('http://localhost:9221');
|
|
10
|
-
console.log('第一个CDP连接成功');
|
|
11
|
-
|
|
12
|
-
// 创建第一个连接的标签页
|
|
13
|
-
console.log('第一个连接:创建第一个标签页...');
|
|
14
|
-
const page1_1 = await browser1.newPage();
|
|
15
|
-
await page1_1.goto('https://www.baidu.com/');
|
|
16
|
-
await page1_1.waitForTimeout(3000);
|
|
17
|
-
console.log('第一个连接:第一个标签页创建完成');
|
|
18
|
-
|
|
19
|
-
// 创建第二个标签页
|
|
20
|
-
console.log('第一个连接:创建第二个标签页...');
|
|
21
|
-
const page1_2 = await browser1.newPage();
|
|
22
|
-
await page1_2.goto('https://www.google.com/');
|
|
23
|
-
await page1_2.waitForTimeout(3000);
|
|
24
|
-
console.log('第一个连接:第二个标签页创建完成');
|
|
25
|
-
|
|
26
|
-
// 等待一段时间,确保标签页被添加到组中
|
|
27
|
-
console.log('等待5秒,确保标签页被添加到组中...');
|
|
28
|
-
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
29
|
-
|
|
30
|
-
// 关闭第一个连接
|
|
31
|
-
console.log('关闭第一个CDP连接...');
|
|
32
|
-
await browser1.close();
|
|
33
|
-
console.log('第一个CDP连接已关闭');
|
|
34
|
-
|
|
35
|
-
// 等待一段时间
|
|
36
|
-
console.log('等待3秒...');
|
|
37
|
-
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
38
|
-
|
|
39
|
-
// 第二个CDP连接
|
|
40
|
-
console.log('\n创建第二个CDP连接...');
|
|
41
|
-
const browser2 = await chromium.connectOverCDP('http://localhost:9221');
|
|
42
|
-
console.log('第二个CDP连接成功');
|
|
43
|
-
|
|
44
|
-
// 创建第二个连接的标签页
|
|
45
|
-
console.log('第二个连接:创建第一个标签页...');
|
|
46
|
-
const page2_1 = await browser2.newPage();
|
|
47
|
-
await page2_1.goto('https://www.github.com/');
|
|
48
|
-
await page2_1.waitForTimeout(3000);
|
|
49
|
-
console.log('第二个连接:第一个标签页创建完成');
|
|
50
|
-
|
|
51
|
-
// 创建第二个标签页
|
|
52
|
-
console.log('第二个连接:创建第二个标签页...');
|
|
53
|
-
const page2_2 = await browser2.newPage();
|
|
54
|
-
await page2_2.goto('https://www.stackoverflow.com/');
|
|
55
|
-
await page2_2.waitForTimeout(3000);
|
|
56
|
-
console.log('第二个连接:第二个标签页创建完成');
|
|
57
|
-
|
|
58
|
-
// 等待一段时间,确保标签页被添加到组中
|
|
59
|
-
console.log('等待5秒,确保标签页被添加到组中...');
|
|
60
|
-
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
61
|
-
|
|
62
|
-
// 关闭第二个连接
|
|
63
|
-
console.log('关闭第二个CDP连接...');
|
|
64
|
-
await browser2.close();
|
|
65
|
-
console.log('第二个CDP连接已关闭');
|
|
66
|
-
|
|
67
|
-
console.log('\n=== 测试完成 ===');
|
|
68
|
-
console.log('请检查Chrome浏览器中的标签组:');
|
|
69
|
-
console.log('1. 第一个CDP连接应该创建了一个标签组,包含百度和Google两个标签页');
|
|
70
|
-
console.log('2. 第二个CDP连接应该创建了另一个标签组,包含GitHub和Stack Overflow两个标签页');
|
|
71
|
-
console.log('3. 两个标签组应该有不同的名称和颜色');
|
|
72
|
-
|
|
73
|
-
} catch (error) {
|
|
74
|
-
console.error('测试过程中出现错误:', error);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
testMultipleCDPGroups();
|
package/test-native.js
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
const { chromium } = require('playwright');
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const http = require('http');
|
|
5
|
-
|
|
6
|
-
let serverPort = 10100;
|
|
7
|
-
|
|
8
|
-
async function test() {
|
|
9
|
-
serverPort++;
|
|
10
|
-
const currentPort = serverPort;
|
|
11
|
-
|
|
12
|
-
const htmlContent1 = `
|
|
13
|
-
<!DOCTYPE html>
|
|
14
|
-
<html>
|
|
15
|
-
<head><title>Page 1</title></head>
|
|
16
|
-
<body>
|
|
17
|
-
<h1>Page 1</h1>
|
|
18
|
-
<a href="page2.html" target="_blank" id="link1">Open New Tab</a>
|
|
19
|
-
</body>
|
|
20
|
-
</html>
|
|
21
|
-
`;
|
|
22
|
-
|
|
23
|
-
const htmlContent2 = `
|
|
24
|
-
<!DOCTYPE html>
|
|
25
|
-
<html>
|
|
26
|
-
<head><title>Page 2</title></head>
|
|
27
|
-
<body>
|
|
28
|
-
<h1>Page 2 - New Tab</h1>
|
|
29
|
-
</body>
|
|
30
|
-
</html>
|
|
31
|
-
`;
|
|
32
|
-
|
|
33
|
-
const serverDir = '/tmp/test-wait';
|
|
34
|
-
if (!fs.existsSync(serverDir)) fs.mkdirSync(serverDir, { recursive: true });
|
|
35
|
-
fs.writeFileSync(path.join(serverDir, 'page1.html'), htmlContent1);
|
|
36
|
-
fs.writeFileSync(path.join(serverDir, 'page2.html'), htmlContent2);
|
|
37
|
-
|
|
38
|
-
const server = http.createServer((req, res) => {
|
|
39
|
-
let filePath = path.join(serverDir, req.url === '/' ? 'page1.html' : req.url);
|
|
40
|
-
if (fs.existsSync(filePath)) {
|
|
41
|
-
res.writeHead(200, { 'Content-Type': 'text/html' });
|
|
42
|
-
res.end(fs.readFileSync(filePath));
|
|
43
|
-
} else {
|
|
44
|
-
res.writeHead(404);
|
|
45
|
-
res.end('Not found');
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
await new Promise(resolve => server.listen(currentPort, resolve));
|
|
50
|
-
console.log(`Server: http://localhost:${currentPort}`);
|
|
51
|
-
|
|
52
|
-
console.log('\n[TEST] Connecting to CDP Tunnel (port 9221)...');
|
|
53
|
-
const browser = await chromium.connectOverCDP('http://localhost:9221');
|
|
54
|
-
console.log('[TEST] Connected!');
|
|
55
|
-
|
|
56
|
-
console.log('[TEST] Creating context...');
|
|
57
|
-
const context = await browser.newContext();
|
|
58
|
-
|
|
59
|
-
console.log('[TEST] Creating page...');
|
|
60
|
-
const page1 = await context.newPage();
|
|
61
|
-
await page1.goto(`http://localhost:${currentPort}/page1.html`, { waitUntil: 'domcontentloaded' });
|
|
62
|
-
console.log('[TEST] Page loaded');
|
|
63
|
-
|
|
64
|
-
console.log('[TEST] Setting up page event listener...');
|
|
65
|
-
let newPageReceived = false;
|
|
66
|
-
context.on('page', (page) => {
|
|
67
|
-
newPageReceived = true;
|
|
68
|
-
console.log('[EVENT] New page created:', page.url());
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
console.log('[TEST] Clicking link and waiting for new page...');
|
|
72
|
-
try {
|
|
73
|
-
const [newPage] = await Promise.all([
|
|
74
|
-
context.waitForEvent('page', { timeout: 10000 }),
|
|
75
|
-
page1.click('#link1')
|
|
76
|
-
]);
|
|
77
|
-
console.log('[TEST] New page captured:', newPage.url());
|
|
78
|
-
} catch (e) {
|
|
79
|
-
console.log('[TEST] Timeout waiting for new page:', e.message);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
console.log('\n[TEST] Checking pages...');
|
|
83
|
-
const allPages = context.pages();
|
|
84
|
-
console.log(`[TEST] Total pages: ${allPages.length}`);
|
|
85
|
-
allPages.forEach((p, i) => {
|
|
86
|
-
console.log(` Page ${i}: ${p.url()}`);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
console.log(`\n[TEST] New page event received: ${newPageReceived}`);
|
|
90
|
-
console.log(`[TEST] RESULT: ${allPages.length >= 2 ? 'PASS' : 'FAIL'}`);
|
|
91
|
-
|
|
92
|
-
server.close();
|
|
93
|
-
await browser.close();
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
test().then(() => process.exit(0));
|