关于selenium自动化测试过程中汉字出现乱码的解决方式

上一篇 / 下一篇  2012-11-12 10:04:36 / 个人分类:WEB

博客已经迁移到http://www.dl41.com 
     最近在使用selenium做WEB自动化测试,但发现了一个比较恼火的问题,就是我们如果在自动化过程中输入内容,或者页面上的标签是汉字的话,在录制完成运行脚本的时候,存在汉字的那行脚本代码会报错。
      本人使用的是Perl+selenium  RC  来运行的测试脚本。在找了网上是资料后发现几种方式都不行。

先附上:找到的两种解决方式 (进过验证对我的环境都无效,)

       方法1:启动selenium  RC的时候附上参数
#################################################################################
最开始用 java -jar selenium-server.jar,未加任何参数,结果就出现乱码问题,加上-Dfile.encoding="Unicode" 参数则问题解决,注意是Unicode,经测试UTF-8也不行,不知是否和我操作系统的字体有关,我是win7操作系统。

解决乱码的完整启动命令行 :

java -jar selenium-server.jar -Dfile.encoding="Unicode"
################################################################################

       方法2:将汉字转换成unicode 
#################################################################################
use strict;
use warnings;
use Encode qw/encode decode/;
use Time::HiRes qw(sleep);
use Test::WWW::Selenium;
use Test::More "no_plan"; 
use Test::Exception;

#步骤3:创建selenium会话,打开百度主页
    our $sel = Test::WWW::Selenium->new(
           host        => "localhost",
           port        => "4444",
           browser     => "*chrome",
           browser_url => "http://www.baidu.com");
   $sel->open_ok("/");
  

my $chinese_text = "新 闻";
my $decode_text = decode("cp936",$chinese_text);//此处转换为啥是CP936?,一直不明白
$sel->click_ok("link=$decode_text");
################################################################################
经IE查看页面编码格式是unicode(utf-8),多次尝试最后将上面两中方式结合,将输入内容转换为utf-8
终于成功通过脚本的运行
====================================================================
use strict;
use warnings;
use Time::HiRes qw(sleep);
use Test::WWW::Selenium;
use Test::More "no_plan";
use Test::Exception;
use Encode;//使用encode 进行格式转码

my $sel = Test::WWW::Selenium->new( host => "localhost",
                                    port => 4444,
                                    browser => "*chrome",
                                    browser_url => "http://192.168.1.93:8888/EIMSAdmin/Home/Login" );

$sel->open_ok("/EIMSAdmin/Home/Login");
my $logincalss= "管理员登陆";
$logincalss = decode("utf-8",$logincalss);//将汉字强制转换成utf-8格式的放到变量$logincalss中
$sel->select_ok("id=type", "label=$logincalss");
$sel->type_ok("id=UserName", "liuhubo");
$sel->type_ok("id=Password", "123456");
$sel->click_ok("css=input.userLoginBtn");
$sel->wait_for_page_to_load_ok("30000");


TAG:

测试小龙套的个人空间 引用 删除 测试小龙套   /   2012-11-16 21:03:04
多谢
测试小龙套的个人空间 引用 删除 测试小龙套   /   2012-11-16 21:02:48
1
 

评分:0

我来说两句

Open Toolbar