Watir對中文缺乏原生的支持,就算是最新的版本,使用中文也會出現(xiàn)亂碼、方塊的現(xiàn)象。
Google了許久,最佳的解決方案是來自于:http://jonny131./blog/654160
修改 c:\ruby\lib\ruby\gems\1.8\gems\watir-1.6.5\lib\watir\win32ole.rb 文件中的下面代碼WIN32OLE.codepage = WIN32OLE::CP_UTF8
修改為
WIN32OLE.codepage = WIN32OLE::CP_ACP
PS:只能修改該文件,在調(diào)用Watir的代碼中設(shè)置OLE的codepage測試無效。
另外一種方式來自于:http://www.cnblogs.com/slaughter/archive/2007/10/18/929385.html,http://www./raimundox/archive/2006/01/11/27549.html
1.打開watir.rb
2.在class TextField中加入一個新的method:
def characters_in(value)
index = 0
while index < value.length
len = value[index] > 128 ? 2 : 1
yield value[index, len]
index += len
end
end
3.更改class TextField的doKeyPress( value )方法部分代碼,將下面代碼
——————————————-
for i in 0 .. value.length-1
sleep @container.typingspeed
c = value[i,1]
@container.log ” adding c.chr ” + c
@o.value = @o.value.to_s + c
@o.fireEvent(“onKeyDown”)
@o.fireEvent(“onKeyPress”)
@o.fireEvent(“onKeyUp”)
end
替換為如下代碼
characters_in(value) {|c|
sleep @container.typingspeed
@o.value = @o.value.to_s + c
@o.fireEvent(“onKeyDown”)
@o.fireEvent(“onKeyPress”)
@o.fireEvent(“onKeyUp”)
}
在http:///tracker/index.php?func=detail&aid=3232&group_id=104&atid=489,可以下載到watir_cn的補丁。
這兩種方式測試都是有效的。
發(fā)現(xiàn)無論瀏覽器中頁面的編碼是GBK或者UTF-8,通過OLE傳入IE的都必須為GBK的字符才行。試圖設(shè)置WinOLE的codepage為utf8,并傳入utf8的中文字符完全不可行!因此在使用Watir的時候,得把所有UTF8的中文轉(zhuǎn)換成GBK先。