-
Notifications
You must be signed in to change notification settings - Fork 9
/
run-page.js
59 lines (43 loc) · 1.64 KB
/
run-page.js
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
import runChrome from './run-chrome.ts';
import connectSocket from './connect-socket.js';
export default async (url='http://sheshbesh.nikfrank.com/')=>{
// runChrome(PORT, url), which will bear a promise for when it closes
// this may entail window.addEventListener('close-chrome')
const chrome = runChrome(url);
// wait for chrome to be listening
// probably should listen to stdout instead
await (new Promise(f=> setTimeout(f, 900)));
// connectSocket(PORT, url)
// make a request to http://localhost:PORT/json
// read .webSocketDebuggerUrl from the item with the correct url
// connect to the socket
const { socket, runCommand } = await connectSocket(url);
// now that there is a connection instance,
// export functions:
// - navigate
// - runJS
const pageDriver = {
navigate: url => runCommand('Page.navigate', { url }),
runJS: (expression, awaitPromise=false) =>
runCommand('Runtime.evaluate', { expression, awaitPromise })
.then(res => res.result.value),
screenshot: ()=> runCommand('Page.captureScreenshot'),
click: selector=> runCommand('Runtime.evaluate', {
expression : 'document.querySelector("'+selector+'").click()'
}),
clickSVG: selector=> pageDriver.runJS(`
(event => {
event.initEvent("click",true,true);
document.querySelector("${selector}").dispatchEvent(event);
})(document.createEvent("SVGEvents"));
`),
dblclickSVG: selector=> pageDriver.runJS(`
(event => {
event.initEvent("dblclick",true,true);
document.querySelector("${selector}").dispatchEvent(event);
})(document.createEvent("SVGEvents"));
`),
chrome,
};
return pageDriver;
};