-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnightmare-upload.js
53 lines (51 loc) · 1.85 KB
/
nightmare-upload.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
var debug = require('debug')('nightmare:upload');
module.exports = exports = function(Nightmare) {
Nightmare.action('upload',
function(ns, options, parent, win, renderer, done) {
parent.respondTo('upload', function(selector, pathsToUpload, done) {
parent.emit('log', 'paths', pathsToUpload);
try {
//attach the debugger
//NOTE: this will fail if devtools is open
win.webContents.debugger.attach('1.1');
} catch (e) {
parent.emit('log', 'problem attaching', e);
return done(e);
}
win.webContents.debugger.sendCommand('DOM.getDocument', {}, function(err, domDocument) {
win.webContents.debugger.sendCommand('DOM.querySelector', {
nodeId: domDocument.root.nodeId,
selector: selector
}, function(err, queryResult) {
//HACK: chromium errors appear to be unpopulated objects?
if (Object.keys(err)
.length > 0) {
parent.emit('log', 'problem selecting', err);
return done(err);
}
win.webContents.debugger.sendCommand('DOM.setFileInputFiles', {
nodeId: queryResult.nodeId,
files: pathsToUpload
}, function(err, setFileResult) {
if (Object.keys(err)
.length > 0) {
parent.emit('log', 'problem setting input', err);
return done(err);
}
win.webContents.debugger.detach();
done(null, pathsToUpload);
});
});
});
});
done();
},
function(selector, pathsToUpload, done) {
if(!Array.isArray(pathsToUpload)){
pathsToUpload = [pathsToUpload];
}
this.child.call('upload', selector, pathsToUpload, (err, stuff) => {
done(err, stuff);
});
})
}