通過(guò)一周多的學(xué)習(xí)和總結(jié),終于掌握了casperjs用于自動(dòng)化的方法,填平了大大小小的各種坑。
casperjs是一個(gè)新興的測(cè)試框架,網(wǎng)上資料很少,基本上靠翻譯英文資料。
貢獻(xiàn)出來(lái),供大家參考:
?
//page.js,存放頁(yè)面元素
//c表示通過(guò)css選擇元素,x表示通過(guò)xpath選擇元素
var baseurl="
http://www.cnblogs.com/reach296/
";
var base={
//首頁(yè)
url:baseurl,
c:{
登錄表單:'form#login',
登錄按鈕:'.btn'
}
};
var index={
//登錄后成功后跳轉(zhuǎn)頁(yè)
url:baseurl+"/seven/index"
};
var sidebar={
//左邊框
url:baseurl+"/seven/sidebar.jsp",
x:{
應(yīng)用庫(kù):"http://span[contains(text(),應(yīng)用庫(kù))]",
應(yīng)用分類:"http://ul[@class='submenu']/li/a[1]"
}
};
var category_list={
//應(yīng)用分類page
url:baseurl+"/seven/app/category-list",
c:{
名稱:'td.sorting_1'
},
x:{
表格:'//*[@id="sample-table-2"]/tbody/tr',
名稱:'//*[@id="sample-table-2"]/tbody/tr/td[1]',
海報(bào):'//*[@id="sample-table-2"]/tbody/tr/td[2]',
編輯:'//*[@id="sample-table-2"]/tbody/tr/td[3]'
}
}
//common.js,存放全局變量和方法
var path={
capture:'cms/capture/',
lib:'cms/lib/'
};
var cap={
clipRect:{top: 0,left: 0,width: 1024,height: 768},
// clipRect:{width: 1024,height:768},
imgOptions:{format: 'jpg',quality:100}
};
var account={'loginName':'reachwang','passwd':'test12345'};
function get_menu_links(){
//獲取一級(jí)模塊
var links = window.frames[0].document.getElementsByTagName("frame")[0].contentDocument.getElementsByClassName("menu-text");
return Array.prototype.map.call(links, function(e) {
return e.innerText;
});
};
function get_submenu_links(){
//獲取二級(jí)模塊
var links = window.frames[0].document.getElementsByTagName("frame")[0].contentDocument.querySelectorAll('.submenu a');
return Array.prototype.map.call(links, function(e) {
return (e.innerText).replace(/(^\s*)|(\s*$)/g, "");
});
};
//應(yīng)用分類測(cè)試用例,檢查應(yīng)用分類頁(yè)面是否正常展示,分類數(shù)據(jù)是否存在
casper.test.begin('應(yīng)用分類測(cè)試用例',function suite(test) {
casper.options.verbose = true;
casper.options.logLevel = "debug";
casper.options.viewportSize={width: 1024, height: 768};
casper.options.waitTimeout=20000;
// casper.options.clientScripts=[
// path.lib+'common.js'
// ];
casper.test.comment('檢查應(yīng)用分類頁(yè)面是否正常展示,分類數(shù)據(jù)是否存在');
casper.start(base.url, function() {
this.echo("1、打開(kāi)登錄頁(yè)面");
test.assertHttpStatus(200,"檢查http請(qǐng)求狀態(tài)");
});
casper.waitForSelector(base.c.登錄按鈕, function() {
this.echo("2、登錄頁(yè)面截圖");
this.capture(path.capture+"登錄頁(yè)面.jpg",cap.clipRect, cap.imgOptions);
});
casper.then(function() {
this.echo("3、登錄頁(yè)面檢查");
test.assertTitle("TCL CMS", "標(biāo)題正確");
test.assertExists(base.c.登錄表單, "帳號(hào)登錄表單存在");
this.echo("4、輸入帳號(hào)和密碼");
this.fill(base.c.登錄表單, account, true);
this.echo("5、點(diǎn)擊登錄按鈕");
test.assertExists(base.c.登錄按鈕, "登錄按鈕存在");
this.mouse.click(base.c.登錄按鈕);
});
casper.waitForUrl(index.url,function(){
test.assertHttpStatus(200,"跳轉(zhuǎn)到登錄完成頁(yè)");
this.capture(path.capture+'跳轉(zhuǎn)到登錄完成頁(yè).jpg',cap.clipRect, cap.imgOptions);
});
casper.withFrame(0,function(){
this.echo("切換到mian frame里");
casper.withFrame(0,function(){
this.echo("切換到mian frame下的sider frame里");
this.echo("6、點(diǎn)擊應(yīng)用庫(kù)");
this.click({type: 'xpath',path:sidebar.x.應(yīng)用庫(kù)});
this.echo("7、點(diǎn)擊應(yīng)用分類");
this.click({type: 'xpath',path:sidebar.x.應(yīng)用分類});
});
});
casper.waitForUrl(index.url,function(){
test.assertHttpStatus(200,"跳轉(zhuǎn)到應(yīng)用分類頁(yè)");
this.capture(path.capture+'打開(kāi)應(yīng)用分類.jpg',cap.clipRect, cap.imgOptions);
});
casper.withFrame(0,function(){
this.echo("切換到mian frame里");
casper.withFrame(1,function(){
this.echo("應(yīng)用分類頁(yè)面")
test.assertExists({type: 'xpath',path:category_list.x.表格},"表格檢查");
test.assertExists(category_list.c.名稱,"名稱字段檢查");
test.assertExists({type: 'xpath',path:category_list.x.海報(bào)},"海報(bào)字段檢查");
test.assertExists({type: 'xpath',path:category_list.x.編輯},"編輯字段檢查");
});
});
casper.run(function() {
test.done();
});
});
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

