手机自动化测试:Appium源码分析之跟踪代码分析五

 

手机自动化测试是未来很重要的测试技术,作为一名测试人员应该熟练掌握,POPTEST举行手机自动化测试的课程,希望可以训练出优秀的手机测试开发工程师。

queue

我们先了解一下事件的集中处理方式,参考文章。从这篇文章可以知道,nodejs提供了一个Async库,该库就是用来处理事件的。里面就有这一个queue。 
在上一篇文章我们讲到了initQueue,我们这次再来看看:

Android.prototype.initQueue = function () {

this.queue = async.queue(function (task, cb) {

var action = task.action,

params = task.params;

this.cbForCurrentCmd = cb;

if (this.adb && !this.shuttingDown) {

this.uiautomator.sendAction(action, params, function (response) {

this.cbForCurrentCmd = null;

if (typeof cb === 'function') {

this.respond(response, cb);

}

}.bind(this));

} else {

this.cbForCurrentCmd = null;

var msg = "Tried to send command to non-existent Android device, " +

"maybe it shut down?";

if (this.shuttingDown) {

msg = "We're in the middle of shutting down the Android device, " +

"so your request won't be executed. Sorry!";

}

this.respond({

status: status.codes.UnknownError.code

, value: msg

}, cb);

}

}.bind(this), 1);

};

Async库中queue的定义一般如下所示:

var q = async.queue(function(task, callback) {

log(‘worker is processing task: ‘, task.name);

task.run(callback);

}, 1);

上面定义了一个工作线程为1的队列。当队列正在处理事件时,其他事件就一直等待。所以当我们使用queue的push方法时,queue会自己唤起然后执行,如果正在执行,push添加进来的事件会等到下一次执行。

uiautomator.sendAction

上面的请求,最终调用的是devices/android/uiautomator的sendAction的方法:

UiAutomator.prototype.sendAction = function (action, params, cb) {

if (typeof params === "function") {

cb = params;

params = {};

}

var extra = {action: action, params: params};

this.sendCommand('action', extra, cb);

};

然后转向sendCommand方法

uiautomator.sendCommand

UiAutomator.prototype.sendCommand = function (type, extra, cb) {

if (this.cmdCb !== null) {

logger.warn("Trying to run a command when one is already in progress. " +

"Will spin a bit and try again");

var start = Date.now();

var timeoutMs = 10000;

var intMs = 200;

var waitForCmdCbNull = function () {

if (this.cmdCb === null) {

this.sendCommand(type, extra, cb);

} else if ((Date.now() - start) < timeoutMs) {

setTimeout(waitForCmdCbNull, intMs);

} else {

cb(new Error("Never became able to push strings since a command " +

"was in process"));

}

}.bind(this);

waitForCmdCbNull();

} else if (this.socketClient) {

this.resendLastCommand = function () {

this.sendCommand(type, extra, cb);

}.bind(this);

if (typeof extra === "undefined" || extra === null) {

extra = {};

}

var cmd = {cmd: type};

cmd = _.extend(cmd, extra);

var cmdJson = JSON.stringify(cmd) + "\n";

this.cmdCb = cb;

var logCmd = cmdJson.trim();

if (logCmd.length > 1000) {

logCmd = logCmd.substr(0, 1000) + "...";

}

this.debug("Sending command to android: " + logCmd);

this.socketClient.write(cmdJson);

} else {

cb({

status: status.codes.UnknownError.code

, value: "Tried to send command to non-existent Android socket, " +

"maybe it's shutting down?"

});

}

};

最终会向设备端的socket发送一条命令,关于设备端接受命令以及如何处理,请查看我之前写的关于appium中bootstrap源码分析相关文章

