forked from allquixotic/SO-ChatBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-headless-webdriver.js
148 lines (138 loc) · 4.49 KB
/
run-headless-webdriver.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
147
148
var cleanupAndExit = function(exitCode) {
var exit = function() {
console.log('Exiting.');
process.exit(exitCode);
};
console.log('Cleaning up...');
setTimeout(function() {
console.warn('Failed to clean up in 5000ms. Some browser instances may remain.');
exit();
}, 5000);
if (client) {
if (dumpBrowserConsoleLoop) {
clearInterval(dumpBrowserConsoleLoop);
}
client.end().then(function() {
console.log('Cleaned up.');
exit();
}).catch(function(err) {
console.error(err.name, err.message);
console.warn('Cleanup failed. Some browser instances may remain.');
exit();
});
} else {
exit();
}
};
var signals = {
'SIGHUP': 1,
'SIGINT': 2,
'SIGTERM': 15,
'SIGBREAK': 21
};
for (var signal in signals) {
process.on(signal, function() {
console.log(signal + ' caught');
cleanupAndExit(128 + signals[signal]); // posix signal 128 + signal number
});
}
process.on('uncaughtException', function(err) {
console.error(err);
cleanupAndExit(1);
});
var config = require('./run-headless.config.json');
var webdriverio = require('webdriverio');
var client = webdriverio.remote(config.driverOptions);
var logLevels = {
'SEVERE': console.error,
'WARNING': console.warn,
'INFO': console.info,
'DEBUG': console.debug
};
var dumpBrowserConsole = function(limit) {
client.log('browser').then(function(logs) {
(limit ? logs.value.slice(-limit) : logs.value).forEach(function(log) {
(logLevels[log.value] || console.log).call(console, log.message);
});
});
};
var dumpBrowserConsoleLoop = null;
// phantomjs does not clear browser logs correctly
// dumping in a loop floods console cause they repeat
//var dumpBrowserConsoleLoop = setInterval(dumpBrowserConsole, 5000);
client
.init()
.url(config.loginUrl)
.getUrl()
.then(function(url) {
console.log('Loaded ' + url);
})
.getText('=log in')
.then(function() {
console.log('Logging in...');
return client
.execute(function() {
openid.signin('stack_exchange');
})
.waitForExist('#affiliate-signin-iframe', 10000)
.frame('affiliate-signin-iframe')
.waitForExist('#email', 10000)
.setValue('#email', config.email)
.setValue('#password', config.password)
.submitForm('.login-form form')
.frame()
.getUrl()
.then(function(url) {
console.log('Login submitted; loaded ' + url);
});
}, function(err) {
console.log('Already logged in; skipping login');
})
.url(config.roomUrl)
.getUrl()
.then(function(url) {
console.log('Loaded chatroom ' + url);
})
.execute(function(scriptUrl) {
var script = document.createElement('script');
script.src = scriptUrl;
script.onload = function() {
//bot.activateDevMode();
console.log('Loaded bot');
bot.adapter.out.add('I have just been restarted! This happens daily automatically, or when my owner restarts me. Ready for commands.');
};
document.head.appendChild(script);
}, config.scriptUrl)
.then(function() {
console.log('Chatbot injected');
})
.then(function() {
var readline = require('readline');
var repl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
repl.on('SIGINT', function() {
process.emit('SIGINT');
});
repl.setPrompt('> ');
console.log('You are now in a REPL with the remote page. Have fun!');
var ready = true;
repl.on('line', function(data) {
console.log('Attempting to run "' + data + '"');
ready = false;
client.executeAsync(function(code) {
return eval(code);
}, data).then(function(ret) {
console.log('$', ret.value);
}).catch(function(err) {
console.log('!', err.name, err.message);
}).finally(function() {
// because phantomjs does not clear browser logs correctly
// we only display the last 5 to avoid flooding
dumpBrowserConsole(5);
repl.prompt();
});
});
repl.prompt();
});