51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 2263|回复: 2
打印 上一主题 下一主题

Ant+testNG,用ant运行遇到中文乱码,怎么处理

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2016-4-20 16:37:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
有个简单的代码,运行Juint和单独运行testng.xml结果是正常的。
Juinit中

driver.get("https://www.baidu.com/");
System.out.println(driver.findElement(By.xpath("//input[@id='su' and @type='submit']")).getAttribute("value"));
Assert.assertTrue(driver.findElement(By.xpath("//input[@id='su' and @type='submit']")).getAttribute("value").equals("百度一下"));


但是放到Ant中,就异常了。郁闷。

代码基本是一样的。
    @Test
    public void GetLinkText(){
        driver.get("https://www.baidu.com/");
        System.out.println(driver.findElement(By.xpath("//input[@id='su' and @type='submit']")).getAttribute("value"));
        Assert.assertTrue(driver.findElement(By.xpath("//input[@id='su' and @type='submit']")).getAttribute("value").equals("百度一下"));
    }

testng配置xml
  1. <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
  2. <suite name="SeleniumWithTestng" allow-return-values="true" preserve-order="true">
  3.     <test verbose="10" name="test" annotations="JDK">
  4.         <classes>
  5.             <class name="com.test.Units.ChineseTest">
  6.             </class>
  7.         </classes>
  8.     </test>
  9. </suite>
复制代码




Ant配置的buid.xml

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <project basedir="." default="run" name="Selenium demo">
  3.     <echo message="initConfig" />
  4.    
  5.     <echo>Java-Home: ${java.home}</echo>
  6.     <echo>Java-Version: ${java.version}</echo>   
  7.    
  8.     <!--copy file to ${dest.dir}-->
  9.     <property name="dest.dir" value="build" />
  10.     <!--out report to ${dest.report}-->
  11.     <property name="dest.report" value="report" />
  12.     <!--suitconfig-->
  13.     <property name="testng.file" value="ZH.xml"/>
  14.     <property name="full-compile" value="true" />
  15.    
  16.     <!--need to put all jars to a dir-->
  17.     <path id="run.classpath">
  18.         <fileset dir="${basedir}">
  19.                     <include name="libs/*.jar" />
  20.         </fileset>
  21.     </path>
  22.     <target name="init">
  23.         <echo message="work beging" />
  24.         <mkdir dir="${dest.dir}" />
  25.     </target>
  26.    
  27.     <target name="compile" depends="init">
  28.         <echo message="make classes" />

  29.         <javac srcdir="src" destdir="${dest.dir}" encoding="UTF-8" debug="true" fork = "yes" >
  30.                     <classpath refid="run.classpath"/>
  31.         </javac>
  32.     </target>
  33.    
  34.     <taskdef name="testng" classname="org.testng.TestNGAntTask">
  35.         <classpath>
  36.           <pathelement location="libs/testng-6.8.5.jar"/>
  37.         </classpath>
  38.     </taskdef>

  39.     <!--run-->
  40.     <target name="run" depends="compile">
  41.         <testng classpathref="run.classpath" outputDir="${dest.report}">
  42.             <classfileset dir="${dest.dir}" includes="*.class" />
  43.               <classpath>
  44.                 <pathelement path = "${dest.dir}"/>
  45.             </classpath>
  46.             <jvmarg value="-Dfile.encoding=UTF-8"/>   
  47.             <xmlfileset dir="${basedir}" includes="${testng.file}" />
  48.             <sysproperty key="org.uncommons.reportng.title" value="My TestSuits" />
  49.         </testng>
  50.       
  51.     </target>   
  52. </project>
复制代码





是不是ant哪里配置有问题?

运行结果类似这样:



[testng] [Invoker 843627620] Invoking com.test.Units.ChineseTest.GetLinkText
[testng] 百度????



   [testng] FAILED: GetLinkText
   [testng] java.lang.AssertionError
   [testng]     at org.junit.Assert.fail(Assert.java:86)
   [testng]     at org.junit.Assert.assertTrue(Assert.java:41)
   [testng]     at org.junit.Assert.assertTrue(Assert.java:52)
   [testng]     at com.test.Units.ChineseTest.GetLinkText(ChineseTest.java:34)      《==这里是上面的Assert地方


   [testng] The tests failed.


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
 楼主| 发表于 2016-4-20 17:31:25 | 只看该作者
找了一下,既然排查是控制台和ant运行问题,这样就解决了。

在@Test里面加
System.out.println(pro.getProperty("file.encoding"));
//如果不是需要的,则这样
pro.setProperty("file.encoding","utf-8");

不过运行后,整个项目的中文注释都乱码了,要重新加注释,也是麻烦。

[testng] PASSED: GetLinkText



回复 支持 反对

使用道具 举报

  • TA的每日心情
    无聊
    2020-12-8 11:20
  • 签到天数: 605 天

    连续签到: 1 天

    [LV.9]测试副司令

    3#
    发表于 2016-4-20 17:47:05 | 只看该作者
    testng.xml头上加一句<?xml version="1.0" encoding="UTF-8"?>

    还有确保你的项目编码格式是utf-8
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-12 08:26 , Processed in 0.064640 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表