无为

无为则可为,无为则至深!

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  190 Posts :: 291 Stories :: 258 Comments :: 0 Trackbacks
国内大部分公司还依靠QA组的MM看着测试用例文档来手工测试,如果钱人有限,又想改变现状,最实在的建议是先编写直接访问数据库的商业层UnitTest和基于Selenium的集成测试这两种实用性最高的测试。

   在徐昊指导下,SpringSide2.0 已经全面应用Selenium。

   Selenium 能被选为最好集成测试、回归测试方案的原因,是因为:

   1.Selenium IDE ,一个FireFox plugin,能自动记录用户的操作,生成测试脚本

   2. 生成的测试脚本可以用Selenium Core手工执行,也能基于Selenium RC放入Java,C#,Ruby的单元测试用例中自动运行

   3. 测试用例调用实际的浏览器(如IE、FireFox)来执行测试。和有些开源方案自行实现Web解释引擎相比,实际的浏览器能模拟更多用户交互和JS语法,顺便还可以测试各浏览器兼容性。

   4. 测试脚本语法非常简单,见后。
  

1. 使用Selenium IDE生成脚本

       Selenium IDE 是一个Firefox1.5插件,下载后用Firefox将其打开。

       工具->Selenium IDE,点击红色的recorder按钮开始录制,在网站中乱点时可以即时看到每个动作的脚本。

       切换Format:显示 HTML,Java,C#,Ruby 语法的脚本。 option里还可以设定Java里Selenium变量的名称,如user。

2.测试用例与测试脚本

   测试用例在Selenium IDE生成->Copy Paste的流程下非常的容易。

public class UserManagerTest extends TestCase
{
    
private Selenium user;

    
public void setUp() throws Exception {
       user
= new DefaultSelenium("localhost", SeleniumServer.DEFAULT_PORT, "*iexplore""http://localhost:8080");
       user.start();
}
protected void tearDown() throws Exception {
        user.stop();
}

public void testUserEdit() {
    user.open(
"/helloworld");
    user.click(
"//a\[contains(@href, 'user.do?id=0')\]");
    user.waitForPageToLoad(
"3000");
    user.type(
"user.name""calvin");
    user.click(
"save");
    user.waitForPageToLoad(
"3000");
    assertTrue(user.isTextPresent(
"calvin"));
}


   留意setUp中的"*iexplore"参数,设定使用IE作为测试浏览器;如果设为"*firefox",就会在PATH中查找*firefox.exe。

   注意,Selenium使用IE时的Proxy机制比较特殊,如果你同时在本机ADSL modem拨号上网,要先断网。

   脚本中按徐昊的指导,使用user 作为Selenium的变量名,使用例更加易读。

   Selenium提供了非常丰富的用户交互函数,但Selenium RC里并没有为Java单列一个函数参考手册,需要阅读公共的Selenium Refrences,再使用同名对应的java函数。

   所有函数都是一个locator参数,将操作付诸某个页面上的对象。支持ID,DOM语法,XPath语法,CSS selector语法等,详见参考手册

   如果不会写,最好的老师还是Selenium IDE 。比如那句点击 <a href="user.do?id=0" />修改</a>,就是用IDE得到user.click("//a[contains(@href, 'user.do?id=0')]")的XPath语句。

3.Ant的运行脚本

    SpringSide的Ant测试脚本比较有特点的一个地方是使用了ant 的<parallel> 并行容器节点,一边同时打开tomcat 和selenium server,一边等待两者打开后执行JUnit,最后关闭tomcat。
   如果不使用并行节点,而是用spawn=true属性后台启动tomcat,屏幕里就看不到tomcat信息,如果测试意外终止的话,就不能靠关闭窗口来tomcat,很不方便。

<parallel>
    
<antcall target="tomcat.start"/>
    
<antcall target="selenium.server.start"/>
    
<sequential>
        
<waitfor maxwait="10" maxwaitunit="minute" checkevery="1" checkeveryunit="second">
            
<http url=http://localhost:8080/>
        
</waitfor>
        
<waitfor maxwait="10" maxwaitunit="minute" checkevery="1" checkeveryunit="second">
            
<socket server="localhost" port="4444"/>
       
</waitfor>
       
<junit./>
       
<antcall target="tomcat.stop"/>
    
</sequential>
</parallel>

4.SpringSide 中的FunctionalTestCase基类

SpringSide中抽象了一个FunctionalTestCase基类,抽取了setUp() ,tearDown()函数中selenium server 开闭操作。

其中浏览器类型默认为"*iexplore", 基本url默认为http://localhost:8080

用户可以在selenium.properties 中重新设定selenium.explorer 和selenium.baseurl 变量。



凡是有该标志的文章,都是该blog博主Caoer(草儿)原创,凡是索引、收藏
、转载请注明来处和原文作者。非常感谢。

posted on 2006-09-07 14:56 草儿 阅读(1288) 评论(0)  编辑  收藏 所属分类: Java编程经验谈

只有注册用户登录后才能发表评论。


网站导航: