root / trunk / web / dojo / dojox / robot / recorder.js
History | View | Annotate | Download (8.43 KB)
| 1 | 9 | andrej.cim | /*
|
|---|---|---|---|
| 2 | Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
||
| 3 | Available via Academic Free License >= 2.1 OR the modified BSD license.
|
||
| 4 | see: http://dojotoolkit.org/license for details
|
||
| 5 | */
|
||
| 6 | |||
| 7 | |||
| 8 | if(!dojo._hasResource["dojox.robot.recorder"]){ |
||
| 9 | dojo._hasResource["dojox.robot.recorder"]=true; |
||
| 10 | dojo.provide("dojox.robot.recorder");
|
||
| 11 | dojo.experimental("dojox.robot.recorder");
|
||
| 12 | (function(){
|
||
| 13 | var _1=1000; |
||
| 14 | var _2=500; |
||
| 15 | var _3=10000; |
||
| 16 | var _4=[];
|
||
| 17 | var _5=0; |
||
| 18 | var _6=null; |
||
| 19 | var _7=null; |
||
| 20 | var _8=function(){ |
||
| 21 | alert("Started recording.");
|
||
| 22 | _4=[]; |
||
| 23 | _6=new Date();
|
||
| 24 | _7=new Date();
|
||
| 25 | }; |
||
| 26 | var _9=function(_a,_b){ |
||
| 27 | if(_6==null||_a=="doh.robot.keyPress"&&_b[0]==dojo.keys.ENTER&&eval("("+_b[2]+")").ctrl&&eval("("+_b[2]+")").alt){ |
||
| 28 | return;
|
||
| 29 | } |
||
| 30 | var dt=Math.max(Math.min(Math.round((new Date()).getTime()-_7.getTime()),_3),1); |
||
| 31 | if(_a=="doh.robot.mouseMove"){ |
||
| 32 | _b[2]=dt;
|
||
| 33 | }else{
|
||
| 34 | _b[1]=dt;
|
||
| 35 | } |
||
| 36 | _4.push({name:_a,args:_b});
|
||
| 37 | _7=new Date();
|
||
| 38 | }; |
||
| 39 | var _c=function(){ |
||
| 40 | var c=_4;
|
||
| 41 | if(c[0].name=="doh.robot.keyPress"&&(c[0].args[0]==dojo.keys.ENTER||c[0].args[0]==77)){ |
||
| 42 | c.splice(0,1); |
||
| 43 | } |
||
| 44 | for(var i=c.length-1;(i>=c.length-2)&&(i>=0);i--){ |
||
| 45 | if(c[i].name=="doh.robot.keyPress"&&c[i].args[0]==dojo.keys.ALT||c[i].args[0]==dojo.keys.CTRL){ |
||
| 46 | c.splice(i,1);
|
||
| 47 | } |
||
| 48 | } |
||
| 49 | for(i=0;i<c.length;i++){ |
||
| 50 | var _d,_e;
|
||
| 51 | if(c[i+1]&&c[i].name=="doh.robot.mouseMove"&&c[i+1].name==c[i].name&&c[i+1].args[2]<_2){ |
||
| 52 | _d=c[i+1];
|
||
| 53 | _e=0;
|
||
| 54 | while(_d&&_d.name==c[i].name&&_d.args[2]<_2){ |
||
| 55 | c.splice(i+1,1); |
||
| 56 | _e+=_d.args[2];
|
||
| 57 | c[i].args[0]=_d.args[0]; |
||
| 58 | c[i].args[1]=_d.args[1]; |
||
| 59 | _d=c[i+1];
|
||
| 60 | } |
||
| 61 | c[i].args[3]=_e;
|
||
| 62 | }else{
|
||
| 63 | if(c[i+1]&&c[i].name=="doh.robot.mouseWheel"&&c[i+1].name==c[i].name&&c[i+1].args[1]<_2){ |
||
| 64 | _d=c[i+1];
|
||
| 65 | _e=0;
|
||
| 66 | while(_d&&_d.name==c[i].name&&_d.args[1]<_2){ |
||
| 67 | c.splice(i+1,1); |
||
| 68 | _e+=_d.args[1];
|
||
| 69 | c[i].args[0]+=_d.args[0]; |
||
| 70 | _d=c[i+1];
|
||
| 71 | } |
||
| 72 | c[i].args[2]=_e;
|
||
| 73 | }else{
|
||
| 74 | if(c[i+2]&&c[i].name=="doh.robot.mouseMoveAt"&&c[i+2].name=="doh.robot.scrollIntoView"){ |
||
| 75 | var _f=c.splice(i+2,1)[0]; |
||
| 76 | c.splice(i,0,_f);
|
||
| 77 | }else{
|
||
| 78 | if(c[i+1]&&c[i].name=="doh.robot.mousePress"&&c[i+1].name=="doh.robot.mouseRelease"&&c[i].args[0]==c[i+1].args[0]){ |
||
| 79 | c[i].name="doh.robot.mouseClick";
|
||
| 80 | c.splice(i+1,1); |
||
| 81 | if(c[i+1]&&c[i+1].name=="doh.robot.mouseClick"&&c[i].args[0]==c[i+1].args[0]){ |
||
| 82 | c.splice(i+1,1); |
||
| 83 | } |
||
| 84 | }else{
|
||
| 85 | if(c[i+1]&&c[i-1]&&c[i-1].name=="doh.robot.mouseMoveAt"&&c[i].name=="doh.robot.mousePress"&&c[i+1].name=="doh.robot.mouseMove"){ |
||
| 86 | var cmd={name:"doh.robot.mouseMoveAt",args:[c[i-1].args[0],1,100,c[i-1].args[3]+1,c[i-1].args[4]]}; |
||
| 87 | c.splice(i+1,0,cmd); |
||
| 88 | }else{
|
||
| 89 | if(c[i+1]&&((c[i].name=="doh.robot.keyPress"&&typeof c[i].args[0]=="string")||c[i].name=="doh.robot.typeKeys")&&c[i+1].name=="doh.robot.keyPress"&&typeof c[i+1].args[0]=="string"&&c[i+1].args[1]<=_1&&!eval("("+c[i].args[2]+")").ctrl&&!eval("("+c[i].args[2]+")").alt&&!eval("("+c[i+1].args[2]+")").ctrl&&!eval("("+c[i+1].args[2]+")").alt){ |
||
| 90 | c[i].name="doh.robot.typeKeys";
|
||
| 91 | c[i].args.splice(3,1); |
||
| 92 | _d=c[i+1];
|
||
| 93 | var _10=0; |
||
| 94 | while(_d&&_d.name=="doh.robot.keyPress"&&typeof _d.args[0]=="string"&&_d.args[1]<=_1&&!eval("("+_d.args[2]+")").ctrl&&!eval("("+_d.args[2]+")").alt){ |
||
| 95 | c.splice(i+1,1); |
||
| 96 | c[i].args[0]+=_d.args[0]; |
||
| 97 | _10+=_d.args[1];
|
||
| 98 | _d=c[i+1];
|
||
| 99 | } |
||
| 100 | c[i].args[2]=_10;
|
||
| 101 | c[i].args[0]="'"+c[i].args[0]+"'"; |
||
| 102 | }else{
|
||
| 103 | if(c[i].name=="doh.robot.keyPress"){ |
||
| 104 | if(typeof c[i].args[0]=="string"){ |
||
| 105 | c[i].args[0]="'"+c[i].args[0]+"'"; |
||
| 106 | }else{
|
||
| 107 | if(c[i].args[0]==0){ |
||
| 108 | c.splice(i,1);
|
||
| 109 | }else{
|
||
| 110 | for(var j in dojo.keys){ |
||
| 111 | if(dojo.keys[j]==c[i].args[0]){ |
||
| 112 | c[i].args[0]="dojo.keys."+j; |
||
| 113 | break;
|
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | }; |
||
| 127 | var _11=function(){ |
||
| 128 | if(!_6){
|
||
| 129 | _8(); |
||
| 130 | }else{
|
||
| 131 | _12(); |
||
| 132 | } |
||
| 133 | }; |
||
| 134 | var _12=function(){ |
||
| 135 | var dt=Math.round((new Date()).getTime()-_6.getTime()); |
||
| 136 | _6=null;
|
||
| 137 | _c(); |
||
| 138 | var c=_4;
|
||
| 139 | if(c.length){
|
||
| 140 | var s="doh.register('dojox.robot.AutoGeneratedTestGroup',{\n"; |
||
| 141 | s+=" name: 'autotest"+(_5++)+"',\n"; |
||
| 142 | s+=" timeout: "+(dt+2000)+",\n"; |
||
| 143 | s+=" runTest: function(){\n";
|
||
| 144 | s+=" var d = new doh.Deferred();\n";
|
||
| 145 | for(var i=0;i<c.length;i++){ |
||
| 146 | s+=" "+c[i].name+"("; |
||
| 147 | for(var j=0;j<c[i].args.length;j++){ |
||
| 148 | var arg=c[i].args[j];
|
||
| 149 | s+=arg; |
||
| 150 | if(j!=c[i].args.length-1){ |
||
| 151 | s+=", ";
|
||
| 152 | } |
||
| 153 | } |
||
| 154 | s+=");\n";
|
||
| 155 | } |
||
| 156 | s+=" doh.robot.sequence(function(){\n";
|
||
| 157 | s+=" if(/*Your condition here*/){\n";
|
||
| 158 | s+=" d.callback(true);\n";
|
||
| 159 | s+=" }else{\n";
|
||
| 160 | s+=" d.errback(new Error('We got a failure'));\n";
|
||
| 161 | s+=" }\n";
|
||
| 162 | s+=" }, 1000);\n";
|
||
| 163 | s+=" return d;\n";
|
||
| 164 | s+=" }\n";
|
||
| 165 | s+="});\n";
|
||
| 166 | var div=document.createElement("div"); |
||
| 167 | div.id="dojox.robot.recorder";
|
||
| 168 | div.style.backgroundColor="white";
|
||
| 169 | div.style.position="absolute";
|
||
| 170 | var _13={y:(window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0),x:(window.pageXOffset||(window["dojo"]?dojo._fixIeBiDiScrollLeft(document.documentElement.scrollLeft):undefined)||document.body.scrollLeft||0)}; |
||
| 171 | div.style.left=_13.x+"px";
|
||
| 172 | div.style.top=_13.y+"px";
|
||
| 173 | var h1=document.createElement("h1"); |
||
| 174 | h1.innerHTML="Your code:";
|
||
| 175 | div.appendChild(h1); |
||
| 176 | var pre=document.createElement("pre"); |
||
| 177 | if(pre.innerText!==undefined){ |
||
| 178 | pre.innerText=s; |
||
| 179 | }else{
|
||
| 180 | pre.textContent=s; |
||
| 181 | } |
||
| 182 | div.appendChild(pre); |
||
| 183 | var _14=document.createElement("button"); |
||
| 184 | _14.innerHTML="Close";
|
||
| 185 | var _15=dojo.connect(_14,"onmouseup",function(e){ |
||
| 186 | dojo.stopEvent(e); |
||
| 187 | document.body.removeChild(div); |
||
| 188 | dojo.disconnect(_15); |
||
| 189 | }); |
||
| 190 | div.appendChild(_14); |
||
| 191 | document.body.appendChild(div); |
||
| 192 | _4=[]; |
||
| 193 | } |
||
| 194 | }; |
||
| 195 | var _16=function(_17){ |
||
| 196 | if(typeof _17=="string"){ |
||
| 197 | return "'"+_17+"'"; |
||
| 198 | }else{
|
||
| 199 | if(_17.id){
|
||
| 200 | return "'"+_17.id+"'"; |
||
| 201 | }else{
|
||
| 202 | var _18=document.getElementsByTagName(_17.nodeName);
|
||
| 203 | var i;
|
||
| 204 | for(i=0;i<_18.length;i++){ |
||
| 205 | if(_18[i]==_17){
|
||
| 206 | break;
|
||
| 207 | } |
||
| 208 | } |
||
| 209 | return "function(){ return document.getElementsByTagName('"+_17.nodeName+"')["+i+"]; }"; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | }; |
||
| 213 | var _19=function(b){ |
||
| 214 | return "{left:"+(b==0)+", middle:"+(b==1)+", right:"+(b==2)+"}"; |
||
| 215 | }; |
||
| 216 | var _1a=function(e){ |
||
| 217 | return "{'shift':"+(e.shiftKey)+", 'ctrl':"+(e.ctrlKey)+", 'alt':"+(e.altKey)+"}"; |
||
| 218 | }; |
||
| 219 | dojo.connect(document,"onkeydown",function(e){ |
||
| 220 | if((e.keyCode==dojo.keys.ENTER||e.keyCode==77)&&e.ctrlKey&&e.altKey){ |
||
| 221 | dojo.stopEvent(e); |
||
| 222 | _11(); |
||
| 223 | } |
||
| 224 | }); |
||
| 225 | var _1b={type:""}; |
||
| 226 | var _1c=function(e){ |
||
| 227 | if(!e||_1b.type==e.type&&_1b.button==e.button){
|
||
| 228 | return;
|
||
| 229 | } |
||
| 230 | _1b={type:e.type,button:e.button};
|
||
| 231 | var _1d=_16(e.target);
|
||
| 232 | var _1e=dojo.coords(e.target);
|
||
| 233 | _9("doh.robot.mouseMoveAt",[_1d,0,100,e.clientX-_1e.x,e.clientY-_1e.y]); |
||
| 234 | _9("doh.robot.mousePress",[_19(e.button-(dojo.isIE?1:0)),0]); |
||
| 235 | }; |
||
| 236 | var _1f=function(e){ |
||
| 237 | if(!e||_1b.type==e.type&&_1b.button==e.button){
|
||
| 238 | return;
|
||
| 239 | } |
||
| 240 | _1b={type:e.type,button:e.button};
|
||
| 241 | var _20=_16(e.target);
|
||
| 242 | var _21=dojo.coords(e.target);
|
||
| 243 | _9("doh.robot.mouseClick",[_19(e.button-(dojo.isIE?1:0)),0]); |
||
| 244 | }; |
||
| 245 | var _22=function(e){ |
||
| 246 | if(!e||_1b.type==e.type&&_1b.button==e.button){
|
||
| 247 | return;
|
||
| 248 | } |
||
| 249 | _1b={type:e.type,button:e.button};
|
||
| 250 | var _23=_16(e.target);
|
||
| 251 | var _24=dojo.coords(e.target);
|
||
| 252 | _9("doh.robot.mouseRelease",[_19(e.button-(dojo.isIE?1:0)),0]); |
||
| 253 | }; |
||
| 254 | var _25=function(e){ |
||
| 255 | if(!e||_1b.type==e.type&&_1b.pageX==e.pageX&&_1b.pageY==e.pageY){
|
||
| 256 | return;
|
||
| 257 | } |
||
| 258 | _1b={type:e.type,pageX:e.pageX,pageY:e.pageY};
|
||
| 259 | _9("doh.robot.mouseMove",[e.pageX,e.pageY,0,100,true]); |
||
| 260 | }; |
||
| 261 | var _26=function(e){ |
||
| 262 | if(!e||_1b.type==e.type&&_1b.pageX==e.pageX&&_1b.pageY==e.pageY){
|
||
| 263 | return;
|
||
| 264 | } |
||
| 265 | _1b={type:e.type,detail:(e.detail?(e.detail):(-e.wheelDelta/120))};
|
||
| 266 | _9("doh.robot.mouseWheel",[_1b.detail]);
|
||
| 267 | }; |
||
| 268 | var _27=function(e){ |
||
| 269 | if(!e||_1b.type==e.type&&(_1b.charCode==e.charCode&&_1b.keyCode==e.keyCode)){
|
||
| 270 | return;
|
||
| 271 | } |
||
| 272 | _1b={type:e.type,charCode:e.charCode,keyCode:e.keyCode};
|
||
| 273 | _9("doh.robot.keyPress",[e.charOrCode==dojo.keys.SPACE?" ":e.charOrCode,0,_1a(e)]); |
||
| 274 | }; |
||
| 275 | var _28=function(e){ |
||
| 276 | if(!e||_1b.type==e.type&&(_1b.charCode==e.charCode&&_1b.keyCode==e.keyCode)){
|
||
| 277 | return;
|
||
| 278 | } |
||
| 279 | _1b={type:e.type,charCode:e.charCode,keyCode:e.keyCode};
|
||
| 280 | }; |
||
| 281 | dojo.connect(document,"onmousedown",_1c);
|
||
| 282 | dojo.connect(document,"onmouseup",_22);
|
||
| 283 | dojo.connect(document,"onclick",_1f);
|
||
| 284 | dojo.connect(document,"onkeypress",_27);
|
||
| 285 | dojo.connect(document,"onkeyup",_28);
|
||
| 286 | dojo.connect(document,"onmousemove",_25);
|
||
| 287 | dojo.connect(document,!dojo.isMozilla?"onmousewheel":"DOMMouseScroll",_26); |
||
| 288 | dojo.addOnLoad(function(){
|
||
| 289 | if(dojo.window){
|
||
| 290 | dojo.connect(dojo.window,"scrollIntoView",function(_29){ |
||
| 291 | _9("doh.robot.scrollIntoView",[_16(_29)]);
|
||
| 292 | }); |
||
| 293 | } |
||
| 294 | }); |
||
| 295 | dojo.connect(dojo,"connect",function(_2a,_2b,f){ |
||
| 296 | if(_2a&&(!f||!f._mine)){
|
||
| 297 | var _2c=null; |
||
| 298 | if(_2b.toLowerCase()=="onmousedown"){ |
||
| 299 | _2c=dojo.hitch(this,_1c);
|
||
| 300 | }else{
|
||
| 301 | if(_2b.toLowerCase()==(!dojo.isMozilla?"onmousewheel":"dommousescroll")){ |
||
| 302 | _2c=dojo.hitch(this,_26);
|
||
| 303 | }else{
|
||
| 304 | if(_2b.toLowerCase()=="onclick"){ |
||
| 305 | _2c=dojo.hitch(this,_1f);
|
||
| 306 | }else{
|
||
| 307 | if(_2b.toLowerCase()=="onmouseup"){ |
||
| 308 | _2c=dojo.hitch(this,_22);
|
||
| 309 | }else{
|
||
| 310 | if(_2b.toLowerCase()=="onkeypress"){ |
||
| 311 | _2c=dojo.hitch(this,_27);
|
||
| 312 | }else{
|
||
| 313 | if(_2b.toLowerCase()=="onkeyup"){ |
||
| 314 | _2c=dojo.hitch(this,_28);
|
||
| 315 | } |
||
| 316 | } |
||
| 317 | } |
||
| 318 | } |
||
| 319 | } |
||
| 320 | } |
||
| 321 | if(_2c==null){ |
||
| 322 | return;
|
||
| 323 | } |
||
| 324 | _2c._mine=true;
|
||
| 325 | dojo.connect(_2a,_2b,_2c); |
||
| 326 | } |
||
| 327 | }); |
||
| 328 | })(); |
||
| 329 | } |