This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (113 loc) · 3.97 KB
/
index.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
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
/**
* A little script to take screenshots of demo.bhi.ma using headless chrome. It
* will loop through all values in the "PAGES_TO_SCREENSHOT" to generate screen
* shots of the application.
*
* AUTHOR: jniles
* LICENSE: MIT
*/
require('dotenv').config();
const puppeteer = require('puppeteer-core');
const webpage = process.env.SITE;
const NETWORK_OPTS = {
waitUntil: ['domcontentloaded', 'networkidle0'],
timeout: 60000,
};
// 1080p
const VIEWPORT = {
width: 1366,
height: 768,
};
const CHROME_OPTS = {
headless: true,
executablePath: process.env.CHROME_BIN,
};
const CREDENTIALS = {
username: process.env.BH_USERNAME,
password: process.env.BH_PASSWORD,
};
// pages to screenshot
// TODO(@jniles) - make filenames without spaces - potentially define the names here. They
// are a pain to work with on the web - have to use encodeURIComponent() to get them to work.
const PAGES_TO_SCREENSHOT = [
{ url: '#!/patients/register', name: 'Patient Registration' },
{ url: '#!/invoices/patient', name: 'Invoicing' },
{ url: '#!/vouchers/complex', name: 'Complex Vouchers' },
{ url: '#!/vouchers/simple', name: 'Simple Vouchers' },
{ url: '#!/accounts', name: 'Account Management' },
{ url: '#!/debtors/groups/create', name: 'Debtor Group Creation' },
{ url: '#!/enterprises', name: 'Enterprise Management ' },
];
function delay(seconds) {
return new Promise((resolve) => { setTimeout(() => resolve(), seconds * 1000); });
}
(async () => {
try {
const browser = await puppeteer.launch(CHROME_OPTS);
// page setup
const page = await browser.newPage();
await page.setViewport(VIEWPORT);
await page.emulateMedia('screen');
console.log(`Going to: ${webpage}.`);
await page.goto(webpage, NETWORK_OPTS);
// intially take the screenshot of the login page since we can't come back here.
console.log('Got to login page! Taking a screenshot..');
await page.screenshot({ path: 'screenshots/login.png', fullPage: true, type: 'png' });
console.log('screenshot saved.');
console.log('Reloading with debug information...');
await page.evaluate(() => {
window.angular.reloadWithDebugInfo();
});
await delay(35);
console.log('done.');
console.log('Logging into server...');
await page.evaluate(login, CREDENTIALS);
console.log('Done!');
// loop through each URL, taking screenshots of it.
for (const route of PAGES_TO_SCREENSHOT) {
await navigateAndScreenshot(page, route);
}
console.log('All done! Closing chrome ...');
await browser.close();
} catch (error) {
console.error(error);
}
})();
/**
* @function login
*
* @description
* This function is slightly hacky, as it manipulates angular's digest loop
* to register values. Oh well. Logs the browser in.
*/
function login(creds) {
const qs = window.document.querySelector.bind(window.document);
const { angular } = window;
const $element = angular.element(qs('form[name="LoginForm"]'));
const $scope = $element.scope();
const $ctrl = $element.controller();
angular.extend($ctrl.credentials, creds);
$scope.$digest();
qs('button[type=submit]').click();
// make sure we wait for the thing to load.
return new Promise((resolve) => { setTimeout(() => resolve(), 5000); });
}
/**
* @function navigateAndScreenshot
*
* @descripton
* This function navigates to awebpage and takes a full page PNG screenshot. It expects
* the route to define the value of the path. Stores all values in a screenshots/ directory.
*/
const navigateAndScreenshot = (async (page, route) => {
try {
console.log(`[page] Navigating to ${webpage}${route.url}.`);
await page.goto(`${webpage}${route.url}`, NETWORK_OPTS);
await delay(15);
const fname = `screenshots/${route.name.trim()}.png`;
await page.screenshot({ path: fname, fullPage: true, type: 'png' });
console.log(`[page] Screenshot of ${route.url} saved to ${fname}.`);
} catch (e) {
console.error(`ERR [route] ${route.name}: ${e.message}`);
}
});