-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathtest-headless-toggle.cjs
More file actions
154 lines (131 loc) · 5.22 KB
/
test-headless-toggle.cjs
File metadata and controls
154 lines (131 loc) · 5.22 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env node
/**
* Test headless mode toggle functionality
*
* Tests:
* 1. Chrome starts in headless mode by default
* 2. Can switch to headed mode
* 3. Can switch back to headless mode
* 4. getBrowserMode() returns correct state
* 5. Tabs are preserved across mode switches
*/
const chromeLib = require('./skills/browsing/chrome-ws-lib.js');
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function runTests() {
console.log('Testing headless mode toggle functionality...\n');
try {
// Test 1: Start Chrome (should be headless by default)
console.log('Test 1: Start Chrome in headless mode (default)');
await chromeLib.startChrome();
await sleep(2000);
let mode = await chromeLib.getBrowserMode();
console.log(` Mode: ${JSON.stringify(mode)}`);
if (mode.headless === true && mode.mode === 'headless' && mode.running === true) {
console.log(' ✓ Pass: Chrome started in headless mode\n');
} else {
console.log(' ✗ FAIL: Expected headless mode\n');
process.exit(1);
}
// Test 2: Navigate to a page
console.log('Test 2: Navigate to a test page');
await chromeLib.navigate(0, 'https://example.com');
await sleep(1000);
const tabs = await chromeLib.getTabs();
console.log(` Tabs open: ${tabs.length}`);
console.log(` Current URL: ${tabs[0].url}`);
if (tabs.length >= 1 && tabs[0].url.includes('example.com')) {
console.log(' ✓ Pass: Page loaded\n');
} else {
console.log(' ✗ FAIL: Page did not load\n');
process.exit(1);
}
// Test 3: Switch to headed mode
console.log('Test 3: Switch to headed mode');
console.log(' WARNING: This will show a browser window briefly');
const showResult = await chromeLib.showBrowser();
console.log(` Result: ${showResult}`);
await sleep(2000);
mode = await chromeLib.getBrowserMode();
console.log(` Mode: ${JSON.stringify(mode)}`);
if (mode.headless === false && mode.mode === 'headed') {
console.log(' ✓ Pass: Switched to headed mode\n');
} else {
console.log(' ✗ FAIL: Expected headed mode\n');
process.exit(1);
}
// Test 4: Verify tabs were restored
console.log('Test 4: Verify tabs were restored after mode switch');
const tabsAfterShow = await chromeLib.getTabs();
console.log(` Tabs after switch: ${tabsAfterShow.length}`);
if (tabsAfterShow.length >= 1) {
const hasExampleTab = tabsAfterShow.some(t => t.url.includes('example.com'));
if (hasExampleTab) {
console.log(' ✓ Pass: Tab restored (page reloaded via GET)\n');
} else {
console.log(' ⚠ Warning: Tab not found, but this is expected if Chrome failed to reopen it\n');
}
} else {
console.log(' ⚠ Warning: No tabs after mode switch\n');
}
// Test 5: Switch back to headless mode
console.log('Test 5: Switch back to headless mode');
const hideResult = await chromeLib.hideBrowser();
console.log(` Result: ${hideResult}`);
await sleep(2000);
mode = await chromeLib.getBrowserMode();
console.log(` Mode: ${JSON.stringify(mode)}`);
if (mode.headless === true && mode.mode === 'headless') {
console.log(' ✓ Pass: Switched back to headless mode\n');
} else {
console.log(' ✗ FAIL: Expected headless mode\n');
process.exit(1);
}
// Test 6: Verify idempotency - switching when already in target mode
console.log('Test 6: Test idempotency (hide when already headless)');
const hideAgainResult = await chromeLib.hideBrowser();
console.log(` Result: ${hideAgainResult}`);
if (hideAgainResult.includes('already')) {
console.log(' ✓ Pass: Correctly detected already in headless mode\n');
} else {
console.log(' ⚠ Warning: Should have detected already in headless mode\n');
}
// Test 7: Test starting Chrome explicitly in headed mode
console.log('Test 7: Kill and restart Chrome explicitly in headed mode');
await chromeLib.killChrome();
await sleep(1000);
await chromeLib.startChrome(false); // false = headed
await sleep(2000);
mode = await chromeLib.getBrowserMode();
console.log(` Mode: ${JSON.stringify(mode)}`);
if (mode.headless === false && mode.mode === 'headed') {
console.log(' ✓ Pass: Started Chrome in headed mode\n');
} else {
console.log(' ✗ FAIL: Expected headed mode\n');
process.exit(1);
}
// Cleanup
console.log('Cleanup: Killing Chrome');
await chromeLib.killChrome();
await sleep(500);
console.log('\n✅ All tests passed!');
console.log('\nKey findings:');
console.log('- Chrome defaults to headless mode');
console.log('- Mode switching works correctly');
console.log('- getBrowserMode() reports accurate state');
console.log('- Tabs are reopened after mode switch (via GET requests)');
console.log('- ⚠️ WARNING: Mode switching LOSES POST-based page state\n');
} catch (error) {
console.error(`\n✗ Test failed with error: ${error.message}`);
console.error(error.stack);
// Try to cleanup
try {
await chromeLib.killChrome();
} catch (e) {
// Ignore cleanup errors
}
process.exit(1);
}
}
runTests();