`

web自动化测试学习以及Javascript学习(四)

阅读更多

    这次原计划写些关于sahi的页面监视的内容,本来我的文章都在百度空间里,现在毕业了,就都搬过来了,前三篇baidu空间wcf1987都有。由于已经毕业了,东西遗失了一些,我就拿我论文中我自己仿造sahi实现的部分讲讲吧。注意:我的原理与sahi基本上一致(其实借鉴了很多。。。),但是sahi考虑了很多,特别是JavaScript在包括了ie与ff的不同,我就只专注了ie。

 

事件驱动模型的三大要素:事件源:能够接收外部事件的源体。侦听器:能够接收事件源通知的对象。事件处理程序:用于处理事件的对象。若要实现事件监听模块,需要给事件源注册侦听器。通常采用的方法是遍历页面上元素分别按照其类型注册侦听器。这种方法有个缺点:当页面元素很多时,如新浪网站有很多信息。这样页面加载时候就要一一为每个元素注册侦听器,会有所延迟。

在这里选择了一种巧妙的思路:考虑用户并不一定会浏览所有的信息,通常情况下是不会的。所以利用JavaScript里的注册侦听器方法,先给页面注册一个mouseover事件监听器。当用户想查看某条信息时,鼠标滑过时会执行mouseover的事件处理程序,在这个事件处理程序中加入对这个元素的注册函数,这样当用户执行某个动作时,如点击时,就能捕获用户的行为动作。

 

如图,Web首先调用了注入的JS文件中addHandlers方法,此方法给页面document对象添加了 this.addEvent(doc, "keyup", this.docEventHandler); this.addEvent(doc, "mousemove", this.docEventHandler)方法,同时重载了系统的函数window.alert;window.confirm;window.prompt; 添加了事件监听。

 

使得当用户执行了keyup或是mouseover动作之后就会产生一个事件(event),根据产生的事件得到触发该事件的元素—事件源,然后给这个元素注册相应的侦听器,即当发生特定类型的事件会触发相应的事件处理程序(JavaScript中称事件句柄)

统一的注册事件句柄的函数为attachEvents(t),针对特定文档元素的特定类型的事件如addEvent(el, "click",onEv); 这是给el对象注册了onclick事件,事件句柄为onEv

当特定事件发生时,比如onclick发生,则可以得到发生事件的页面元素的name,id,value,文本,类型等,将其传递给AccessorInfo,得到处理后的字符串s,利用实现了ActiveXObject异步PostsendToServer,发送结果到服务器。

 

最后需要指出的是,本文中采用的2DOM中的高级事件处理,在该模型中,通过调用方法注册事件句柄,而不是设置HTML属性或JavaScript属性来注册事件句柄,所以可以给一个指定的对象的指定类型的事件注册多个事件句柄。从而不至于影响了页面原始的JavaScript代码。

 

  效果图:

全部JS如下,sahi的我就不贴了。大家可以在sahi的htdocs\spr下找到,共有5个被注入到了网页,但是核心就一个concat.js。

 

  function ice(){
   
    this.real_confirm = window.confirm;
    this.real_prompt = window.prompt;
  
 this.real_alert = window.alert;
 window.alert = function (s){
      return fish.alertMock(s);
 };
    window.confirm = function (s){
  return fish.confirmMock(s);
  };
    window.prompt = function (s){
  return fish.promptMock(s);
  };
 
    //window.confirm = function (s){return fish.confirmMock(s)};
    //window.prompt = function (s){return fish.promptMock(s)};
   // window.print = function (s){return fish.printMock(s)};
}

ice.prototype.alertMock= function (s) {
   
         this.real_alert(s);
    var inp=document.getElementById("haha");
   //inp.value=s;
   var info= new AccessorInfo("null", "null", "alert", "null", "null");
         var s=info.createPostString();
        // alert(s);
     //   window.setTimeout("alert('"+s+"')",1000);
          window.setTimeout("fish.sendToServer('"+s+"')",0);
         
        //    fish.sendToServer(s);
     return;
  
}
ice.prototype.confirmMock= function (s) {
   
         var boo=this.real_confirm(s);
         var inp=document.getElementById("haha");
   inp.value=boo;
   //alert(boo);
    var info= new AccessorInfo("null", "null", "confirm", "null", boo);
         var s=info.createPostString();
        // alert(s);
         window.setTimeout("fish.sendToServer('"+s+"')",0);
           // fish.sendToServer(s);
     return;
  
}
ice.prototype.promptMock= function (s) {
   
       var boo=this.real_prompt(s);
         var inp=document.getElementById("haha");
   inp.value=boo;
   //alert(boo);
    var info= new AccessorInfo("null", "null", "prompt", "null", boo);
         var s=info.createPostString();
        // alert(s);
         window.setTimeout("fish.sendToServer('"+s+"')",0);
          //  fish.sendToServer(s);
     return;
  
}


ice.prototype.getKnowedTags = function (src){
    var el = src;
 
    while (true) {
  
        if (!el) return src;
        if (!el.tagName || el.tagName.toLowerCase() == "html" || el.tagName.toLowerCase() == "body") return null;
        var tag = el.tagName.toLowerCase();
        if (tag == "a" || tag == "select" || tag == "img" || tag == "form" ||
  tag == "input" ||
  tag == "button" ||
  tag == "textarea" ||
  tag == "textarea" ||
  tag == "td" ||
  tag == "table" ||
  ((tag == "div" || tag == "span")) ||
  tag == "label" ||
  tag == "li") {
   //alert("tag:"+el);
   return el;
  }
        el = el.parentNode;
    }
}
ice.prototype.addHandlers = function () {
  
    var doc = document;
    this.addEvent(doc, "keyup", this.docEventHandler);
    this.addEvent(doc, "mousemove", this.docEventHandler);
}

ice.prototype.addEvent = function (el, ev, fn) {
          el.attachEvent("on" + ev, fn);
  
}
ice.prototype.docEventHandler = function () {
 
    var e = window.event;
 
    var t =fish.getKnowedTags(e.srcElement);
 
 
    if (t && !t.hasAttached && t.tagName) {
  
        var tag = t.tagName.toLowerCase();
        if (tag == "select"||tag == "a" || t.form || tag == "img" || tag == "div" || tag == "span" || tag == "li" || tag == "td" || tag == "table"
         || tag == "input" || tag == "textarea" || tag == "button") {
         
      fish.attachEvents(t);
        }
        t.hasAttached = true;
    }

}
ice.prototype.attachEvents = function (el) {
 
    var tagName = el.tagName.toLowerCase();
    if (tagName == "a") {
        this.attachLinkEvents(el)

    } else if (el.type) {
        this.attachFormElementEvents(el);
    } else if (tagName == "img" || tagName == "div" || tagName == "span" || tagName == "td" || tagName == "table" || tagName == "li") {
        this.attachImageEvents(el);
    }
}


ice.prototype.attachLinkEvents = function (el) {
    this.addEvent(el, "click", this.onEv);
}
ice.prototype.attachFormElementEvents = function (el) {
    var type = el.type;
 
    if (el.onchange == this.onEv || el.onblur == this.onEv || el.onclick == this.onEv) return;
 
    if (type == "text" || type == "file" || type == "textarea" || type == "password") {
  
        this.addEvent(el, "change", this.onEv);
    } else if (type == "select-one" || type == "select-multiple") {
        fish.addEvent(el, "change", this.onEv);
    } else if (type == "button" || type == "submit" || type == "reset" || type == "checkbox" || type == "radio" || type == "image") {
        fish.addEvent(el, "click", this.onEv);
    }
}
ice.prototype.attachImageEvents = function (el) {
    this.addEvent(el, "click", this.onEv);
}
ice.prototype.onEv = function (e) {
     e.cancelBubble=true;
   
    var targ = fish.getKnowedTags(e.srcElement);
 
    if (e.type == "click") {
        if (targ.type) {
            var type = targ.type;
            if (type == "text" || type == "textarea" || type == "password"
                || type == "select-one" || type == "select-multiple") return;
        }
    }

    var info = fish.getInfo(targ);
 //alert(info.id);
 //alert(info.name);
 //alert(info.value);
 //alert(info.type);
// alert(info.event);
 
 var s=info.createPostString();
 //alert(s);
    fish.sendToServer(s);
     
}

ice.prototype.getInfo=function (el){
   
    var type = el.type;
    var tagLC = el.tagName.toLowerCase();
    if (tagLC == "img") {
        return new AccessorInfo(el.id, el.name, "img", "click",el.src);
    } else if (type == "text" || type == "textarea" || type == "password") {
        return new AccessorInfo(el.id, el.name, type, "setvalue", el.value);
    } else if (type == "select-one" || type == "select-multiple") {
        return new AccessorInfo(el.id, el.name, type, "setselected", el.value);
    } else if (tagLC == "a") {
        return new AccessorInfo(el.id, el.innerText, "link", "click" ,el.href);
    } else if (type == "button" || type == "reset" || type == "submit" || type == "image") {
        return new AccessorInfo(el.id, el.name, type, "click",el.value);
    } else if (type == "checkbox" || type == "radio") {
        return new AccessorInfo(el.id, el.name, type, "click", el.value);
    } else if (type == "file") {
        return new AccessorInfo(el.id, el.name, type, "setFile", el.value);
    } else if (tagLC == "td") {
    //    return new AccessorInfo(el.id, el.name, "cell", "click", el.innerText);
    } else if (tagLC == "div" || tagLC == "span") {
      //  return new AccessorInfo(el.id, el.name, "spandiv", "click", el.innerText);
    } else if (tagLC == "label") {
      //  return new AccessorInfo(el.id, el.name, "label", "click", el.innerText);
    } else if (tagLC == "li") {
      //  return new AccessorInfo(el.id, el.name, "listItem", "click", el.innerText);
    }
}

var AccessorInfo = function (id, name, type, event, value) {
    this.name=name;
 this.id=id;
    this.type = type;
    this.event = event;
    this.value = value;
}
function addPostParm(sp,paramname,paramvalue){
 
 if(sp.length>0){
  sp+="&icespy&";
 }
 if(paramvalue==null){
  
  paramvalue="null";
 }
  if(paramvalue.length==null){
  
  paramvalue="null";
 }
 if(paramvalue.length==0){
  paramvalue="null"
 }
 //encodeURIComponent;
 
 return sp+encodeURIComponent(paramname)+"="+encodeURIComponent(paramvalue);
}
AccessorInfo.prototype.createPostString=function(){
 var s="";
 s=addPostParm(s,"type",this.type);
 s=addPostParm(s,"name",this.name);
 s=addPostParm(s,"id",this.id);
 s=addPostParm(s,"value",this.value);
 s=addPostParm(s,"event",this.event);
 return s; 
}
ice.prototype. Create=function(){
 var aSignal=("MSXML2.XMLHTTP.5.0");
 
 var oRequest=new ActiveXObject(aSignal);
 return oRequest;
 
}
ice.prototype.sendToServer=function (s){
 var http=fish.Create();
 http.open("post","icespy.test",false);
 http.setRequestHeader("Content-Type","application/x-wwww-form-urlencoded");
 http.send(s);
 //alert(s);
 return "sucess";
}

分享到:
评论

相关推荐

    JavaScript在web自动化测试中的作用示例详解

    JS的全称JavaScript,是一种运行在浏览器中的解释型脚本语言,通常用来实现web前端页面的基本功能,对于前端开发人员是不得不掌握的一门基本技能,但是对于做web自动化测试的人员来说,如果为了实施自动化测试专门...

    Web自动化测试面试题

    Web自动化测试 Selenium中hidden或者是display = none的元素是否可以定位到? 不能,可以写JavaScript将标签中的hidden先改为0,再定位元素 Selenium中如何保证操作元素的成功率?也就是说如何保证我点击的元素一定...

    selenium web自动化测试框架

    这是一个完整的web自动化测试框架,使用的是selenium+unittest+ddt框架。包含使用po模型方式、关键字方式。有数据驱动、日志监控、报告生成。

    使用Sahi实现Web自动化测试

    Sahi是TytoSoftware旗下的一个基于业务的开源Web应用自动化测试工具。Sahi运行为一个代理服务器,并...在当前全球软件都在追求高效、敏捷的开发模式的大背景下,Web自动化测试成为了新一波技术探讨和研究的热潮。因为

    "Kunlun-ATP:基于Python的自动化测试平台源码,整合JavaScript、HTML、CSS"

    项目简介:Kunlun-ATP 是一个基于 Python 的自动化测试平台,其源码融合了 JavaScript、HTML、CSS 等多种编程语言,旨在通过命令行界面提供便捷的测试自动化服务。 技术构成: - 主要语言:Python - 整合语言:...

    超全的Web渗透自我学习资料合集(64篇).zip

    web渗透: 自动化漏洞扫描 web渗透: Google Hacking web渗透: web服务器指纹识别 web渗透: 枚举web服务器应用 web渗透: 识别web应用框架 web渗透: 配置管理测试 web渗透: 身份管理测试 web渗透: 不安全的HTTP方法 web...

    前端学习笔记网站(HTML5+CSS3+JavaScript前端网页web课程设计).rar

    3、本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也可作为毕设项目、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶...

    devops-web-test:网络自动化测试流水线工具

    定制流水线式执行web自动化测试的工具。 推荐的目录结构 不同插件的默认值是按照以下目录结构来设置值的,也是我们推荐的目录结构。 project_path 开发运维 devops-app(用于DevOps执行自动化测试和输出测试产物)...

    用selenium进行自动化测试

    Selenium 是 ThoughtWorks 专门为 Web 应用程序编写的一个验收测试工具。据 Selenium 主页所说,与其他测试工具相比,使用 Selenium 的最大好处是: Selenium 测试直接在浏览器中运行,就像真实用户所做的一样。...

    go-webfriend:您在现代Web自动化和测试中的友好朋友

    “您在现代Web自动化和测试中的友好朋友。” 阿尔法警报 该软件正在大量开发中,不应视为已投入生产。 它大部分可以工作,但仍有许多工作尚未实现。 这是我对该项目的的完全重写。 目前缺少文档和示例,并且文档和...

    星月编程学习网站(HTML5+CSS3+JavaScript前端网页web课程设计).rar

    3、本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也可作为课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。...

    MonkeytestJS:面向前端Web开发人员的自动化功能测试

    为什么当周围有很多工具可以编写基于浏览器的自动化测试(例如 , , , 和 )时,很少有前端开发人员使用它们? 我们考虑了一下,并决定现有工具不适合我们的工作方式。 使用它们并不难,但是关于它们的某些东西...

    selenium-remote-control-0.8.0.zip_Selenium web 测试_remote_seleniu

    开源web自动化测试工具,核心是javascript开发的。很适合用于火狐浏览器的自动化测试

    利用Sahi脚本自动生成代码加速Web自动化测试开发

    本文向大家介绍如何编写一个Sahi的脚本以自动生成应用对象层的代码从而简化和加速Web自动化测试用例的开发。之所以有可能开发一个Sahi脚本来生成应用对象层的代码,主要得益于以下几个方面:Dojo本身将页面中的控件...

    基于HTML+JavaScript实现的web版写作业帮助器源码.tar

    基于HTML+JavaScript实现的web版写作业帮助器源码.tar基于HTML+JavaScript实现的web版写作业帮助器源码.tar基于HTML+JavaScript实现的web版写作业帮助器源码.tar基于HTML+JavaScript实现的web版写作业帮助器源码.tar...

Global site tag (gtag.js) - Google Analytics