手机自动化测试:Appium源码分析之跟踪代码分析五的更多相关文章

  1. 手机自动化测试:Appium源码分析之跟踪代码分析九

    手机自动化测试:Appium源码分析之跟踪代码分析九   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家 ...

  2. 手机自动化测试:Appium源码分析之跟踪代码分析八

    手机自动化测试:Appium源码分析之跟踪代码分析八   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家 ...

  3. 手机自动化测试:Appium源码分析之跟踪代码分析七

    手机自动化测试:Appium源码分析之跟踪代码分析七   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.poptest推出手机自 ...

  4. 手机自动化测试:Appium源码分析之跟踪代码分析六

    手机自动化测试:Appium源码分析之跟踪代码分析六   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.poptest推出手机自 ...

  5. 手机自动化测试:appium源码分析之bootstrap三

    手机自动化测试:appium源码分析之bootstrap三   研究bootstrap源码,我们可以通过代码的结构,可以看出来appium的扩展思路和实现方式,从中可以添加我们自己要的功能,针对app ...

  6. 手机自动化测试:appium源码分析之bootstrap二

    手机自动化测试:appium源码分析之bootstrap二   在bootstrap项目中的io.appium.android.bootstrap.handler包中的类都是对应的指令类, priva ...

  7. 手机自动化测试:appium源码分析之bootstrap一

    手机自动化测试:appium源码分析之bootstrap一   前言: poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.popte ...

  8. 手机自动化测试:appium源码分析之bootstrap十七

    手机自动化测试:appium源码分析之bootstrap十七   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

  9. 手机自动化测试:appium源码分析之bootstrap十六

    手机自动化测试:appium源码分析之bootstrap十六   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...

随机推荐

  1. ubuntu12.04下安装pptp_vpn服务器

    1.下载安装apt-get install pptpd 2.配置/etc/pptpd.confvim /etc/pptpd.conf添加下面两行(在配置文件的最后取消注释修改IP即可)localip ...

  2. 锋利的jQuery中的事件与动画

    奋夜的奋斗  ----  事件与动画 ----  来自地狱的战镰 小小的单词难不倒我们哦!!!!!!!    bind:绑定     unbind:接触绑定    toggle:栓牢   fadeou ...

  3. 使用Python以优雅的方式实现根据shp数据对栅格影像进行切割

    目录 前言 涉及到的技术 优雅切割 总结 一.前言        前面一篇文章(使用Python实现子区域数据分类统计)讲述了通过geopandas库实现对子区域数据的分类统计,说白了也就是如何根据一 ...

  4. mongodb 安装到创建用户,认证auth,httpinterface

    今天花了一天时间来解开这个mongodb的谜团,如果有遇到了其他的问题,可以咨询我. #开始 2.6.10安装方式 不同版本后面设置用户权限方式有所差异#下载这个版本的mongodb mongodb- ...

  5. c++中,int *a=new int(120)语句的意思

    int *a=new int(120); 申请一个整型变量空间,赋初值为120,并定义一个整型指针a指向该地址空间 int *a=new int[120]; 申请120个整型变量空间,没有赋初值,并定 ...

  6. Ajax封装函数笔记

    Ajax封装函数: function ajax(method, url, data, success) { //打开浏览器 //1.创建一个ajax对象 var xhr = null; try { x ...

  7. JavaScript中非常强大的Swiper

    刚开始学习javaScript的时候,做轮播图(比如手机淘宝首页的广告位置)是使用html和css结合js的for语句.传参等知识写出来的.但学到js事件时,其实用Swiper更加好写,Swiper的 ...

  8. TypeScript 优秀开源项目大合集

    TypeScript出来有段时间了,也冒出了很多用TypeScript开发的优秀开源项目,搜寻了一些基于TypeScript项目,分享给大家: https://github.com/brookshi/ ...

  9. VS2008与opencv结合使用的方法

    使用平台:win7(32位)系统,opencv 2.3.1,vs 2008. 目前在需要在vs上运行KCF视觉跟踪算法,其中有用到opencv的库,刚刚开始遇到的如下问题: 1. 计算机无法找到ope ...

  10. centOS7 mini配置linux服务器(二) 配置IP

    1.登录root用户,输入指令  #ip addr 可以看到除lo外的属于你的网卡配置. 2.输入 #cd /etc/sysconfig/network-scripts/         #vi if ...