blogjava's web log

blogjava's web log
...

Selenium测试 使用笔记

看了江南白衣 Selenium--透明反复推介的集成测试工具(Pragmatic系列)
确实不错。值得推荐使用。但是有的时候确实也挺麻烦。
比如说,我一个页面可能有上百个文本框。等很多动作。
那在Junit 里可能要 写 一段很长的代码了。

    test.open("/");
   
test.click("");
   
test.type("");
。。。。。
。。。。。。。
。。。。。
 我想在稍微大点的项目中 这样写测试代码 可能也是个噩梦。。


解决办法

1.如果没有安装firefox 下载fireFox  

2.Selenium IDE

  1. 下载  Selenium IDE https://addons.mozilla.org/firefox/2079/
  2. Firefox > Tools > Selenium IDE
  3. Selenium IDE > Options > Options...
    1. 选择 Format 选项.
    2. 点击添加按钮.
    3. (Name of the fommat )给新添的起个名字 testSelenium
    4. 粘贴下面JS代码到文本恇
    5. 点击确定
  4. 点击工具 菜单 Selenium IDE > Options > Format > testSelenium
  5. 可以录脚本了

testSelenium.js

var  SEPARATORS = {
  pipe: 
"|",
  comma: 
",",
  tab: 
"\t"
};

function formatCommands(commands) {
  
var result = '';
  
var sep = SEPARATORS[options.separator];
  
var prefix = options.wrap == 'true? sep : "";
  
var postfix = prefix;
  
for (var i = 0; i < commands.length; i++) {
    
var command = commands[i];
    
if (command.type == 'command') {
      result 
+= prefix + command.command + sep + command.target + sep + command.value + postfix + "\n";
    }
  }
  
return result;
}

function parse(testCase, source) {
  
var doc = source;
  
var commands = [];
  
var sep = SEPARATORS[options.separator];
  
var base = options.wrap == 'true? 1 : 0;
  
while (doc.length > 0) {
    
var line = /(.*)(\r\n|[\r\n])?/.exec(doc);
    
var array = line[1].split(sep);
    
if (array.length >= (base+3)) {
      
var command = new Command();
      command.command 
= array[base+0];
      command.target 
= array[base+1];
      command.value 
= array[base+2];
      commands.push(command);
    }
    doc 
= doc.substr(line[0].length);
  }
  testCase.setCommands(commands);
}

function format(testCase, name) {
  
return formatCommands(testCase.commands);
}

options 
= {
    separator: 'pipe',
    wrap: '
true'
};

configForm 
= 
    '
<description>Separator</description>+ 
    '
<menulist id="options_separator">+
    '
<menupopup>+
    '
<menuitem label="Pipe" value="pipe"/>+
    '
<menuitem label="Comma" value="comma"/>+
    '
<menuitem label="Tab" value="tab"/>+
    '
</menupopup>+
    '
</menulist>+
    '
<checkbox id="options_wrap" label="Wrap command with separator"/>';


最后 录制的 脚本 如下

base url http://www.cnblogs.com/

|open|/||
|clickAndWait|link=注册||
|type|ctl00_cphMain_tbApplicant|junmy|
|type|ctl00_cphMain_tbEmail|wunjunlove(At)gmial.com|
|type|ctl00_cphMain_tbCompany|msSonft|
|type|ctl00_cphMain_tbCity|shenzhen|
|type|ctl00_cphMain_tbApplyText|teeee详细申请理由(比如:技术专长,感兴趣的技术,在Blog中写哪方面的内容|
|click|ctl00_cphMain_btnSend||


把此录制的脚本 保存为一个文件。

再写一个类专门来处理分析 这个脚本

就不用 在Junit测试 写 那么长的

    test.open(
"/");
   
test.click("");
   
user.type("");

等特长的代码了 。



posted on 2006-10-29 12:09 record java and net 阅读(3878) 评论(4)  编辑  收藏 所属分类: Springjava

评论

# re: Selenium测试 使用笔记 2006-10-30 09:56 inlife.cn

没听过。  回复  更多评论   

# re: Selenium测试 使用笔记 2006-10-30 21:28 Tin

先后和nemo、limo、raimudox、nicholas讨论:
selenium的Remote Control比较适合在需求阶段就撰写,作为验收的测试。好处是RC对重构支持相对好一些,而且你可以换Agent,也可以做浏览器兼容测试。(但是由于RC的限制,兼容侧试兼容性并不好:)
按照raimudox所说RC是更加Pragmatic的实践,更能体现敏捷软件开发的测试先行的特性。功能测试可以说是沟通用户与开发者的最佳契约。
Selenium IDE适合作为基线保留,作为某次重构之前的样本。或者说,如果觉得手写测试脚本太麻烦,而喜欢本末倒置(没有贬义,纯技术上)的人设计的。更现实的说,这很有用,比如一个项目从一半开始敏捷改造,引入功能测试、单元测试,对以后的迭代进行基线的衡量,给新引入的CI(持续集成)一个更有实际意义的测试保障,用Selenium IDE帮助生成一下Script,然后再使用RC或者直接用Core执行一下都是不错的实践。
还有,据Nicholas同学实践,Selenium IDE所录制的script在IDE中执行比RC方式兼容性要好,尤其对于跨域的情况,RC很有可能是无法工作的。还有一个问题,就是Selenium实际上是ThoughtWorks和BEA牵头的项目,TW负责Core,目前Core的代码发展的必较快,而RC由BEA负责,发展比较缓慢,所以,有些时候选择也就成为无奈了。  回复  更多评论   

# re: Selenium测试 使用笔记 2006-11-19 13:09 苗春利

仿宋  回复  更多评论   

# re: Selenium测试 使用笔记 2007-07-05 10:43 Jackei

对于有开发经验的人来说,RC 模式的确更合适一些,特别是 RC 模式对于脚本的可扩展性和可维护性会好很多。而且脚本可以通过抽象和封装来提高可重用性。 Core 模式下几乎就无法实现这些特性了。  回复  更多评论   


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


网站导航:
 

导航

常用链接

留言簿(44)

新闻档案

2.动态语言

3.工具箱

9.文档教程

友情链接

搜索

最新评论