watir更改winole默认编码的方式
今天遇到的问题:当直接用如下assert的代码进行断言检查时,会报不兼容的字符编码的错误。
# encoding: GBK
require 'watir'
require 'test/unit'
class TC_login < Test::Unit::TestCase
def setup
puts "starting a testcase..."
end
def test_01_login
#使用ie
@ie=Watir::Browser.new
#login
@ie.goto("http://www.fenbi.com")
@ie.text_field(:id,"formview0_email").set("XXXX")
@ie.text_field(:id,"formview0_password").set("XXXX")
@ie.button(:type,"submit").click
#判断是否登录成功check_text="我的首页"
assert(@ie.text.include?"我的首页")
@ie.link(:class,"XXX").fire_event("onmouseover")
#点击弹出的tips中的选项
@ie.link(:class,"logout").click
@ie.close
end
end
代码: assert(@ie.text.include?"我的首页")
错误输出: Encoding::CompatibilityError: incompatible character encodings: UTF-8 and GBK
当时疑惑的是我已经在顶部注明了# encoding: GBK,为什么还会提示说这是不兼容的字符编码?后来在网上查了一些信息才知道watir在执行时是通过winole这个工具来获得ie的控制权及ie的众多属性,在获取ie中页面的文字时,winole会将页面中文本转成代码中字符串,而winole默认的文字编码方式是utf8,自然就提示不兼容了。
于是在ruby的目录下找到win32ole的文件,修改默认文件编码方式是以GBK编码即可,也可根据实际情况调整这两个选项,如下。
require 'win32ole'
#以utf8编码页面文字
WIN32OLE.codepage = WIN32OLE::CP_UTF8
#以gbk编码页面文字
WIN32OLE.codepage = WIN32OLE::CP_ACP

浙公网安备 33010602011771号