From 787e8065666324a7fafbcf23e8e798905446ecfb Mon Sep 17 00:00:00 2001 From: Allen Kleiner Date: Wed, 19 Jul 2017 14:57:34 -0700 Subject: [PATCH 1/2] Add uploadFile for ChromePageObject --- ghostjs-core/src/chrome/index.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/ghostjs-core/src/chrome/index.js b/ghostjs-core/src/chrome/index.js index 25846cd..46d39d5 100644 --- a/ghostjs-core/src/chrome/index.js +++ b/ghostjs-core/src/chrome/index.js @@ -214,6 +214,24 @@ class ChromePageObject { } } + async uploadFile(selector, filePath) { + this.getCDP().then(async (client) => { + const { Page, Runtime, DOM } = client + try { + await Page.enable() + await DOM.enable() + await Runtime.enable() + + const { objectId } = await Runtime.evaluate({ expression: `document.querySelector(${selector})`}) + const node = await DOM.requestNode(objectId) + + await DOM.setFileInputFiles({ files: [filePath], nodeId: node }) + } catch (err) { + console.error(err) + } + }) + } + close () { this.getCDP().then(async (client) => { if (this.targetId) { From 481e615ab8eaa385c5985817c22c03eba3a0a999 Mon Sep 17 00:00:00 2001 From: Allen Kleiner Date: Wed, 19 Jul 2017 15:24:23 -0700 Subject: [PATCH 2/2] Fix the linter --- ghostjs-core/src/chrome/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ghostjs-core/src/chrome/index.js b/ghostjs-core/src/chrome/index.js index 46d39d5..6db0bf8 100644 --- a/ghostjs-core/src/chrome/index.js +++ b/ghostjs-core/src/chrome/index.js @@ -214,7 +214,7 @@ class ChromePageObject { } } - async uploadFile(selector, filePath) { + async uploadFile (selector, filePath) { this.getCDP().then(async (client) => { const { Page, Runtime, DOM } = client try { @@ -222,7 +222,7 @@ class ChromePageObject { await DOM.enable() await Runtime.enable() - const { objectId } = await Runtime.evaluate({ expression: `document.querySelector(${selector})`}) + const { objectId } = await Runtime.evaluate({ expression: `document.querySelector(${selector})` }) const node = await DOM.requestNode(objectId) await DOM.setFileInputFiles({ files: [filePath], nodeId: node })