看了
江南白衣 的
Selenium--透明反復推介的集成測試工具(Pragmatic系列)確實不錯。值得推薦使用。但是有的時候確實也挺麻煩。
比如說,我一個頁面可能有上百個文本框。等很多動作。
那在Junit 里可能要 寫 一段很長的代碼了。
像
??? test.open("/");
??? test.click(""
);
??? test.type("");
。。。。。
。。。。。。。
。。。。。
?我想在稍微大點的項目中 這樣寫測試代碼 可能也是個噩夢。。
解決辦法
1.如果沒有安裝firefox
下載fireFox ?
2.Selenium IDE
- 下載? Selenium IDE https://addons.mozilla.org/firefox/2079/
- Firefox > Tools > Selenium IDE
- Selenium IDE > Options > Options...
- 選擇 Format 選項.
- 點擊添加按鈕.
- (Name of the fommat )給新添的起個名字
testSelenium
- 粘貼下面JS代碼到文本恇
- 點擊確定
- 點擊工具 菜單 Selenium IDE > Options > Format > testSelenium
- 可以錄腳本了
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("");
等特長的代碼了 。