How to download a pdf file automatically in chrome browser using Robot Framework

风流意气都作罢 提交于 2019-12-25 16:08:55

问题


As a part of my test , i want to download a pdf file from browser automatically as soon as it loads on browser. Can someone guide me how this can be achieved.Following is the way that i have tried.I have tried clicking the download button but it didn't work for me. Following is the screenshot that appears on browser .

*** Test Cases ***

    Download and deploy content package 

        ${output}=   Run keyword   job history
        Log to console   ${output}
        # create unique folder
        ${now}    Get Time    epoch
        ${download directory}    Join Path    ${OUTPUT DIR}    downloads_${now}
        Create Directory    ${download directory}
        ${chrome options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
        ${prefs}    Create Dictionary    prompt_for_download=false  download.default_directory=${download directory}    
        Call Method    ${chrome options}    add_experimental_option    prefs    ${prefs}
        Create Webdriver    Chrome    chrome_options=${chrome options}
        Goto   ${output}   
        Sleep       5

回答1:


I have quite simple workaround for file downloads. It has following rules:

  • Create download folder in suite setup

    Set Global Variable    ${global_downloadDir}    ${CURDIR}\\Downloads\\${suite_orgName}
    ${chromeOptions}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
    ${prefs} =    Create Dictionary    download.default_directory=${global_downloadDir}
    Call Method    ${chromeOptions}    add_experimental_option    prefs    ${prefs}
    Create Webdriver    Chrome    chrome_options=${chromeOptions}
    
  • Clear the folder on test teardown

    Run Keyword And Ignore Error    Empty Directory    ${global_downloadDir}
    

    With above, you'll be easily able to manipulate downloaded files.




回答2:


Although above answer is good but for me following code worked.

Conference Summary
     ${output}=   Run keyword   SLO conformance summary
    # create unique folder in current directory
    ${current_date}=    Get Current Date 
    ${folder_name} =    Convert Date    ${current_date}    result_format=${SUITE NAME}.%d.%m.%Y_%H.%M
    ${download directory}    Join Path    ${OUTPUT DIR}    ${folder_name}
    Create Directory    ${download directory}
    ${options}  Evaluate  sys.modules['selenium.webdriver'].ChromeOptions()  sys, selenium.webdriver
    Call Method  ${options}  add_argument  --start-maximized
    ${preferences} =    Create Dictionary   prompt_for_download=false  download.default_directory=${download directory}    download.directory_update=True
    Call Method  ${options}  add_experimental_option  prefs  ${preferences}
     Create WebDriver  Chrome  chrome_options=${options} 
    Go To    ${output}     
    sleep   4
    Page Should Not Contain Element  //div[@class='errorIcon']
    Run Keyword     Get export position


来源:https://stackoverflow.com/questions/44156753/how-to-download-a-pdf-file-automatically-in-chrome-browser-using-robot-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!