Robot Framework Download File

本小妞迷上赌 提交于 2019-11-30 13:56:13

The solution is very browser specific. For Chrome, you can tell Chrome where to download files. Choosing a new folder allows you to monitor the status of the download. Also, since you are downloading a PDF, disabling the PDF plugin is necessary to prevent the PDF from being displayed instead of downloaded. Here is a test that worked on my machine using a simple page and PDF file.

*** Settings ***
Test Teardown     Close All Browsers
Library           Selenium2Library
Library           OperatingSystem

*** Test Cases ***
Download PDF
    # 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
    # list of plugins to disable. disabling PDF Viewer is necessary so that PDFs are saved rather than displayed
    ${disabled}    Create List    Chrome PDF Viewer
    ${prefs}    Create Dictionary    download.default_directory=${download directory}    plugins.plugins_disabled=${disabled}
    Call Method    ${chrome options}    add_experimental_option    prefs    ${prefs}
    Create Webdriver    Chrome    chrome_options=${chrome options}
    Goto    http://localhost/download.html
    Click Link    link    # downloads a file
    # wait for download to finish
    ${file}    Wait Until Keyword Succeeds    1 min    2 sec    Download should be done    ${download directory}

*** Keywords ***
Download should be done
    [Arguments]    ${directory}
    [Documentation]    Verifies that the directory has only one folder and it is not a temp file.
    ...
    ...    Returns path to the file
    ${files}    List Files In Directory    ${directory}
    Length Should Be    ${files}    1    Should be only one file in the download folder
    Should Not Match Regexp    ${files[0]}    (?i).*\\.tmp    Chrome is still downloading a file
    ${file}    Join Path    ${directory}    ${files[0]}
    Log    File was successfully downloaded to ${file}
    [Return]    ${file}

Contents of download.html:

<html><body><a href="file.pdf" id="link">Click Here</a></body></html>
${home_dir}         Get Environment Variable    HOME
${download_dir}     Join Path   ${home_dir}     Downloads
${result}    Run Keyword And Return Status  File Should Exist   
${download_dir}/filename.pdf

To check further on the file ${content} Get File ${download_dir}/filename.pdf ${count_file} Get Line Count ${content}

Above can be used for basic assertion For more keywords please check the link http://robotframework.org/robotframework/latest/libraries/OperatingSystem.html

You need to check the MD5 of the file - before download and after download. Both the MD5 should be same.

Assuming file is on Linux machine - before and after download :

  1. Login to linux machine using SSH library
  2. Run the command : #md5sum PATH_TO_FILE
  3. Store the output in a variable.
  4. Compare the variable value

Hope so this information was helpful.

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