机器人框架下载文件
Robot Framework Download File
我使用机器人框架。
在我的 HTML 页面上有一个简单的按钮。当您点击它时,它会下载一个 PDF 文件。
如果文件已下载,我如何检查 Robot Framework?
解决方案是特定于浏览器的。对于 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>
您需要检查文件的 MD5 - 下载前和下载后。
两个MD5应该相同。
假设文件在 Linux 机器上 - 下载前后:
- 使用 SSH 库登录 linux 机器
- 运行 命令:#md5sum PATH_TO_FILE
- 将输出存储在变量中。
- 比较变量值
希望这些信息对您有所帮助。
${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}
以上可用于基本断言
更多关键字请查看link http://robotframework.org/robotframework/latest/libraries/OperatingSystem.html
一种方法是在点击下载之前捕获并存储时间戳,点击下载后,继续检查文件夹是否存在时间戳晚于存储的文件(在点击下载之前Tstamp)。当您不知道文件名时,此方法有效。
即使您知道该文件,当运行再次测试用例时,文件夹中存在名为 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
以下为测试结果(模拟下载)
我正在使用 FOR IN 对每个文件进行大量处理(当您不知道大小以及下载它需要多少时间时)。
例如
${range} Set Variable 9999999
FOR ${s} IN RANGE ${range}
${status} Run Keyword And Return Status File Should Not Exist ${path}\*.zip.part
Sleep 60
因此脚本每 60 秒检查一次下载是否完成(没有 *.zip.part(对于 FF 浏览器)特别是下载文件夹)然后退出循环。
我使用机器人框架。
在我的 HTML 页面上有一个简单的按钮。当您点击它时,它会下载一个 PDF 文件。
如果文件已下载,我如何检查 Robot Framework?
解决方案是特定于浏览器的。对于 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>
您需要检查文件的 MD5 - 下载前和下载后。 两个MD5应该相同。
假设文件在 Linux 机器上 - 下载前后:
- 使用 SSH 库登录 linux 机器
- 运行 命令:#md5sum PATH_TO_FILE
- 将输出存储在变量中。
- 比较变量值
希望这些信息对您有所帮助。
${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}
以上可用于基本断言 更多关键字请查看link http://robotframework.org/robotframework/latest/libraries/OperatingSystem.html
一种方法是在点击下载之前捕获并存储时间戳,点击下载后,继续检查文件夹是否存在时间戳晚于存储的文件(在点击下载之前Tstamp)。当您不知道文件名时,此方法有效。
即使您知道该文件,当运行再次测试用例时,文件夹中存在名为 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
以下为测试结果(模拟下载)
我正在使用 FOR IN 对每个文件进行大量处理(当您不知道大小以及下载它需要多少时间时)。 例如
${range} Set Variable 9999999
FOR ${s} IN RANGE ${range}
${status} Run Keyword And Return Status File Should Not Exist ${path}\*.zip.part
Sleep 60
因此脚本每 60 秒检查一次下载是否完成(没有 *.zip.part(对于 FF 浏览器)特别是下载文件夹)然后退出循环。