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'
@@ -1352,20 +1355,41 @@ class WebDriver extends Helper {
13521355
13531356 const res = await findFields . call ( this , locator )
13541357 this . debug ( `Uploading ${ file } ` )
1355- assertElementExists ( res , locator , 'File field' )
1356- const el = usingFirstElement ( res )
13571358
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 } ` )
1359+ if ( res . length ) {
1360+ const el = usingFirstElement ( res )
1361+ const tag = await this . browser . execute ( function ( elem ) { return elem . tagName } , el )
1362+ const type = await this . browser . execute ( function ( elem ) { return elem . type } , el )
1363+ if ( tag === 'INPUT' && type === 'file' ) {
1364+ if ( this . options . remoteFileUpload ) {
1365+ try {
1366+ this . debugSection ( 'File' , 'Uploading file to remote server' )
1367+ file = await this . browser . uploadFile ( file )
1368+ } catch ( err ) {
1369+ throw new Error ( `File can't be transferred to remote server. Set \`remoteFileUpload: false\` in config to upload file locally.\n${ err . message } ` )
1370+ }
1371+ }
1372+ return el . addValue ( file )
13651373 }
13661374 }
13671375
1368- return el . addValue ( file )
1376+ const targetRes = res . length ? res : await this . _locate ( locator )
1377+ assertElementExists ( targetRes , locator , 'Element' )
1378+ const targetEl = usingFirstElement ( targetRes )
1379+ const base64Content = base64EncodeFile ( file )
1380+ const fileName = path . basename ( file )
1381+ const mimeType = getMimeType ( fileName )
1382+ return this . browser . execute ( function ( el , base64Content , fileName , mimeType ) {
1383+ var binaryStr = atob ( base64Content )
1384+ var bytes = new Uint8Array ( binaryStr . length )
1385+ for ( var i = 0 ; i < binaryStr . length ; i ++ ) bytes [ i ] = binaryStr . charCodeAt ( i )
1386+ var fileObj = new File ( [ bytes ] , fileName , { type : mimeType } )
1387+ var dataTransfer = new DataTransfer ( )
1388+ dataTransfer . items . add ( fileObj )
1389+ el . dispatchEvent ( new DragEvent ( 'dragenter' , { dataTransfer : dataTransfer , bubbles : true } ) )
1390+ el . dispatchEvent ( new DragEvent ( 'dragover' , { dataTransfer : dataTransfer , bubbles : true } ) )
1391+ el . dispatchEvent ( new DragEvent ( 'drop' , { dataTransfer : dataTransfer , bubbles : true } ) )
1392+ } , targetEl , base64Content , fileName , mimeType )
13691393 }
13701394
13711395 /**
0 commit comments