如何用Selenium IDE 实现简单的循环和条件语句

Selenium IDE下载地址:

 http://seleniumhq.org/download/

Selenium是一个很好用的Web界面测试框架。但它的功能也有不足之处,比如: 在Selenium IDE中不支持程序控制语句。下面介绍如何在Selenium IDE中添加程序控制功能。

1) 下载Selenium插件 (sideflow.js): 

 如果不想下载,直接把下面的代码保存到本机也可。

2) 简单的应用 if-else function 我们一般会用到gotoIfgotolabellabel这三个command,在selenium ide里按照command | target | value的顺序举个简单的例子:


举例录制新浪通行证的注册模块:


如何用Selenium <wbr>IDE <wbr>实现简单的循环和条件语句


添加sideflow.js文件的步骤,需要在selenium的options——general中添加sideflow.js文件,添加后重启Selenium IDE。

如何用Selenium <wbr>IDE <wbr>实现简单的循环和条件语句


其中sideflow.js的下载地址:

https://github.com/darrenderidder/sideflow/blob/master/sideflow.js


sideflow.js的代码如下:


   

 
var  gotoLabels =  {};
var  whileLabels  =  {};

// overload the original Selenium reset function
Selenium . prototype . reset  =  function ()  {
     // reset the labels
     this . initialiseLabels ();
     // proceed with original reset code
     this . defaultTimeout  =  Selenium . DEFAULT_TIMEOUT ;
     this . browserbot . selectWindow ( "null" );
     this . browserbot . resetPopups ();
}


Selenium . prototype . initialiseLabels  =  function ()
{
     gotoLabels  =  {};
     whileLabels  =  {  ends :  {},  whiles :  {}  };
     var  command_rows  =  [];
     var  numCommands  =  testCase . commands . length ;
     for  ( var  i  =  0 ;  i  <</span> numCommands; ++i) {
         var  x  =  testCase . commands [ i ];
         command_rows . push ( x );
     }
     var  cycles  =  [];
     var  forEachCmds  =  [];
     for (  var  i  =  0 ;  i  <</span> command_rows.length; i++ ) {
         if  ( command_rows [ i ]. type  ==  'command' )
         switch (  command_rows [ i ]. command . toLowerCase ()  )  {
             case  "label" :
                 gotoLabels [  command_rows [ i ]. target  ]  =  i ;
                 break ;
             case  "while" :
             case  "endwhile" :
                 cycles . push (  [ command_rows [ i ]. command . toLowerCase (),  i ]  )
                 break ;
             case  "foreach" :
             case  "endforeach" :
                 forEachCmds . push (  [ command_rows [ i ]. command . toLowerCase (),  i ]  )
                 break ;
         }
     }
     var  i  =  0 ;
     while (  cycles . length  )  {
         if (  i  >=  cycles . length  )  {
             throw  new  Error (  "non-matching while/endWhile found"  );
         }
         switch (  cycles [ i ][ 0 ]  )  {
             case  "while" :
                 if (  (  i + 1  <</span> cycles.length ) && ( "endwhile" == cycles[i+1][0] )) {
                     // pair found
                     whileLabels . ends [  cycles [ i + 1 ][ 1 ]  ]  =  cycles [ i ][ 1 ];
                     whileLabels . whiles [  cycles [ i ][ 1 ]  ]  =  cycles [ i + 1 ][ 1 ];
                     cycles . splice (  i ,  2  );
                     i  =  0 ;
                 }  else  ++ i ;
                 break ;
             case  "endwhile" :
                 ++ i ;
                 break ;
         }
     }

}

Selenium . prototype . continueFromRow  =  function (  row_num  )
{
     if ( row_num  ==  undefined  ||  row_num  ==  null  ||  row_num  <</span> 0) {
         throw  new  Error (  "Invalid row_num specified."  );
     }
     testCase . debugContext . debugIndex  =  row_num ;
}

// do nothing. simple label
Selenium . prototype . doLabel  =  function (){};

Selenium . prototype . doGotoLabel  =  function (  label  )
{
     if (  undefined  ==  gotoLabels [ label ]  )  {
         throw  new  Error (  "Specified label '"  +  label  +  "' is not found."  );
     }
     this . continueFromRow (  gotoLabels [  label  ]  );
};

Selenium . prototype . doGoto  =  Selenium . prototype . doGotoLabel ;

Selenium . prototype . doGotoIf  =  function (  condition ,  label  )
{
     if (  eval ( condition )  )  this . doGotoLabel (  label  );
}

Selenium . prototype . doWhile  =  function (  condition  )
{
     if (  ! eval ( condition )  )  {
         var  last_row  =  testCase . debugContext . debugIndex ;
         var  end_while_row  =  whileLabels . whiles [  last_row  ];
         if (  undefined  ==  end_while_row  )  throw  new  Error (  "Corresponding 'endWhile' is not found."  );
         this . continueFromRow (  end_while_row  );
     }
}

Selenium . prototype . doEndWhile  =  function ()
{
     var  last_row  =  testCase . debugContext . debugIndex ;
     var  while_row  =  whileLabels . ends [  last_row  ]  -  1 ;
     if (  undefined  ==  while_row  )  throw  new  Error (  "Corresponding 'While' is not found."  );
     this . continueFromRow (  while_row  );
}

Selenium . prototype . doPush =  function ( value ,  varName )
{
     if ( ! storedVars [ varName ])  {
         storedVars [ varName ]  =  new  Array ();
     }
     if ( typeof  storedVars [ varName ]  !==  'object' )  {
         throw  new  Error ( "Cannot push value onto non-array "  +  varName );
     }  else  {
         storedVars [ varName ]. push ( value );
     }
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Selenium是一个广泛使用的自动化测试工具套件,它提供了多种组件用于Web应用程序的自动化测试。Selenium IDESelenium WebDriver(也称为Selenium RC)和Selenium分别代表了Selenium的不同部分: 1. **Selenium IDE**:这是一个基于浏览器的工具,主要用于录制和回放Web测试脚本。它允许用户直观地设计测试用例,通过图形界面录制用户的鼠标和键盘操作,然后生成可重复执行的测试代码。它并不直接支持自动化测试的执行,而是将录制的测试转换为WebDriver JSON Wire Protocol(W3C标准)。 2. **Selenium WebDriver (Selenium RC)**:这是一个客户端库和服务器端API的组合,它包含了Selenium的核心功能,可以直接编写代码进行自动化测试。WebDriver提供了一个统一的方式来控制不同的浏览器,如Chrome、Firefox等,使测试脚本可以在不同环境中运行,比IDE更灵活和强大。它是Selenium的自动化测试驱动部分,IDE中的动作会被转化为WebDriver API调用。 3. **Selenium**:这是整体的项目名称,包含了IDE、WebDriver以及其他相关的工具和库,如Selenium Grid用于分布式测试,Selenium Server用于管理和调度测试。Selenium WebDriver是Selenium项目的基石,IDE作为它的简易入口,而WebDriver则提供了更多的高级功能和定制化选项。 总结一下,Selenium IDESelenium工具套件的一部分,它作为一个图形化工具方便初学者入门,而Selenium WebDriver是更底层、更强大的核心测试驱动,它们都是为了实现更复杂的Web应用自动化测试。如果你在使用中,可能会先从IDE开始,然后逐渐转向更专业的WebDriver来编写和维护测试代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值