机器人框架下载文件

时间:2016-02-09 17:08:17

标签: html testing download robotframework

我使用机器人框架。

在我的HTML页面上,我有一个简单的按钮。当您单击它时,它会下载pdf文件。

如果文件已下载,我如何查看Robot Framework?

韩国社交协会

我找到了一个解决方案,请转到@ ombre42:

${SERVER}                  ${SERVER_DEV}
${NAME}                 Robot
${FILE_NAME}                Robot.pdf
${CLASS_NAME}               in
${DOWNLOAD_DIRECTORY}       C:\\robot_download

Scenario: User can download
    Create Directory    ${DOWNLOAD_DIRECTORY}
    ${CHROME_OPTIONS}=  Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()   sys, selenium.webdriver
    ${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    ${SERVER}
    Click Element ${NAME}
    Wait Until Element Is Visible   css=div.${CLASS_NAME}   8
    Page Should Contain ${NAME}
    Set Selenium Speed  10s
    Download PDF    ${NAME}
    File Should Exist   C:\\robot_download\\${FILE_NAME}

3 个答案:

答案 0 :(得分:15)

该解决方案非常适合浏览器。对于Chrome,您可以告诉Chrome在哪里下载文件。选择新文件夹可以监视下载的状态。此外,由于您正在下载PDF,因此需要禁用PDF插件以防止显示PDF而不是下载PDF。这是一个使用简单页面和PDF文件在我的机器上运行的测试。

*** 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}

download.html的内容:

<html><body><a href="file.pdf" id="link">Click Here</a></body></html>

答案 1 :(得分:2)

${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

进一步检查文件     $ {content}获取文件$ {download_dir} /filename.pdf     $ {count_file}获取行数$ {content}

以上可用于基本断言 有关更多关键字,请查看链接http://robotframework.org/robotframework/latest/libraries/OperatingSystem.html

答案 2 :(得分:0)

您需要在下载之前和下载之后检查文件的MD5。 MD5都应该相同。

假设文件在Linux机器上 - 下载之前和之后:

  1. 使用SSH库登录Linux机器
  2. 运行命令:#md5sum PATH_TO_FILE
  3. 将输出存储在变量中。
  4. 比较变量值
  5. 希望这些信息有用。