zoukankan      html  css  js  c++  java
  • bootstrap之UpdateStrings

    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文件并返回给client。

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





  • 相关阅读:
    spring boot web开发使用kindeditor中调用地图功能的异常情况"此内容不能显示在一个框架中"
    mysql在spring+mybatis中出现value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp的几种种解决办法
    spring oauth2 的认证流程
    mingw32 QT环境在集成一些librtmp代码由于zlib出现的一些错误:undefined reference to `inflate'
    QT下c和c++混编问题
    产品与开发那些事
    Qt5.6 windows下vs2015(vs2012)编译ODBC
    转 2018 疯狂微服务之死
    Source Tree时免登录安装
    Win7系统下局域连接OPC配置
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5351851.html
Copyright © 2011-2022 走看看