简体   繁体   中英

Setting default file upload folder (directory) in robot framework

We've a web application wherein it uploads attachments ( Files - JPG , PDF or any other ). Right now to upload these files, the files need to be kept in folder : C:\\Users\\username so that the chrome browser automatically uploads them during test automation execution.

We want to upload JPG files from a custom and generic folder so that upload is independent of the path. Is there any way wherein we specify the chrome to choose a specific folder to upload these files ( ex : To upload files from folder C:\\TestData ) independent of the user logged in ?

So my issue is how to set the default file upload path in Chrome Browser so that all the files can be picked-up for uploading from that default directory (folder).

My Attempt:

  1. Launched the chrome browser in automation script.
  2. In Chrome : "Setting" then searched for "download"
  3. Then changed the "Downloads" location to the generic path.

After the relaunch of the chrome browser through script, still it continued to refer to C:\\Users\\username rather than custom path specified in chrome settings.

Screenshot attached below for reference.

在此处输入图片说明

Every time selenium starts a browser, it does so with a new profile - thus when you change the settings in it, they are not present the next time the automation is ran.

Here's how to set the browser's download location - the folder where any streamed is saved (I'm stressing in this, cause you seem to use upload/download interchangeably). You create a special dictionary of settings, and pass that when creating the driver:

${chromeOptions}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
${prefs} =    Create Dictionary    download.default_directory=c:\\your\\directory
Call Method    ${chromeOptions}    add_experimental_option    prefs    ${prefs}
Create Webdriver    Chrome    chrome_options=${chromeOptions}

Obviously, the setting you are looking for is the download.default_directory one.

Finally found a solution using Open Browser Keyword to change the download directory

   ${prefs} =    Create Dictionary    download.default_directory=C:\\Balaji
   Open Browser    https://www.google.com/    chrome    
   options=add_experimental_option("prefs",${prefs})
   Maximize Browser Window
   Set Browser Implicit Wait    20

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM