1717 * under the License.
1818 */
1919
20- import { delimiter } from 'path' ;
20+ import { delimiter , resolve } from 'path' ;
21+ import Fs from 'fs' ;
2122
2223import * as Rx from 'rxjs' ;
2324import { mergeMap , map , takeUntil } from 'rxjs/operators' ;
@@ -37,6 +38,7 @@ import { Executor } from 'selenium-webdriver/lib/http';
3738import { getLogger } from 'selenium-webdriver/lib/logging' ;
3839import { installDriver } from 'ms-chromium-edge-driver' ;
3940
41+ import { REPO_ROOT } from '@kbn/dev-utils' ;
4042import { pollForLogEntry$ } from './poll_for_log_entry' ;
4143import { createStdoutSocket } from './create_stdout_stream' ;
4244import { preventParallelCalls } from './prevent_parallel_calls' ;
@@ -50,6 +52,13 @@ const certValidation: string = process.env.NODE_TLS_REJECT_UNAUTHORIZED as strin
5052const SECOND = 1000 ;
5153const MINUTE = 60 * SECOND ;
5254const NO_QUEUE_COMMANDS = [ 'getLog' , 'getStatus' , 'newSession' , 'quit' ] ;
55+ const downloadDir = resolve ( REPO_ROOT , 'target/functional-tests/downloads' ) ;
56+ const chromiumDownloadPrefs = {
57+ prefs : {
58+ 'download.default_directory' : downloadDir ,
59+ 'download.prompt_for_download' : false ,
60+ } ,
61+ } ;
5362
5463/**
5564 * Best we can tell WebDriver locks up sometimes when we send too many
@@ -112,6 +121,7 @@ async function attemptToCreateCommand(
112121 chromeCapabilities . set ( 'goog:chromeOptions' , {
113122 w3c : true ,
114123 args : chromeOptions ,
124+ ...chromiumDownloadPrefs ,
115125 } ) ;
116126 chromeCapabilities . set ( 'unexpectedAlertBehaviour' , 'accept' ) ;
117127 chromeCapabilities . set ( 'goog:loggingPrefs' , { browser : 'ALL' } ) ;
@@ -145,6 +155,10 @@ async function attemptToCreateCommand(
145155 edgeOptions . setEdgeChromium ( true ) ;
146156 // @ts -ignore internal modules are not typed
147157 edgeOptions . setBinaryPath ( edgePaths . browserPath ) ;
158+ const options = edgeOptions . get ( 'ms:edgeOptions' ) ;
159+ // overriding options to include preferences
160+ Object . assign ( options , chromiumDownloadPrefs ) ;
161+ edgeOptions . set ( 'ms:edgeOptions' , options ) ;
148162 const session = await new Builder ( )
149163 . forBrowser ( 'MicrosoftEdge' )
150164 . setEdgeOptions ( edgeOptions )
@@ -175,6 +189,14 @@ async function attemptToCreateCommand(
175189 firefoxOptions . set ( 'moz:firefoxOptions' , {
176190 prefs : { 'devtools.console.stdout.content' : true } ,
177191 } ) ;
192+ firefoxOptions . setPreference ( 'browser.download.folderList' , 2 ) ;
193+ firefoxOptions . setPreference ( 'browser.download.manager.showWhenStarting' , false ) ;
194+ firefoxOptions . setPreference ( 'browser.download.dir' , downloadDir ) ;
195+ firefoxOptions . setPreference (
196+ 'browser.helperApps.neverAsk.saveToDisk' ,
197+ 'application/comma-separated-values, text/csv, text/plain'
198+ ) ;
199+
178200 if ( headlessBrowser === '1' ) {
179201 // See: https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Headless_mode
180202 firefoxOptions . headless ( ) ;
@@ -298,6 +320,9 @@ export async function initWebDriver(
298320 log . verbose ( entry . message ) ;
299321 } ) ;
300322
323+ // create browser download folder
324+ Fs . mkdirSync ( downloadDir , { recursive : true } ) ;
325+
301326 // download Edge driver only in case of usage
302327 if ( browserType === Browsers . ChromiumEdge ) {
303328 edgePaths = await installDriver ( ) ;
0 commit comments