机器人框架下载文件

新手上路,请多包涵

我使用机器人框架。

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

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

原文由 Raphael 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 268
2 个回答

该解决方案是非常特定于浏览器的。对于 Chrome,您可以告诉 Chrome 从哪里下载文件。选择新文件夹可让您监控下载状态。此外,由于您正在下载 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>

原文由 ombre42 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

原文由 Royalyn H 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题