-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheadlessBot.js
More file actions
169 lines (151 loc) · 5.17 KB
/
headlessBot.js
File metadata and controls
169 lines (151 loc) · 5.17 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const fs = require('fs');
const { chromium, devices } = require('playwright');
const TARGET = 'https://cside-assesment.vercel.app';
const OUTFILE = 'bot-test-results.json';
async function grabDiagnostics(page) {
try {
await page.waitForTimeout(500);
const json = await page.evaluate(() => {
if (window.__BOT_DETECTION_OUTPUT__) return window.__BOT_DETECTION_OUTPUT__;
const pre = document.querySelector('pre');
if (pre) {
try { return JSON.parse(pre.innerText); } catch (e) { return { raw: pre.innerText }; }
}
return { summary: undefined, signals: [] };
});
return json;
} catch (err) {
return { error: String(err) };
}
}
function reportScenario(results, name) {
return { scenario: name, timestamp: new Date().toISOString(), result: results };
}
async function doHumanInteraction(page) {
for (let i = 0; i < 30; i++) {
await page.mouse.move(100 + Math.random() * 300, 100 + Math.random() * 200, { steps: 3 });
if (i % 5 === 0) await page.mouse.click(120 + Math.random() * 200, 120 + Math.random() * 100);
await page.waitForTimeout(40 + Math.random() * 80);
}
await page.keyboard.type('hello test', { delay: 50 });
await page.waitForTimeout(300);
}
async function doPerfectRegularMotion(page, events = 100, intervalMs = 20) {
for (let i = 0; i < events; i++) {
const x = 10 + (i % 50) * 5;
const y = 10 + Math.floor(i / 50) * 5;
await page.mouse.move(x, y);
await page.waitForTimeout(intervalMs);
}
}
async function runScenario({ name, launchOptions = {}, contextOptions = {}, initScript = null, actions = null, waitAfter = 7000 }) {
const browser = await chromium.launch(launchOptions);
const context = await browser.newContext(contextOptions);
const page = await context.newPage();
if (initScript) await page.addInitScript(initScript);
await page.goto(TARGET, { waitUntil: 'domcontentloaded' });
if (actions) {
try {
await actions(page);
} catch (e) {
console.error(`[${name}] action error`, e);
}
}
await page.waitForTimeout(waitAfter);
const diag = await grabDiagnostics(page);
await browser.close();
return reportScenario(diag, name);
}
(async () => {
const results = [];
results.push(await runScenario({
name: 'human-headful-baseline',
launchOptions: { headless: false },
actions: async (page) => {
await doHumanInteraction(page);
},
waitAfter: 3000
}));
results.push(await runScenario({
name: 'headless-default-noinput',
launchOptions: { headless: true },
actions: null,
waitAfter: 6000
}));
results.push(await runScenario({
name: 'headless-force-webdriver',
launchOptions: { headless: true },
initScript: `Object.defineProperty(navigator, 'webdriver', { get: () => true }); window.__AUTOMATION_INJECTED__ = true;`,
actions: null,
waitAfter: 6000
}));
results.push(await runScenario({
name: 'headless-ua-spoof',
launchOptions: { headless: true },
contextOptions: { userAgent: 'HeadlessChrome/1.0 (automated)' },
actions: null,
waitAfter: 6000
}));
results.push(await runScenario({
name: 'headless-perfect-regular-motion',
launchOptions: { headless: true },
actions: async (page) => {
await doPerfectRegularMotion(page, 200, 12);
},
waitAfter: 1000
}));
results.push(await runScenario({
name: 'headless-automation-globals',
launchOptions: { headless: true },
initScript: `
window.__nightmare = true;
window.__selenium_unwrapped = true;
window.__driver_evaluate = true;
try { Object.defineProperty(navigator, 'webdriver', { get: () => true }); } catch(e) {}
`,
waitAfter: 6000
}));
results.push(await runScenario({
name: 'headless-rapid-resize-blur',
launchOptions: { headless: true },
actions: async (page) => {
await page.evaluate(() => {
window.resizeTo(300,300); window.dispatchEvent(new Event('resize'));
window.resizeTo(1200,800); window.dispatchEvent(new Event('resize'));
window.dispatchEvent(new Event('blur'));
window.dispatchEvent(new Event('focus'));
});
},
waitAfter: 4000
}));
results.push(await runScenario({
name: 'mobile-emulation-touch',
launchOptions: { headless: true },
contextOptions: { ...devices['iPhone 13'] },
actions: async (page) => {
await page.touchscreen.tap(100, 200);
await page.evaluate(() => window.dispatchEvent(new Event('gesturestart')));
},
waitAfter: 4000
}));
results.push(await runScenario({
name: 'headless-slow-network',
launchOptions: { headless: true },
actions: async (page) => {
await page.route('**/*', route => route.continue());
},
waitAfter: 9000
}));
results.push(await runScenario({
name: 'headless-force-flag-query-param',
launchOptions: { headless: true },
actions: async (page) => {
await page.goto(TARGET + '?forceAutomation=true', { waitUntil: 'domcontentloaded' });
},
waitAfter: 4000
}));
fs.writeFileSync(OUTFILE, JSON.stringify({ target: TARGET, results }, null, 2));
console.log('Saved results to', OUTFILE);
console.log(JSON.stringify({ target: TARGET, results }, null, 2));
process.exit(0);
})();