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

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的数组第一个元素是根据操作系统及浏览器的文件上传弹窗而修改的

 

 

 

 

转载于:https://www.cnblogs.com/Javame/p/3830235.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值