对于文件下载自动化,我有一些不同的方法。
我是这样做的:
要求:
require 'rubygems'
require 'watir-webdriver'
require 'win32ole'
首先创建一个文件大小处理的方法:
def fileinfo(name)
if File.exists?(name)
print "#{name} exists "
bytes = File.size(name)
print "and is #{bytes} in size;"
whenm = File.mtime(name)
print whenm,";"
print whenm.to_i,";"
else
print "#{name} does NOT exist;"
end
end
第二次用预先设置的下载目录驱动chrome:
download_directory = "#{Dir.pwd}/downloads"
download_directory.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?
profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = download_directory
接下来删除文件(从以前的运行中)以测试用例的可重用性和有效性(3 个之一):
%x(DEL /Q C:\\automation\\qa\\downloads\\*.exe)
%x(DEL /Q downloads\\*.exe)
%x(DEL /Q downloads\\*.*)
定义下载组件的大小变量:
contains = Dir.new(download_directory).entries
dlc = contains[2]
dcinfo = fileinfo("downloads/"+dlc)
dlcsize = File.size("downloads/"+dlc)
最后你可以包含验证点:
if dlcsize > 0
puts "File found and is #{dlcsize} bytes."
logfile = open("test_results.csv", "a")
begin
logRow = "#{__FILE__}"
logfile.puts logRow + "," + "Passed".to_s
end
else
puts "Test Failed! File not found either is zero."
logfile = open("test_results.csv", "a")
begin
logRow = "#{__FILE__}"
logfile.puts logRow + "," + "Passed".to_s
end
end