Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@devtools: Enable file uploads by changing input elements' value #5347

Merged
merged 7 commits into from May 2, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions e2e/element.test.js
Expand Up @@ -105,6 +105,12 @@ describe('elements', () => {
expect(await browser.getElementProperty(textarea[ELEMENT_KEY], 'value')).toBe('foobar')
})

it.only('elementSendKeys for file-type input', async () => {
christian-bromann marked this conversation as resolved.
Show resolved Hide resolved
const fileInput = await browser.findElement('css selector', '#upload-test')
await browser.elementSendKeys(fileInput[ELEMENT_KEY], 'README.MD')
christian-bromann marked this conversation as resolved.
Show resolved Hide resolved
expect(await browser.getElementProperty(fileInput[ELEMENT_KEY], 'value')).toBe('C:\\fakepath\\README.MD')
})

it('elementClear', async () => {
const textarea = await browser.findElement('css selector', 'textarea')
await browser.elementClear(textarea[ELEMENT_KEY])
Expand Down
11 changes: 10 additions & 1 deletion packages/devtools/src/commands/elementSendKeys.js
@@ -1,4 +1,5 @@
import { getStaleElementError } from '../utils'
import path from 'path'

export default async function elementSendKeys ({ elementId, text }) {
const elementHandle = this.elementStore.get(elementId)
Expand All @@ -9,7 +10,15 @@ export default async function elementSendKeys ({ elementId, text }) {

await elementHandle.focus()
const page = this.getPageHandle()
await page.keyboard.type(text)
const tagName = await (await elementHandle.getProperty('tagName')).jsonValue()
const type = await (await elementHandle.getProperty('type')).jsonValue()

if(tagName === 'INPUT' && type === 'file'){
const paths = (text || '').split('\n').map(p => path.resolve(p))
await elementHandle.uploadFile(...paths)
} else {
await page.keyboard.type(text)
}

return null
}