-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheadless.js
More file actions
271 lines (218 loc) · 7.95 KB
/
headless.js
File metadata and controls
271 lines (218 loc) · 7.95 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
var puppeteer = require('puppeteer');
const chromeArgs = [
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-renderer-backgrounding',
'--enable-resource-load-scheduler=false'
];
var headless = module.exports = function(address, user, pass, options, callback) {
console.log(`Clicking around on ${address}`);
var that = this;
this.address = address
this.launchBrowser().then(async browser => {
that.browser = browser;
that.page = await that.launchPage(browser, address);
//await that.setPageActive(that.page)
await that.login(that.page, user, pass);
await that.launchMaintenacePage(that.page);
await that.launchFileManagement(that.page);
if (options.firmwarePath) {
await that.launchFirmwarePage(that.page);
await that.uploadFirmware(that.page, options.firmwarePath);
}
if (options.configPath) {
await that.launchConfigUploadPage(that.page);
await that.uploadConfiguration(that.page, options.configPath);
}
await that.launchCopyFile(that.page);
await that.clickCopyFile(that.page);
callback && callback();
})
.catch(function(e) {console.log(e)});
}
headless.prototype.log = function(message) {
console.log("Address: " + this.address + ", " + message)
}
var getiFrame = headless.prototype.getiFrame = async(page, iFrameName) => {
console.log(`Getting iframe named ${iFrameName}`);
//await page.waitForSelector(`#${iFrameName}`, {visible: true});
var frames = page.frames().filter(frame => { console.log(frame.name()); return frame.name() == iFrameName});
console.log("Found " + frames.length + " frames"); // should always be 0
return frames[0];
}
// this method attempts to tell the page to be active, so that everything is "visible"
// even when the page isn't focused. But it doesn't work. :(
headless.prototype.setPageActive = async(page) => {
session = await page.target().createCDPSession()
await session.send('Page.enable');
await session.send('Page.setWebLifecycleState', {state: 'active'});
await session.detach()
}
headless.prototype.launchFirmwarePage = async(page) => {
var selector = 'tr[id="2070~2160"] > td > a:last';
await page.evaluate(() => {
$(selector).click();
return Promise.resolve()
})
await waitUntilIdle(page);
}
headless.prototype.uploadFirmware = async(page, firmwarePath) => {
var formSelector = "#dlData";
console.log("waiting for frame");
await page.waitForSelector(formSelector, {visible: true});
console.log("iframe visible");
await waitUntilIdle(page);
var frames = page.frames().filter(frame => { console.log(frame.name()); return frame.name() == "dlData"});
console.log("Found " + frames.length + " frames");
var popupFrame = frames[0];
var fileSelector = "#srcFileName";
await popupFrame.waitForSelector(fileSelector, {visible: true});
var uploadInput = await popupFrame.$(selector);
await uploadInput.uploadFile(firmwarePath);
var submit = await popupFrame.$("#defaultButton");
await submit.click();
await waitUntilIdle(page);
await clickOkOnDialog(page);
}
headless.prototype.launchConfigUploadPage = async(page) => {
await page.evaluate(() => {
$('tr[id="2070~2180"] > td > a:last').click();
return Promise.resolve()
})
await waitUntilIdle(page);
}
headless.prototype.uploadConfiguration = async(page, path) => {
var frame = await getiFrame(page, "mainFrame");
var httpframe = await getiFrame(page, "httpData");
var uploadFileNameButtonSelector = "#srcFileName";
var applyButtonSelector = "#defaultButton"
await httpframe.waitForSelector(uploadFileNameButtonSelector, {visible: true});
var fileInput = await httpframe.$(uploadFileNameButtonSelector);
var applyButton = await frame.$(applyButtonSelector);
await fileInput.uploadFile(path);
await applyButton.click();
await clickOkOnDialog(page);
await waitUntilIdle(page);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function waitUntilIdle(page) {
//await sleep (5000);
// if it takes longer than 30 seconds for the network to chill then
// this throws an exception... ignore it
try {
await page.waitForNavigation({
waitUntil: "networkidle2",
})
} catch (e) {}
}
var browser = null
headless.prototype.launchBrowser = async () => {
if (browser == null) {
browser =puppeteer.launch({headless: false, args: chromeArgs});
}
return await browser
}
headless.prototype.launchPage = async (browser, address) => {
console.log("Launching browser page", address);
ctx = await browser.createIncognitoBrowserContext();
var page = await ctx.newPage();
await page.goto(address, {waitUntil: 'networkidle2'})
return page;
}
headless.prototype.login = async(page, user, pass) => {
await page.waitForSelector("input[name=userName]", {visible: true})
console.log(`Logging in as user ${user}`)
var userBox = await page.$("input[name=userName]")
await userBox.focus()
await page.keyboard.type(user);
var passBox = await page.$("#password");
await passBox.focus();
await page.keyboard.type(pass);
var loginBox = await page.$("#btnLogin");
await loginBox.click();
await waitUntilIdle(page);
}
headless.prototype.launchMaintenacePage = async(page) => {
await waitUntilIdle(page);
console.log("clickyclicky on maintenance page link");
var link = await page.$("#maintenancePage");
await link.click();
await waitUntilIdle(page);
}
headless.prototype.launchFileManagement = async(page) => {
console.log("Entering file management")
await waitUntilIdle(page);
// !!! I bet this ID changes between firmawre versions !!!
// Clicks on "Configuratin file copy" on the menu. Assumes we're already in the "Maintenance" tab
var linkId = "tr[id='2070'] > td > table"
await page.waitForSelector(linkId)
var link = await page.$(linkId);
console.log("clickyclicky on file upload page");
await waitUntilIdle(page);
await link.click();
await waitUntilIdle(page);
// await link.click();
// await waitUntilIdle(page);
console.log("done");
}
headless.prototype.launchCopyFile = async(page) => {
console.log("Entering copy file")
await waitUntilIdle(page);
// !!! I bet this ID changes between firmawre versions !!!
var linkId = "[id='2070~2200'] > td > a"
await page.waitForSelector(linkId)
var link = await page.$(linkId);
console.log("clickyclicky on copy file page");
await waitUntilIdle(page);
await link.click();
await waitUntilIdle(page);
// await link.click();
// await waitUntilIdle(page);
console.log("done");
}
// Final step in saving the file - click save!
headless.prototype.clickCopyFile = async(page) => {
var frame = await getiFrame(page, "mainFrame");
// var httpframe = await getiFrame(page, "httpData");
//var uploadFileNameButtonSelector = "#srcFileName";
var applyButtonSelector = "#defaultButton"
//await httpframe.waitForSelector(uploadFileNameButtonSelector, {visible: true});
//var fileInput = await httpframe.$(uploadFileNameButtonSelector);
var applyButton = await frame.$(applyButtonSelector);
//await fileInput.uploadFile(path);
await applyButton.click();
await clickOkOnDialog(page);
await waitUntilIdle(page);
}
// Click the logout link, and shutdown the browser instance
headless.prototype.close = function(callback) {
var that = this;
that.page.$("#lblLogout").then(async (logoutLink) => {
await logoutLink.click();
await clickOkOnDialog(that.page);
await waitUntilIdle(that.page);
await that.browser.close();
callback && callback();
})
.catch(function(e) {
console.log(e);
});
}
var clickOkOnDialog = async(page) => {
var popupSelector = "#frameless > div.tcontent > iframe";
await page.waitForSelector(popupSelector, {visible: true});
await waitUntilIdle(page);
// console.log("pop up");
// var frames = page.frames().filter(frame => { frame.name() == "popup_gw"});
// var popupFrame = frames[0];
popupFrame = await getiFrame(page, "popup_gw");
var okButtonSelector = "#btnOk";
await popupFrame.waitForSelector(okButtonSelector, {visible: true});
var ok = await popupFrame.$(okButtonSelector);
waitUntilIdle(page);
await ok.click();
}
//<% uplink_trunk_vlans.forEach((vlan) => { %> <%= vlan %>
//<% }) %>