发布新日志

  • ant-使用yuicompressor对JS文件进行压缩

    2010-02-03 15:04:52

    ant-使用yuicompressor对JS文件进行压缩

    主要ANT脚本内容如下:

    <property name="yuicompressor" value="./yuicompressor-2.4.2.jar" />

           <target name="compressjs" >
                    <echo> start to compress js files </echo>
                    <apply executable="java" parallel="false" verbose="true" dest="${js.dir}">
                            <fileset dir="${js.dir}">
                                    <include name="abc.js" />
                            </fileset>
                            <arg line="-jar" />
                            <arg path="${yuicompressor}" />
                            <arg line="--charset utf-8" />
                            <arg value="-o"/>
                            <targetfile />
                            <mapper type="glob" from="*.js" to="*_123456789.js" />
                    </apply>

                    <move todir="${js.dir}" includeemptydirs="false">
                        <fileset dir="${js.dir}">
                          <include name="**/*_123456789.js"/>
                        </fileset>
                        <mapper type="glob" from="*_123456789.js" to="*.js"/>
                      </move>
       </target>

    yuicompressor-2.4.2.rar(787 KB)

    解压后,取得里面的jar包;

  • ant中启动、停止selenium-server-zt

    2009-03-13 15:53:12

    转自:http://wiki.javascud.org/display/SEL/Selenium-RC+and+Continuous+Integration

    本页面是关于在一个持续集成的系统中使用 Selenium-RC - 通过命令行、Ant 或者 TestNG 来运行 Selenium 测试。

    首先,我们收集信息。然后我们把它们合理的整理好。关键问题是有很多种方式启动测试并且收集结果,  所以你不得不配合好你所使用的工具 (ANT, TestNG, CC, ...)

    如此使用 Selenium-RC 就需要了解很多事情:

    • Selenium 服务器 (selenium-server.jar) 是实际上启动 Web 浏览器的程序。这非常重要,因为如果你想通过命令行参数传递参数给浏览器,你就要通过改变服务器的环境来做。
    • 使用 Xvfb (X Windows Virtual Frame. Buffer):如果你想让 Selenium 运行在 Unix 服器上 - 而不需要使用 X Windows 显示 - 或者你不想看到 Web 浏览器弹出,使用 xvfb。这是一个仅运行在内存里面的 X Server。

    ANT

    启动服务器用的 target : 

    <java jar="${selenium-server.jar}" fork="true" spawn="true" />

     停止服务器用的 target (引自 Forum thread) :

    <target name="stop-server">
        <get taskname="selenium-shutdown" src="http://localhost:4444/selenium-server/driver/?cmd=shutDown"
            dest="result.txt" ignoreerrors="true" />
        <echo taskname="selenium-shutdown" message="DGF Errors during shutdown are expected" />
    </target>

    用于启动 Selenese 测试的 target (引自 Forum thread) : 

    <target name="runSeleniumTests">
        <java jar="${acceptanceTestLibDir}/selenium-server.jar" fork="true">
            <arg line="-htmlSuite &quot;${firefox}&quot;"/>
            <arg line="&quot;${baseURL}&quot;"/>
            <arg line="&quot;${acceptanceTestListDir}/testSuite.html&quot;"/>
            <arg line="&quot;${acceptanceTestReportDir}/results.html&quot;"/>
            <arg line="-timeout 30"/>
        </java>
    </target>

    一个完整的例子 :

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="Run Test" default="run_test" basedir=".">
    
        <property name="test.dir" value="src\test" />
    
        <property name="testLibDir" value="lib" />
    
        <path id="run.cp">
            <pathelement path="build"/>
            <fileset dir="build/">
                <include name="*.jar"/>
            </fileset>
            <pathelement path="lib"/>
            <fileset dir="lib/">
                <include name="*.jar"/>
            </fileset>
        </path>
    
        <target name="run_test" description="Start Proxy ; Run TestNG ; stop Proxy">
            <parallel>
                <antcall target="start-server"></antcall>
                <sequential>	
                    <echo taskname="waitfor" message="Wait for proxy server launch" />
                    <waitfor maxwait="2" maxwaitunit="minute" checkevery="100">
                        <http url="http://localhost:4444/selenium-server/driver/?cmd=testComplete"/>
                    </waitfor>
                        <antcall target="run_testNG"></antcall>
                        <antcall target="stop-server"></antcall>
                </sequential>
            </parallel>
        </target>
    
        <target name="run_testNG" description="Run TestNG">	
            <testng classpathref="run.cp" haltOnfailure="false">
                <xmlfileset dir="." includes="testng.xml" />
            </testng>
        </target>
    
        <target name="start-server">
            <java jar="lib/selenium-server.jar" fork="true">
                <arg line="-timeout 30"/>
                <jvmarg value="-Dhttp.proxyHost=proxy.corporate.com"/>
                <jvmarg value="-Dhttp.proxyPort=3128"/>
            </java>
        </target>
    
        <target name="stop-server">
            <get taskname="selenium-shutdown" 
                src="http://localhost:4444/selenium-server/driver/?cmd=shutDown"	
                dest="result.txt" ignoreerrors="true" />
            <echo taskname="selenium-shutdown" message="DGF Errors during shutdown are expected" />
        </target>
    
        <taskdef resource="testngtasks" classpath="lib/testng-5.0-jdk15.jar" />
    
    </project>

    ANT 用户 (欢迎反馈信息) : jattardi ; alxdark ; mustaphakhan ; jmheneman. ANT+CC : paulocheque ; lance.li2005 ...

    MAVEN 2

    想要在集成测试 前/后 启动/停止 Selenium RC,编辑 pom.xml 包含下面的内容: (引自 Maven 用户列表 此帖 )。

    <?xml version="1.0"?>
    <project>
        <dependencies>
            <dependency>
                <groupId>org.openqa.selenium.client-drivers</groupId>
                <artifactId>selenium-java-client-driver</artifactId>
                <version>${selenium-version}</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.openqa.selenium.server</groupId>
                <artifactId>selenium-server</artifactId>
                <version>${selenium-version}</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>dependency-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy</id>
                            <phase>pre-integration-test</phase>
                            <goals>
                                <goal>copy</goal>
                            </goals>
                            <configuration>
                                <artifactItems>
                                    <artifactItem>
                                        <groupId>org.openqa.selenium.server</groupId>
                                        <artifactId>selenium-server</artifactId>
                                        <version>${selenium-version}</version>
                                        <type>jar</type>
                                        <outputDirectory>${project.build.directory}</outputDirectory>
                                        <destFileName>selenium-server.jar</destFileName>
                                    </artifactItem>
                                </artifactItems>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>start-selenium</id>
                            <phase>pre-integration-test</phase>
                            <configuration>
                                <tasks>
                                    <echo taskname="selenium" message="---------------------------------------------------------"/>
                                    <echo taskname="selenium" message=" Starting Selenium Remote Control                        "/>
                                    <echo taskname="selenium" message=" Please make sure that FireFox executable is on the PATH "/>
                                    <java jar="${project.build.directory}/selenium-server.jar" fork="yes" spawn="true"/>
                                    <echo taskname="selenium" message="---------------------------------------------------------"/>                                
                                </tasks>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>stop-selenium</id>
                            <phase>post-integration-test</phase>
                            <configuration>
                                <tasks>
                                    <echo taskname="selenium" message="---------------------------------------------------------"/>
                                    <echo taskname="selenium" message=" Shutting down Selenium Remote Control                   "/>
                                    <echo taskname="selenium" message=" DGF Errors during shutdown are expected                 "/>
                                    <get taskname="selenium"
                                         src="http://localhost:4444/selenium-server/driver/?cmd=shutDown"
                                         dest="result.txt" ignoreerrors="true"/>
                                    <echo taskname="selenium" message="---------------------------------------------------------"/>
                                </tasks>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>  
            </plugins>
        </build>
    
        <repositories>
            <repository>
                <id>openqa</id>
                <url>http://maven.openqa.org</url>
            </repository>
        </repositories>
    
        <properties>        
            <selenium-version>0.8.1</selenium-version>
        </properties>
    </project>

    Maven2 用户: Binil Thomas

    JAVA

    启动服务器的另一种方法 (引自 Forum thread) :

    当然,把 selenium-server.jar 加到 classpath 中去,创建一个 SeleniumServer 对象并 .start() 它。

     这典型应用在 TestNG 的 SetUp() 中。

    LINUX 

    这里有一系列的有用的 Unix 命令做这件事:

     启动 Xvfb 作为 display 1:

    % startx -- `which Xvfb` :1 -screen 0 1024x768x24 2>&1 >/dev/null &

    启动一个 Selenium Server 并且并在此 display (display 1) 上启动 Web 浏览器:

    % DISPLAY=:1 java -jar selenium-server.jar

    截取 xvfb display 下的一个屏幕快照看看发生了什么 (调试模式下很有用):

    % DISPLAY=:1 import -window root /tmp/xvfb_snapshot.png

    注意: '%' 是 Unix shell 提示符。'DISPLAY=:1" 是一个命令行设置的环境变量。你可以很简单的设置 DISPLAY 到例如 foo.bar.com:2,代表 Selenium Web 浏览器会出现在 foo.bar.com 的第二个 X Windows display 上。

    Linux 用户 : obinho.

     

  • ant中启动selenium-server

    2009-03-13 13:26:34

    ant中启动selenium-server的方法如下:

     <target name="env-init">
      <java jar="${basedir}/selenium-server.jar" fork="true" spawn="true">
       <arg value="-multiwindow" />
       <arg value="-firefoxProfileTemplate"/>
       <arg path="${basedir}\veq1hvu2.default"/>
      </java>
     </target>

  • ant中<emma>的使用

    2009-03-12 17:56:35

    参考文章:

    http://www.miiceic.org.cn/07/0706/070603/200701291038429.asp
    http://dohkoos.name/ant-series-emma-measuring-test-coverage-with-the-question-of-when.html

    emma下载:http://emma.sourceforge.net/

    安装emma:把emma.jar、emma_ant.jar两个包放到ANT_HOME\lib下

    emma-2.0.5312.zip(676 KB)


    使用到的代码段如下:

     <path id="classpath">
      <pathelement location="${coverage.instr}"/>  //这句要在classpath的第一行
      <pathelement location="${classes}" />
     </path>


     <path id="emma.lib" >
      <pathelement location="${ant_lib}\emma.jar" />
      <pathelement location="${ant_lib}\emma_ant.jar" />
     </path>

    //在ANT脚本中加入EMMA这个TASK
     <taskdef resource="emma_ant.properties" classpathref="emma.lib" />

     <!--coverage instrument-->
     <target name="coverage.instrument" depends="compile">
      <emma enabled="yes">
       <instr instrpath="${classes}" destdir="${coverage.instr}" metadatafile="${coverage.report.dir}
    \metadata.emma" merge="true" >
       </instr>
      </emma>
     </target>

    //下面sysproperty与jvmarg的功能相同,两种方式都可以使用
     <!-- run junit test-->
     <target name="test" depends="coverage.instrument">
      <junit printsummary="on" haltonfailure="false" fork="true" failureproperty="tests.failed" showoutput="true">
       <!--<sysproperty key="emma.coverage.out.file" value="${coverage.report.dir}\coverage.emma" />-->
       <classpath refid="classpath" />
       <jvmarg value="-Demma.coverage.out.file=${coverage.report.dir}\coverage.emma" />
       <jvmarg value="-Demma.coverage.out.merge=true" />
     
       <formatter type="xml" />
       <batchtest todir="${junit.report.dir}" >
        <fileset dir="${classes}">
         <include name="**/TestCore.class"/>
        </fileset>
       </batchtest>
      </junit>
     </target>

    //生成报告,并以metrics为失败或成功的指标
     <!--coverage report-->
     <target name="coverageReport" depends="test">
      <emma enabled="yes">
       <report sourcepath="${src.java}" sort="+block,+name,+method,+class" 
    metrics="method:70,block:80,line:80,class:100">
        <fileset dir="${coverage.report.dir}">
         <include name="*.emma"/>
        </fileset>
        <html utfile="${coverage.report.dir}\coverage.html" depth="method" />
       </report>
      </emma>
     </target>

     

     

  • ant中<mail>task的使用

    2009-03-11 17:03:48

    ant中<mail>task的使用

    使用mail这个task时,如果出现错误:

    error message:problem while sending mime mail

    注意是否有把mail.jar及activation.jar两个包放到ANT_HOME\lib下
    mail.jar来源:http://java.sun.com/products/javamail/
    activation.jar来源:http://java.sun.com/products/javabeans/glasgow/jaf.html

    mail_lib.rar(441 KB)

    如果还不行,就注意下使用的邮件服务器,SMTP连接时,是否需要认证
    有些要求要的:mail后要增加属性:user="XXX" password="XXX"
    有些不要求的:mail后不要user及password

    还可以通过:
    ant -v buildtask来运行需要测试的task,-v参数会把运行过程中的详细信息打印出来,从中就可以看到相应的错误信息,再进行问题定位

     

    mail的使用示例:

     <target name="sendEmail">
      <zip destfile="${report.dir}\html\Report.zip">
       <zipfileset dir="${report.dir}\html" includes="**\*.*"  excludes="*.zip" />
      </zip>
      <mail mailhost="smtp.163.cn" mailport="25" subject="Test Ant">
       <from address="XXX"/>
       <to address="XXX" />
       <message>The test has completed</message>
       <attachments>
        <fileset dir="${report.dir}\html">
         <include name="*Report.zip" />
        </fileset>
       </attachments>
      </mail>
     </target>

  • eclipse+ant+junit问题二

    2009-03-10 17:00:52

    参考文章:http://dohkoos.name/eclipse-environment-combining-ant-automatically-junit-tests-bis.html

    执行<junit>这个task时,出现错误如下时:

    java.lang.ClassNotFoundException: TestCore
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at org.eclipse.ant.internal.ui.antsupport.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
    at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:423)
    at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:137)

     

    出现原因:

    Ant 不会去加载TestCore。而只是加载了junit库,junit再去加载TestCore,由于java 加载采用的是delegation 模式,而且junit 和TestCore不在同一个空间,所以junit 会调用parent 的classLoader 来加载
    库,结果就出现上面的ClassNotFoundException 提示信息

     

    解决方法:

    (1)要写classpath,且classpath中要包含batchtest的目录路径,即${classes}
     <path id="classpath">
      <pathelement location="${classes}" />
      ……

     </path>

    (2)junit这个task中,指明classpath
      <junit printsummary="on" haltonfailure="false" failureproperty="tests.failed" showoutput="true">
       <classpath refid="classpath" />
       <formatter type="xml" />
       <batchtest todir="${report.dir}" >
        <fileset dir="${classes}">
         <include name="**/TestCore.class"/>
        </fileset>
       </batchtest>
      </junit>

  • eclipse+ant+junit问题一

    2009-03-10 15:35:40

    当出现下述错误时:

    Could not create task or type of type: junit.

    Ant could not find the task or a class this task relies upon.

    This is common and has a number of causes; the usual
    solutions are to read the manual pages then download and
    install needed JAR files, or fix the build file:
     - You have misspelt 'junit'.
       Fix: check your spelling.
     - The task needs an external JAR file to execute
         and this is not found at the right place in the classpath.
       Fix: check the documentation for dependencies.
       Fix: declare the task.
     - The task is an Ant optional task and the JAR file and/or libraries
         implementing the functionality were not found at the time you
         yourself built your installation of Ant from the Ant sources.
       Fix: Look in the ANT_HOME/lib for the 'ant-' JAR corresponding to the
         task and make sure it contains more than merely a META-INF/MANIFEST.MF.
         If all it contains is the manifest, then rebuild Ant with the needed
         libraries present in ${ant.home}/lib/optional/ , or alternatively,
         download a pre-built release version from apache.org
     - The build file was written for a later version of Ant
       Fix: upgrade to at least the latest release version of Ant
     - The task is not an Ant core or optional task
         and needs to be declared using <taskdef>.
     - You are attempting to use a task defined using
        <presetdef> or <macrodef> but have spelt wrong or not
       defined it at the point of use

    Remember that for JAR files to be visible to Ant tasks implemented
    in ANT_HOME/lib, the files must be in the same directory or on the
    classpath

    Please neither file bug reports on this problem, nor email the
    Ant mailing lists, until all of these causes have been explored,
    as this is not an Ant bug.

     

    把 junit.jar 复制到 D:\Program Files\eclipse3.2\plugins\org.apache.ant_1.6.5\lib路径下

    并在eclipse中,window->preferences->ant->Runtime下,
    Classpath->Ant Home Entries(Default) 下 add External Jars,选择刚才复制到 D:\Program Files\eclipse3.2\plugins\org.apache.ant_1.6.5\lib路径下的junit.jar文件,即可解决问题。

  • ANT使用注意项

    2009-03-02 17:01:46

    ANT使用注意项

    ant的官方手册http://ant.apache.org/manual/index.html

    1、ant安装
     windows在环境变量path添加C:\Ant\bin
     Linux 是在/root/.bash_profile

    2、执行ANT构建文件
     ant -buildfile testbuild.xml -logfile /usr/log/log-for-ant.log

    3、target元素,depends参数
     当有多个依赖关系时,使用“,”隔开,顺序执行完定义的依赖关系,才会执行这个target

    4、路径中的*
     一个*表示:该目录和该目录下的第一级目录;
     两个*表示(**):该目录和该目录下的所有文件及子目录(递归);

    5、path的使用
     <classpath refid="classpath"/>
     <path id="classpath">
        <fileset dir="${lib}"/>
     </path>
     refid:对当前构建文件中,某处定义的一个path的引用;

    6、copy的使用
     <copy todir="${classes}" verwrite="true">
        <fileset dir="${src}"/>
     </copy>
     overwrite:指定是否覆盖目标文件,默认值是不覆盖;


    7、jar的使用
     使用方法1:<jar destfile="util.jar" basedir="${bin}"/>
     使用方法2:<jar jarfile="${basedir}/trBatchDay.jar" manifest="${trBatchDay}/MANIFEST.MF">
                  <fileset dir="${bin}"/>
              </jar>

    8、javac的使用
     <javac srcdir="src" destdir="classes">
        <classpath>
           <fileset dir="lib">
              <include name="**/*.jar"/>
           </fileset>
        </classpath>
     </javac>

  • ANT中过滤器的使用-zt

    2009-02-10 17:53:10

     

    <?xml version="1.0"?>

    <project name="access" default="show" basedir=".">
        
    <!--
            将一组需要过滤的值写入一个过滤文件,过滤文件的
            格式与一般的属性文件相同,如下:
            month=12
            year=2008
        
    -->
        
    <filter filtersfile="filter.properties"/> 

        
    <!--
            定义一个过滤器
        
    -->
        
    <filter token="time" value="14时7分"/>

        
    <target name="show">
            
    <mkdir dir="dest"/>
            
    <!-- 在copy中添加filtering属性启动过滤器 -->
            
    <copy todir="dest" filtering="true">
                
    <fileset dir="src"/>
            
    </copy>
        
    </target>
    </project>

    该过滤器的作用就是将src下面的文件复制到dest目录下面.而且将src下面,凡是包含@year@,@month@,@time@的字符进行替换(是指src文件夹下面文件中的内容,不是指文件名)。另外还可以使用filterset标签,它也可以引用外部filter文件,也可以在内容指定一组filter,如下:

     

    与单纯地使用filter相比,filterset的功能要强大一点,使用得被替换的字符不仅限制于以@开始和结束的变量了,你可以自己定义(使用begintoken和endtoken)。

    <copy file="${build.dir}/version.txt" toFile="${dist.dir}/version.txt">
      
    <filterset>
        
    <filter token="DATE" value="${TODAY}"/>
      
    </filterset>
    </copy>

    <!-- 自定义变量的格式 -->
    <copy file="${build.dir}/version.txt" toFile="${dist.dir}/version.txt">
      
    <filterset begintoken="%" endtoken="*">
        
    <filter token="DATE" value="${TODAY}"/>
      
    </filterset>
    </copy>

    <!-- 使用外部的过滤定义文件 -->
    <copy toDir="${dist.dir}/docs">
      
    <fileset dir="${build.dir}/docs">
        
    <include name="**/*.html">
      
    </fileset>
      
    <filterset begintoken="%" endtoken="*">
        
    <filtersfile file="${user.dir}/dist.properties"/>
      
    </filterset>
    </copy>

    <!-- 使用引用方式,重复利用过滤集 -->
    <filterset id="myFilterSet" begintoken="%" endtoken="*">
      
    <filter token="DATE" value="${TODAY}"/>
    </filterset>

    <copy file="${build.dir}/version.txt" toFile="${dist.dir}/version.txt">
      
    <filterset refid="myFilterSet"/>
    </copy>
Open Toolbar