Created
November 21, 2017 21:45
-
-
Save Bunk/6c384fbe15556f7702d32aa8aa7a7521 to your computer and use it in GitHub Desktop.
Nightwatch — Upload local files to remote selenium grid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path') | |
const util = require('util') | |
const events = require('events') | |
const archiver = require('archiver') | |
const handleResult = cb => result => { | |
if (result.status !== 0) throw new Error(result.value.message) | |
cb(result.value) | |
} | |
function uploadLocalFile () { events.EventEmitter.call(this) } | |
util.inherits(uploadLocalFile, events.EventEmitter) | |
/** | |
* uploadLocalFile is responsible for using webdriver protocol to upload a local | |
* file to a remote Selenium server for use in testing uploads. | |
* | |
* @argument filePath Local path to the file used for uploading | |
* @argument inputSelector Input selector for the file input to upload with | |
*/ | |
uploadLocalFile.prototype.command = function uploadLocalFile (filePath, inputSelector) { | |
const self = this | |
const Nightwatch = this.client | |
const api = this.api | |
const uploadRemote = cb => { | |
let buffers = [] | |
let zip = archiver('zip') | |
zip | |
.on('data', data => { buffers.push(data) }) | |
.on('error', err => { throw err }) | |
.on('finish', () => { | |
const file = Buffer.concat(buffers).toString('base64') | |
api.session(session => { | |
const opt = { | |
path: `/session/${session.sessionId}/file`, | |
method: 'POST', | |
data: { file } | |
} | |
Nightwatch.runProtocolAction(opt, handleResult(cb)).send() | |
}) | |
}) | |
const name = path.basename(filePath) | |
zip.file(filePath, { name }) | |
zip.finalize() | |
} | |
uploadRemote(tempUrl => { | |
api.setValue(inputSelector, tempUrl, () => self.emit('complete')) | |
}) | |
return self | |
} | |
module.exports = uploadLocalFile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rosariosm still not working for me for firefox. Working fine for chrome.