-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (66 loc) · 2.36 KB
/
index.js
File metadata and controls
77 lines (66 loc) · 2.36 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
var webdriver = require("selenium-webdriver");
var util = require("./lib/common/util");
var fs = require("fs");
var path = require("path");
var launch = {};
launch.launch = function(options) {
options = util.update({
browser: "chrome",
webdriver_url: "http://localhost:4444/wd/hub",
events_url: null,
page_url: null
}, options);
var caps = {
"acceptSslCerts": true,
"browserName": options.browser
};
console.log("Connecting to webdriver", options.webdriver_url, caps);
var driver = new webdriver.Builder()
.usingServer(options.webdriver_url)
.withCapabilities(caps)
.build();
var is_file = options.events_url.indexOf("file:") === 0;
var promise = driver.getSession().then(function(session) {
// The `wdsid` and `wdurl` parameters have names that the WebdriverJS
// library looks for.
var query_params = {
wdsid: session.getId(),
wdurl: options.webdriver_url,
cyclops_events: is_file ? "js" : options.events_url
};
var url = util.appendQuery(options.page_url, query_params);
console.log("Navigating " + caps.browserName + " to " + url);
return driver.get(url);
});
// If the URL is a local file, read it in and upload it to the JS
// once the page is loaded.
if (is_file) {
// Read it in, a newline-separate list of events
var filename = decodeURIComponent(options.events_url.split("://")[1]);
var lines = fs.readFileSync(filename, "utf8").split("\n");
var events = [];
lines.forEach(function(line) {
line = line.trim();
if (line !== "") {
events.push(JSON.parse(line));
}
});
console.log("Read in " + events.length + " events from " + filename);
// Send a massive request via webdriver to set it in the JS
// TODO: break up into event chunks so we can support really large lists
promise = promise.then(function() {
console.log("Uploading events to browser");
return driver.executeAsyncScript(
"var callback = arguments[arguments.length - 1];\n" +
"var cyclops_data = {};\n" +
"cyclops_data.events = JSON.parse(arguments[0]);\n" +
"window._$cyclops = cyclops_data;\n" +
"callback();\n",
JSON.stringify(events));
}).then(function() {
console.log("Finished uploading events to browser");
});
}
return promise;
};
module.exports = launch;