mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user