madanling 2013-10-09
自动投票
var casper = require('casper').create({ verbose: true, //logLevel: 'debug', pageSettings: { loadImages: false, loadPlugins: true, userAgent: 'Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/20100101 Firefox/17.0' }, viewportSize: { width: 1024, height: 768 }, clientScripts: [ '/home/pandy/PhantomJS/demo/toupiao/jquery.js' //'http://libs.baidu.com/jquery/1.9.0/jquery.js' //引入jquery //但是类似this.test.assertExists(".rh_ui_icon li a:first","存在链接")测试失败, //还不知道原因和解决办法 ], onError: function(self,m){//错误回调函数 this.capture("error.png"); console.log("onError===========================FATAL:" + m); self.exit(); }, onAlert: function (msg) {//alert的回调函数 console.log("onAlert===========================msg:" + msg); } }); //phantom.outputEncoding="gbk"; //casper.options.viewportSize = {width: 1680, height: 924}; /* //=============> A: 第1种方式 casper.start('http://www.cnave.com/xjz/index.php?m=content&c=index&a=toupiao&id=42',function(){ this.capture("c:/test.png",{ top: 00, left: 0, width: 1024, height: 768 }); var date = new Date(); this.echo(date.getTime()+"====================>capture over"); }); casper.run(); */ //=============> B:第2种方式 casper.start('http://www.cnave.com/xjz/',function(){ this.capture("/home/pandy/PhantomJS/demo/toupiao/test2.png",{ top: 00, left: 0, width: 1366, height: 768 }); var date = new Date(); this.echo(date.getTime()+"====================>capture over"); var nameCount = this.evaluate(function() { var names = $("a[onclick='toupiao(8)']"); names.trigger("click"); return names.length; }); this.echo(nameCount); }); casper.run();
对163所有新闻的截图例子这只是一个casper1.0.x版本的语法例子。
//casperjs /mnt/D/work_documents/workspace_ide/phantomjs_casperjs/casperjs/list_163_new.js var casper = require("casper").create({ verbose: true, logLevel: "info", viewportSize: [1366, 678], pageSettings:{ //定义一些变量 }, clientScripts: [ //"http://code.jquery.com/jquery-1.9.0.js" ], onAlert: function (msg) { }, onError: function () { console.log("错误","error"); } }); var utils = require("utils"); var url = "http://news.163.com"; var d1 = new Date(); var count=0; //一个计数器 var links = []; //用于记录所有匹配的链接 console.log("开始启动程序"); //启动 casper.start(url, function () { console.log("打开标题:"+this.getTitle()); }); casper.then(function () { console.log("匹配链接"); links = this.evaluate(function () { return [].map.call(__utils__.findAll('.area-main .main-list a'), function (node) { return node.getAttribute('href'); }); }); }); casper.then(function(){ console.log("打开链接并截图"); this.each(links, function (self, link) { console.log(link); self.thenOpen(link,function(){ var path=link.substring(7);//去掉http:// console.log("截图["+links.length+":"+count+"] title=>"+this.getTitle()); this.capture("/mnt/D/work_documents/workspace_ide/phantomjs_casperjs/images/"+path+".png"); count++; }); }); }); casper.then(function(){ console.log("计算耗时"); var d2=new Date(); var i=d2.getTime()-d1.getTime(); console.log("共使用了 "+(i)/1000+" s导出"+count+"张图片."); }) casper.run(function () { console.log("操作完成."); casper.exit(); });
删除持续集成hudson里面的一些部署历史
// casperjs /mnt/D/work_documents/workspace_ide/phantomjs_casperjs/casperjs/hudson_dev_cpm_delete.js casper = require("casper").create({ viewportSize: {width: 1366, height: 768}, onError: function (msg, backtrace) { this.echo("出现错误信息:\n" + msg + "\n" + backtrace); }, onAlert: function (msg) { this.echo("Alert:\n" + msg); } }); //要删除任务名称 jobName="dev_cpm_disabled"; //hudson的地址 url = "http://192.168.0.170:8081/hudson3/"; //删除最大历史记录数,用于循环 max_record=100; //启动 casper.start(url, function () { this.capture("/mnt/D/work_documents/workspace_ide/phantomjs_casperjs/images/1_start.png"); this.echo("启动程序...."); this.echo(this.getTitle()); }); //为登录准备 casper.then(function () { this.click("#login-field a"); //this.click(x('//td[@id="login-field"]//a[1]')); this.capture("/mnt/D/work_documents/workspace_ide/phantomjs_casperjs/images/2_login.png"); this.echo("点击登录的范围"); }); //输入登录信息 casper.then(function () { this.fill("form[id='loginForm']", { "j_username": "admin", "j_password": "admin" }, false); this.capture("/mnt/D/work_documents/workspace_ide/phantomjs_casperjs/images/3_login.png"); this.echo("等待点击登录按钮"); }); //点击登录按钮 casper.then(function () { this.click("div.ui-dialog-buttonset button"); this.capture("/mnt/D/work_documents/workspace_ide/phantomjs_casperjs/images/4_login.png"); this.echo("已经点击登录按钮, 跳转等待....."); this.wait(3000, function () { this.echo(this.getTitle()); this.capture("/mnt/D/work_documents/workspace_ide/phantomjs_casperjs/images/5_login_success.png"); this.echo("登录成功"); }); }); //登录成功后,准备进入任务的历史列表页面 casper.then(function () { //this.clickLable("dev_cpm_disabled", "a"); this.click("a[href='job/"+jobName+"/']"); this.echo("准备进入"+jobName+", 等待....."); this.wait(3000, function () { this.echo(this.getTitle()); this.capture("/mnt/D/work_documents/workspace_ide/phantomjs_casperjs/images/6_gogo_job_history.png"); this.echo("进入 "+jobName+" 成功"); }); }); //循环删除离历史 for(var i=0; i<max_record; i++){ var d=new Date(); //1.点击第一个历史,并进入细节页面 casper.then(function () { this.echo("---------------------------------------------------------------------------------------------"); //注意,这里选择器匹配多个的时候,只会默认点击第一个被匹配的元素 this.click("table[id='buildHistory'] a[class='tip']") this.echo("点击任务历史链接, 等待进入detai页面....."); this.wait(3000, function () { this.capture("/mnt/D/work_documents/workspace_ide/phantomjs_casperjs/images/"+ d.getTime()+"A.png"); this.echo("进入 "+jobName+" detail页面成功"); }); }); //2.点击右上角的删除按钮 casper.then(function () { this.click("input[id='deleteBuild']") this.echo("点击删除按钮, 等待弹出确认对话框....."); this.wait(2000, function () { this.capture("/mnt/D/work_documents/workspace_ide/phantomjs_casperjs/images/"+ d.getTime()+"B.png"); this.echo("删除 "+jobName+" 任务历史的确认对话框成功,等待确认..."); }); }); //3.确认删除,并自动跳转到历史列表页面 casper.then(function () { //注意,这里选择器匹配多个的时候,只会默认点击第一个被匹配的元素 this.click("div.ui-dialog-buttonset button"); this.echo("点击确认删除历史对话框, 等待跳转到历史里表....."); this.wait(3000, function () { this.capture("/mnt/D/work_documents/workspace_ide/phantomjs_casperjs/images/"+ d.getTime()+"C.png"); this.echo("删除成功,并成功跳转到历史列表"); }); }); //等待几秒钟后,继续循环 casper.then(function(){ this.echo("等待2秒钟,准备循环......"); this.wait(2000,function(){ d=new Date(); }); }) } casper.run(function () { this.echo("程序执行完毕..."); this.exit(); })