11let webdriverio
22
3+ import fs from 'fs'
34import assert from 'assert'
45import path from 'path'
56import crypto from 'crypto'
@@ -24,6 +25,8 @@ import {
2425 modifierKeys ,
2526 normalizePath ,
2627 resolveUrl ,
28+ getMimeType ,
29+ base64EncodeFile ,
2730} from '../utils.js'
2831import { isColorProperty , convertColorToRGBA } from '../colorUtils.js'
2932import ElementNotFound from './errors/ElementNotFound.js'
@@ -33,6 +36,7 @@ import { highlightElement } from './scripts/highlightElement.js'
3336import { focusElement } from './scripts/focusElement.js'
3437import { blurElement } from './scripts/blurElement.js'
3538import { dontSeeElementError , seeElementError , seeElementInDOMError , dontSeeElementInDOMError } from './errors/ElementAssertion.js'
39+ import { dropFile } from './scripts/dropFile.js'
3640import { dontSeeTraffic , seeTraffic , grabRecordedNetworkTraffics , stopRecordingTraffic , flushNetworkTraffics } from './network/actions.js'
3741import WebElement from '../element/WebElement.js'
3842
@@ -1352,20 +1356,33 @@ class WebDriver extends Helper {
13521356
13531357 const res = await findFields . call ( this , locator , context )
13541358 this . debug ( `Uploading ${ file } ` )
1355- assertElementExists ( res , locator , 'File field' )
1356- const el = usingFirstElement ( res )
13571359
1358- // Remote Upload (when running Selenium Server)
1359- if ( this . options . remoteFileUpload ) {
1360- try {
1361- this . debugSection ( 'File' , 'Uploading file to remote server' )
1362- file = await this . browser . uploadFile ( file )
1363- } catch ( err ) {
1364- throw new Error ( `File can't be transferred to remote server. Set \`remoteFileUpload: false\` in config to upload file locally.\n${ err . message } ` )
1360+ if ( res . length ) {
1361+ const el = usingFirstElement ( res )
1362+ const tag = await this . browser . execute ( function ( elem ) { return elem . tagName } , el )
1363+ const type = await this . browser . execute ( function ( elem ) { return elem . type } , el )
1364+ if ( tag === 'INPUT' && type === 'file' ) {
1365+ if ( this . options . remoteFileUpload ) {
1366+ try {
1367+ this . debugSection ( 'File' , 'Uploading file to remote server' )
1368+ file = await this . browser . uploadFile ( file )
1369+ } catch ( err ) {
1370+ throw new Error ( `File can't be transferred to remote server. Set \`remoteFileUpload: false\` in config to upload file locally.\n${ err . message } ` )
1371+ }
1372+ }
1373+ return el . addValue ( file )
13651374 }
13661375 }
13671376
1368- return el . addValue ( file )
1377+ const targetRes = res . length ? res : await this . _locate ( locator )
1378+ assertElementExists ( targetRes , locator , 'Element' )
1379+ const targetEl = usingFirstElement ( targetRes )
1380+ const fileData = {
1381+ base64Content : base64EncodeFile ( file ) ,
1382+ fileName : path . basename ( file ) ,
1383+ mimeType : getMimeType ( path . basename ( file ) ) ,
1384+ }
1385+ return this . browser . execute ( dropFile , targetEl , fileData )
13691386 }
13701387
13711388 /**
0 commit comments