• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ruby + watir 自动化上传图片文件解决方案

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

watir自动化捕获上传图片元素:

require 'watir'
include Watir
require 'test/unit'
class TC_recorded < Test::Unit::TestCase
    def test_recorded
        puts "First Line"         
        ie=Watir::IE.new 
        puts "Open IE"
        ie.goto("http://localshot:8082")
ie.file_field(:name,'file').set("C:\\wy.jpg")

执行脚本,报错!原因是什么呢?

看这面这段代码:input_elements.rb

class FileField < InputElement 
   #:stopdoc: 
   INPUT_TYPES = ["file"] 
   POPUP_TITLES = ['Choose file', 'Choose File to Upload'] 
   #:startdoc: 
    
   # set the file location in the Choose file dialog in a new process 
   # will raise a WatirException if AutoIt is not correctly installed 
   def set(path_to_file) 
     assert_exists 
     require 'watir/windowhelper' 
     WindowHelper.check_autoit_installed 
     begin 
       Thread.new do 
         sleep 1 # it takes some time for popup to appear 
 
         system %{ruby -e ' 
             require "win32ole" 
 
             @autoit = WIN32OLE.new("AutoItX3.Control") 
             time    = Time.now 
 
             while (Time.now - time) < 15 # the loop will wait up to 15 seconds for popup to appear 
               #{POPUP_TITLES.inspect}.each do |popup_title| 
                 next unless @autoit.WinWait(popup_title, "", 1) == 1 
 
                 @autoit.ControlSetText(popup_title, "", "Edit1", #{path_to_file.inspect}) 
                 @autoit.ControlSend(popup_title, "", "Button2", "{ENTER}") 
                 exit 
               end # each 
             end # while 
         '} 
       end.join(1) 
     rescue 
       raise Watir::Exception::WatirException, "Problem accessing Choose file dialog" 
     end 
     click 
   end 
 end 
  POPUP_TITLES = ['Choose file', 'Choose File to Upload'] 
  Choose file ! 本地化的问题.

怎么修改呢?

POPUP_TITLES = ['选择要加载的文件', 'Choose File to Upload']

     这个titles的数组第一个元素是根据操作系统及浏览器的文件上传弹窗而修改的

 

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Ruby(2008-02-07)发布时间:2022-07-13
下一篇:
Why Would a .Net Programmer Learn Ruby On Rails(翻译)发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap