Resolve paths against CWD in page.uploadFile() method

This patch:
- teaches page.uploadFile() to resolve given file paths against
  current working directory. This aligns paths handling with all the
  other methods
- moves page.uploadFile() under Frame
- changes test to use relative path for file upload
This commit is contained in:
Andrey Lushnikov
2017-07-28 00:06:57 -07:00
parent 0452644b83
commit d26e2399f2
4 changed files with 26 additions and 8 deletions

View File

@@ -15,6 +15,7 @@
*/
let fs = require('fs');
let path = require('path');
let EventEmitter = require('events');
let helper = require('./helper');
@@ -323,6 +324,21 @@ class Frame {
return new WaitTask(this, predicateCode, polling, timeout).promise;
}
/**
* @param {string} selector
* @param {!Array<string>} filePaths
* @return {!Promise}
*/
async uploadFile(selector, ...filePaths) {
let expression = helper.evaluationString(selector => document.querySelector(selector), selector);
const {result} = await this._client.send('Runtime.evaluate', { expression });
if (!result)
return;
const objectId = result.objectId;
filePaths = filePaths.map(filePath => path.resolve(filePath));
return this._client.send('DOM.setFileInputFiles', { objectId, files: filePaths });
}
/**
* @template T
* @param {string} selector

View File

@@ -590,12 +590,7 @@ class Page extends EventEmitter {
* @return {!Promise}
*/
async uploadFile(selector, ...filePaths) {
let expression = helper.evaluationString(selector => document.querySelector(selector), selector);
const {result} = await this._client.send('Runtime.evaluate', { expression });
if (!result)
return;
const objectId = result.objectId;
return this._client.send('DOM.setFileInputFiles', { objectId, files: filePaths });
return this.mainFrame().uploadFile(selector, ...filePaths);
}
/**