【问题标题】:Robot Framework Download File机器人框架下载文件
【发布时间】:2016-05-19 18:53:14
【问题描述】:

我使用机器人框架。

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

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

【问题讨论】:

  • 您使用的是哪种浏览器?

标签: html testing download robotframework


【解决方案1】:

该解决方案是特定于浏览器的。对于 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>

【讨论】:

  • IE 可以做到吗?
【解决方案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

【讨论】:

    【解决方案3】:

    您需要检查文件的 MD5 - 下载前和下载后。 两者的 MD5 应该相同。

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

    1. 使用 SSH 库登录 linux 机器
    2. 运行命令:#md5sum PATH_TO_FILE
    3. 将输出存储在变量中。
    4. 比较变量值

    希望这些信息对您有所帮助。

    【讨论】:

      【解决方案4】:

      一种方法是在单击下载之前捕获并存储时间戳,在单击下载之后,如果存在时间戳晚于存储的时间戳的文件(在单击下载之前),请继续检查文件夹。这在您不知道文件名时有效。

      即使你知道该文件,再次运行测试用例时,文件夹中存在名为download(1).pdf的文件。

      *** Settings ***
      # Library    Selenium2Library
      Library   checkdownload.py
      *** Test Cases ***
      Download file Check TC
          [Documentation]  TC to Check file download button is working
          ${now}    Get Time    epoch
          #Click on download button #add here
          Sleep    30s
          ${res}    Check Download    ${now}
          Log    ${res}
      

      创建一个py文件并添加机器人测试套件

      #checkdownload.py
      import os
      import glob
      import getpass
      from datetime import datetime
      
      def Check_download(timestamp):
          user_name=getpass.getuser()
          default_download_path=r'/Users/'+user_name+'/Downloads/*' #for mac
          # default_download_path=r'C:\Users\\'+user_name+'\Downloads\*' #windows
      
          files_list=glob.glob(default_download_path) 
          for i in files_list:
              if(timestamp < os.path.getctime(i)):
                  return i
          return False
      

      以下是测试结果(模拟下载)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-04-02
        • 1970-01-01
        • 2021-02-15
        • 2016-07-22
        • 2018-03-13
        • 2013-02-13
        • 1970-01-01
        • 2021-11-23
        相关资源
        最近更新 更多