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

 

poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标。如果对课程感兴趣,请大家咨询qq:908821478。

UpdateStrings

package io.appium.android.bootstrap.handler;

import io.appium.android.bootstrap.AndroidCommand;

import io.appium.android.bootstrap.AndroidCommandResult;

import io.appium.android.bootstrap.CommandHandler;

import io.appium.android.bootstrap.Logger;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import org.json.JSONObject;

/**

* This handler is used to update the apk strings.

*

*/

public class UpdateStrings extends CommandHandler {

/**

* strings.json文件保存的是apk的strings.xml里的内容,在Bootstrap启动前由appium服务器解析并push到设备端的

*

* @return

*/

public static boolean loadStringsJson() {

Logger.debug("Loading json...");

try {

final String filePath = "/data/local/tmp/strings.json";

final File jsonFile = new File(filePath);

// json will not exist for apks that are only on device

// 你的case必须写明apk的路径,如果启动设备上已有的应用而case中没有app路径,此时json文件是不存在的

// because the node server can't extract the json from the apk.

if (!jsonFile.exists()) {

return false;

}

final DataInputStream dataInput = new DataInputStream(

new FileInputStream(jsonFile));

final byte[] jsonBytes = new byte[(int) jsonFile.length()];

dataInput.readFully(jsonBytes);

// this closes FileInputStream

dataInput.close();

final String jsonString = new String(jsonBytes, "UTF-8");

// 将读取出来的信息赋给Find类中的属性,以做后用

Find.apkStrings = new JSONObject(jsonString);

Logger.debug("json loading complete.");

} catch (final Exception e) {

Logger.error("Error loading json: " + e.getMessage());

return false;

}

return true;

}

/*

* @param command The {@link AndroidCommand} used for this handler.

*

* @return {@link AndroidCommandResult}

*

* @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.

* bootstrap.AndroidCommand)

*/

@Override

public AndroidCommandResult execute(final AndroidCommand command) {

if (!loadStringsJson()) {

return getErrorResult("Unable to load json file and update strings.");

}

return getSuccessResult(true);

}

}

在appium初始化的时候,如果你代码中添加了app应用,而不是启动手机设备中已经有的应用,这时候appium会将该app解析,并提取出设备当前语言环境的strings.xml文件里的信息保存在strings.json里,并将其push到手机的/data/local/tmp目录下,当你想要获取应用中用到的字符串时,手机会去该目录下读取strings.json文件并返回给客户端。

所以上面的代码也就是我上面说的过程。

最新文章

  1. SQLite学习笔记(七)&&事务处理
  2. redis命令全集(自用)
  3. 37 网络相关函数(五)——live555源码阅读(四)网络
  4. js对汉字首字母排序
  5. pragma伪指令
  6. Oracle表连接总结
  7. [HNOI2016]大数
  8. Python cmp() 函数
  9. 4-(基础入门篇)学会刷Wi-Fi模块固件(刷AT指令固件)
  10. git add 文件夹
  11. 如何使用Django 启动命令行及执行脚本
  12. 计算a+b
  13. Uva 12889 One-Two-Three
  14. 一、python小功能记录——监听键盘事件
  15. Linux 判断系统任务是否正在运行
  16. C语言跟内存分配方式-alloc malloc calloc
  17. linux 配置SSH网络传输数据安全方案,JDK,Tomcat和Eclipes
  18. LWIP协议栈1
  19. python爬取淘宝排名
  20. 多语言中的“默认语言”设置

热门文章

  1. 对象this的引用
  2. 测试工作中ADB命令实战
  3. ASP.NET MVC 项目直接预览PDF文件
  4. 推荐一款好用的WSL终端模拟器
  5. 每天一个linux命令(41)--ping命令
  6. KEEP!
  7. HTML重要标签及属性详解
  8. 【2017-03-05】函数基础、函数四种结构、ref和out参数、递归
  9. wemall app商城源码Android短信监听接收器
  10. 获取手机 id 与 ip