机器人框架下载文件 [英] Robot Framework Download File
问题描述
我使用Robot Framework。
I use Robot Framework.
在我的HTML页面上,我有一个简单的按钮。当您点击它时,它会下载一个pdf文件。
On my HTML page I have a simple button. When you click on it, it downloads a pdf file.
如果文件已下载,我该如何检查Robot Framework?
How can I check with Robot Framework if the file has been downloaded ?
Tks
我找到了一个解决方案,转到@ ombre42:
I found a solution, tks to @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}
推荐答案
解决方案是非常具体的浏览器。对于Chrome,您可以告诉Chrome在哪里下载文件。选择一个新文件夹允许您监视下载的状态。此外,由于您正在下载PDF,因此禁用PDF插件是为了防止PDF被显示而不是下载。这是一个在我的机器上使用简单的页面和PDF文件的测试。
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}
download.html的内容:
Contents of download.html:
<html><body><a href="file.pdf" id="link">Click Here</a></body></html>
这篇关于机器人框架下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!