---
title: "Watir 1.5.6的版本的安装以及要解决的问题"
author: "lihbobo"
date: "2010-06-09T16:34:52Z"
tags: ["C", "C#", "C++", "Ruby", "rspec"]
categories: ["编程语言"]
slug: "watir-1-5-6"
draft: false
lastmod: "2010-06-09T16:34:52Z"
id: 688138
url: "https://lihbobo.iteye.com/blog/688138"
---

1\. ruby 1.8.6 最高只支持到1.5.4，所以需要:gem update --system

2\. 使用ruby 1.9.X貌似，对中文的支持有问题，没搞定，以后再研究。

3\. 解决输入中文的问题：1.5.6以上不需要

一.问题  
使用watir1.5时候，如果发现它不支持中文，可以尝试修改  
一下它的源代码。

二. 解决方案  
1.打开watir.rb  
2.在class TextField中加入一个新的method，代码如下：  
\-------------------------------------------  
    def characters\_in(value)  
        index = 0  
        while index &lt; value.length  
         len = value\[index] &gt; 128 ? 2 : 1  
         yield value\[index, len]  
         index += len  
        end  
    end  
\-------------------------------------------

3.更改class TextField的doKeyPress( value )方法部分代码，将下面代码注释掉或删除  
\-------------------------------------------  
    for i in 0 .. value.length-1    
        sleep @ieController.typingspeed    
        typing speed  
        c = value\[i,1]  
        @ieController.log  " adding c.chr " + c  .chr.to\_s  
        @o.value = @o.value.to\_s + c  
        fire\_key\_events  
    end  
\-------------------------------------------  
替换为如下代码  
\-------------------------------------------  
    characters\_in(value) {|c|  
        sleep @ieController.typingspeed  
        @o.value = @o.value.to\_s + c  
        fire\_key\_events  
       }  
\-------------------------------------------

4\. 解决上传图片的问题：

1\. 首先修改C:\\ruby\\lib\\ruby\\gems\\1.8\\gems\\watir-1.5.6\\watir\\input\_elements.rb文件

找到FileField类下的set方法,把原来的替换成

`system("rubyw -e \"require 'win32ole'; @autoit=WIN32OLE.new('AutoItX3.Control'); waitresult=@autoit.WinWait '选择文件', '', 15; sleep 1; if waitresult == 1\" -e \"@autoit.ControlSetText '选择文件', '', 'Edit1', '#{setPath}'; @autoit.ControlSend '选择文件', '', 'Button2', '{ENTER}';\" -e \"end\"")`

这样做是为了支持中文.

5\. 检查Watir版本：

```
ruby -e 'require



"watir"; puts Watir::IE::VERSION'




6. 解决多个Watir版本共存的办法， 把不用的Watir版本移走，系统能自动识别。

7. ci_reporter
    gem install reporter

    If you're using Test::Unit, ensure the ci/reporter/rake/test_unit_loader.rb file is loaded before

 the test is run. If you're using RSpec, you‘ll need to pass the following arguments to the spec command:
     --

require

 GEM_PATH/

lib/

ci/

reporter/

rake/

rspec_loader
    
 --

format

 CI::Reporter::RSpec


  You may also want to set the output directory as demonstrated by setting the CI_REPORTS environment variable.
  
  
```

```
require

 'test/unit'


require

 'ci/reporter/rake/test_unit_loader.rb'


require

 'watir'


ENV[

"CI_REPORTS"

]

 = 'C:/temp/'

```

```

```