JavaScud | Forum | JIRA | Blog |
  Dashboard > SpringSide > Home > selenium
  SpringSide Log In View a printable version of the current page.  
  selenium
Added by calvin, last edited by calvin on Sep 06, 2006  (view change)
Labels: 
(None)

Selenium指南

    网站:http://www.openqa.org

    国内大部分公司还依靠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.测试用例与测试脚本

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 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的运行脚本

   我写的Ant测试脚本一个重要特征是使用<parallel> 并行容器节点,同时打开tomcat 和selenium server,一边等待两者打开后执行JUnit。
   如果不使用并行节点,而是用spawn=yes属性后台启动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 变量。

SeleniumRefrence (SpringSide)
Site running on a free Atlassian Confluence Open Source Project License granted to WebWork China. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.3 Build:#808 May 29, 2007) - Bug/feature request - Contact Administrators