eclipse+selenium+testNG+Ant集成

1. 在eclipse中安装TestNGinx插件,http://beust.com/eclipse
2. eclipse已经集成了Ant,所以无需安装Ant
3. 执行testNG之后,会自动生成test-output目录
创建TestNG class文件,GoogleTest.java

  1. package com.twioo.test.google;
  2.  
  3. import com.thoughtworks.selenium.*;
  4. import static org.testng.AssertJUnit.assertTrue;
  5.  
  6. import org.testng.annotations.Test;
  7.  
  8. public class GoogleTest {
  9.   String url = "http://www.google.com";
  10.   private Selenium selenium = new DefaultSelenium("localhost", 4444,
  11.             "*iexplore", url);
  12.   @Test
  13.   public void testSearch() {
  14.       selenium.open("/");
  15.       selenium.type("q", "selenium rc");
  16.       selenium.click("btnG");
  17.       selenium.waitForPageToLoad("30000");
  18.       assertTrue(selenium.isTextPresent("Results * for selenium rc"));
  19.   }
  20. }

创建TestNG文件时,可以指定XML suite file,如testng.xml,会自动生成XML文件,内容如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <suite name="Suite" parallel="false">
  3.   <test name="testSearch">
  4.     <classes>
  5.       <class name="com.twioo.test.google.GoogleTest"/>
  6.     </classes>
  7.   </test>
  8. </suite>

4. 在testng.xml右键,点击run as -> TestNG Suite,即可运行
5. 整合ANT,build.xml文件内容如下

  1. <project name="myproject" basedir="." default="start_server_and_run_tests">
  2.     <property name="src.dir" value="src" />
  3.     <property name="lib.dir" value="lib" />
  4.     <property name="test.dir" value="test" />
  5.     <property name="dist.dir" value="dist" />
  6.     <path id="test.classpath">
  7.         <pathelement location="${src.dir}" />
  8.         <pathelement location="${dist.dir}" />
  9.         <pathelement location="." />
  10.         <!-- adding the saxon jar to your classpath -->
  11.         <fileset dir="${lib.dir}" includes="*.jar" />
  12.     </path>
  13.  
  14.     <taskdef name="testng" classname="com.beust.testng.TestNGAntTask" classpath="${lib.dir}/testng-6.1.1.jar" />
  15.  
  16.     <target name="compile">
  17.         <!--clean the old classes-->
  18.         <delete dir="${dist.dir}" failonerror="false" />
  19.         <!--create new dist dir-->
  20.         <mkdir dir="${dist.dir}" />
  21.         <!--compile-->
  22.         <javac classpathref="test.classpath" srcdir="${src.dir}" destdir="${dist.dir}" />
  23.     </target>
  24.  
  25.     <target name="start_server_and_run_tests" depends="compile" description="start selenium server and run tests">
  26.         <parallel>
  27.             <antcall target="start_selenium_server" />
  28.             <sequential>
  29.                 <echo taskname="waitfor" message="wait for selenium server launch" />
  30.                 <waitfor maxwait="2" maxwaitunit="minute" checkevery="10">
  31.                     <http url="http://localhost:4444/selenium-server/driver/?cmd=testComplete" />
  32.                 </waitfor>
  33.                 <antcall target="run_tests">
  34.                 </antcall>
  35.             </sequential>
  36.         </parallel>
  37.     </target>
  38.  
  39.     <target name="run_tests">
  40.         <testng classpathref="test.classpath" outputDir="test-output">
  41.             <xmlfileset dir="src" includes="testng.xml" />
  42.         </testng>
  43.         <xslt in="${basedir}/test-output/testng-results.xml" style="${basedir}/test-output/testng-results.xsl" out="${basedir}/test-output/index1.html">
  44.             <!-- you need to specify the directory here again -->
  45.             <param name="testNgXslt.outputDir" expression="${basedir}/test-output/" />
  46.             <classpath refid="test.classpath" />
  47.         </xslt>
  48.         <antcall target="stop_selenium_server" />
  49.         <fail message="ERROR: test failed!!!!!" if="test.failed" />
  50.     </target>
  51.  
  52.     <target name="start_selenium_server">
  53.         <echo message="starting selenium server" />
  54.         <java jar="${lib.dir}/selenium-server-standalone-2.1.0.jar" fork="true" spawn="false" output="selenium.log" />
  55.     </target>
  56.  
  57.     <target name="stop_selenium_server">
  58.         <echo message="going to stop the selenium server" />
  59.         <get taskname="selenium-shutdown" src="http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer" dest="stop.out" ignoreerrors="true" />
  60.     </target>
  61. </project>

6. 执行build.xml之后,会在test-output文件夹下生成index1.htm文件,打开即可看到结果
遇到的问题:
1. 警告:编码 UTF8 的不可映射字符
原因:eclipse工程中默认的编码是GBK,xml中指定的是utf-8,两者要保持一致
2. 执行build.xml时报Cannot find class in classpath
原因:没有编译,编译之后就可以了

参考的文档

http://www.ibm.com/developerworks/cn/java/j-testng/

http://www.seleniumcn.cn/simple/index.php?t164.html-

利用autoit来解决selenium对onload事件中alert弹出框不支持的问题

经过一番查证,selenium2.1对onload事件中的alert弹出框仍然不支持,官网也说的很明确http://seleniumhq.org/docs/05_selenium_rc.html#learning-the-api
Is it ok to load a custom pop-up as the parent page is loading (i.e., before the parent page’s javascript window.onload() function runs)?
No. Selenium relies on interceptors to determine window names as they are being loaded. These interceptors work best in catching new windows if the windows are loaded AFTER the onload() function. Selenium may not recognize windows loaded before the onload function.

