-
Notifications
You must be signed in to change notification settings - Fork 0
/
pageCmd.js
75 lines (66 loc) · 2.44 KB
/
pageCmd.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const puppeteer = require("puppeteer-extra");
const fs = require('fs');
module.exports = async function pageCmd(commands) {
//let screenshot_base64;
const browser = await puppeteer.launch();
const page = await browser.newPage();
page.setViewport({width: 1366, height: 768});
//await page.goto(url);
let response;
for (const command of commands) {
let cmd = command.cmd;
let val = command.val;
//console.log(cmd, val);
// RUN
if (cmd === "goto") {
//await page.setRequestInterception(true);
response = await page.goto(val);
const chain = response.request().redirectChain();
//console.log(chain.length); // 0
console.log("<p>" + chain.length + "<p>"); // 0
}
if (cmd === "goto_wait") {
response = await page.goto(val, {waitUntil: 'domcontentloaded'});
}
if (cmd === "click") {
await page.click(val);
}
if (cmd === "focus") {
await page.click(val);
}
if (cmd === "screenshot") {
await page.screenshot({path: val});
}
if (cmd === "screenshot_pdf") {
//createPDFStream
await page.pdf({path: val});
}
if (cmd === "screenshot_base64") {
//const base64 = await page.screenshot({ encoding: "base64" })
//return await page.screenshot({ encoding: "base64" });
//console.log('<img src="data:image/png;base64,' + await page.screenshot({ encoding: "base64" }) + '" alt="' + val + '"/> ');
return await page.screenshot({ encoding: "base64" }).then(function(data){
let base64Encode = `data:image/png;base64,${data}`;
//console.log('<p>' + val + '</p>' + '<img src="' + base64Encode + '" alt="' + val + '"/> ');
//console.log('<p>' + val + '</p>');
//process.exit();
browser.close();
return base64Encode;
});
}
if (cmd === "wait") {
await page.waitForTimeout(val);
}
if (cmd === "write_from_file") {
const value = fs.readFileSync(val, "utf8");
await page.keyboard.type(value);
}
if (cmd === "write") {
await page.keyboard.type(val);
}
if (cmd === "wait_browser") {
await page.waitForNavigation();
}
}
browser.close();
}