但是我不想就这么算了,虽然现在的项目没有这种弹出框,但我还是希望能找到支持onload中alert的方法。
有些时候心里明明清楚,每一款框架都不可能百分百的完美,可我还是希望能达到最大限度的完美。
第一个想到可以辅助支持alert的就是autoit,在用ruby的时候接触过。还是蛮好用的。
下面的方案,经实验可以解决onload事件中的alert。但是我还不够满意,暂时先记录一下,慢慢寻找更好的解决方案。
1. 用autoit脚本,并转换成exe文件IE_Handle.exe

  1. #include <IE.au3>   
  2. WinWaitActive("来自网页的消息","") 
  3.  If WinActive("来自网页的消息","") Then 
  4.     Send("{ENTER}") 
  5.  endif 
  6. Exit

2. 在java代码中调用该exe文件

  1. package com.example.tests;
  2.  
  3. import com.thoughtworks.selenium.*;
  4.  
  5. @SuppressWarnings("deprecation")
  6. public class NewTest extends SeleneseTestCase {
  7.     public void setUp() throws Exception {
  8.         setUp("http://localhost:8080/", "*iexplore");
  9.     }
  10.  
  11.     public void testNew() throws Exception {
  12.         openExe();
  13.         selenium.open("/test/test.html");
  14.     }
  15.  
  16.     public static void openExe() {
  17.         Runtime rn = Runtime.getRuntime();
  18.         Process p = null;
  19.         try {
  20.             p = rn.exec("\"D:/IE_Handle.exe\"");
  21.         } catch (Exception e) {
  22.             System.out.println("Error exec!");
  23.         }
  24.     }
  25. }

teset.html中的代码是

  1. <script>alert('aaa');</script>

因为现在对java代码不是还很熟,有一点疑惑的地方
按照流程来讲testNew中,执行顺序应该是selenium.open(“/test/test.html”);openExe();
可实践证明,要先openExe();然后再执行selenium.open(“/test/test.html”);

我用au3文件测试过,先打开test.html,然后执行au3是可以关闭弹出框的
可是生成exe文件之后,就得先执行exe文件,然后打开test.html才可以关闭弹出框,不解啊,十分的不解

准备再次着手研究selenium

第一次接触selenium是09年的时候,那时候selenium还不太成熟。因为项目中用到的alert窗口很多,但是selenium还无法识别alert窗口,无耐只好放弃。

最近比较空,就想起这个老朋友了。随便google了一下,发现貌似现在支持了,加上现在的项目已经没有alert窗口,用的都是浮出层的形式,再加上最近真的很空,打酱油都已经打的无聊了,做点正事吧。

再次研究研究selenium,希望有一天能派上用场,对项目的测试能有些帮助。

Python strftime日期格式化的中文问题

想将日期格式显示为:xxxx年xx月xx日的格式,但是因为里面有中文,使用time.strftime(“%Y年%m月%d日”, time.localtime())将会出现编码导致的错误问题

于是我将各个部分拆分出来,然后再进行字符串的拼接
time.strftime(“%Y”, time.localtime()) + u”年” + time.strftime(“%m”, time.localtime()) + u”月” + time.strftime(“%d”, time.localtime()) + u”日”

不知道是否有更好的办法

Python编码转换方法(解决因中文带来的编码问题)

  1. #将Unicode转换成普通的Python字符串:"编码(encode)"
  2.  unicodestring = u"Hello world"
  3.  utf8string = unicodestring.encode("utf-8")
  4.  asciistring = unicodestring.encode("ascii")
  5.  isostring = unicodestring.encode("ISO-8859-1")
  6.  utf16string = unicodestring.encode("utf-16")
  7.  
  8. #将普通的Python字符串转换成Unicode: "解码(decode)"
  9. plainstring1 = unicode(utf8string, "utf-8")
  10. plainstring2 = unicode(asciistring, "ascii")
  11. plainstring3 = unicode(isostring, "ISO-8859-1")
  12. plainstring4 = unicode(utf16string, "utf-16")

windows下安装mod_python

1. 运行mod_python-3.3.1.win32-py2.6-Apache2.2.exe进行安装,安装程序会检测 Python 和 询问Apache 的安装路径,安装完成后你会发现:mod_python.so 安装至 Apache2.2/modules 目录下;Python26\Lib\site-packages下面也多了一个mod_python文件夹

2. 打开 Apache2.2/conf/httpd.conf,在文件里面添加以下内容。

LoadModule python_module modules/mod_python.so
Alias /python D:/py
<Directory “D:/py”>
Order deny,allow
Allow from all
AllowOverride FileInfo
AddHandler mod_python .py
PythonHandler test
PythonDebug On
</Directory>
注意:如果没有“Order deny,allow 或 Allow from all” 就可能出现403错误,提示没有访问权限

3. 重新启动 Apache2.2。

4. 在 d: 下建立 /py 目录,用于存放 .py 文件。

下面写一个文件来测试一下。在 D:/py下新建文件 test.py
此处一定要注意 PythonHandler test意指Python的Handler是test.py。所以PythonHandler 后面一定要是要执行的.py文件的文件名
内容如下:
from mod_python import apache

def handler(req):
req.write(”Hello!”)
return apache.OK

最后:打开浏览器,输入地址 http://localhost/python/test.py,回车。
如果页面显示“Hello!”,则说明安装成功。