# 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文件并返回給客戶端。
所以上面的代碼也就是我上面說的過程。
- 前言
- appium框架之bootstrap
- bootstrap之Click事件
- bootstrap之WaitForIdle&&Clear
- bootstrap之Orientation
- bootstrap之Swipe
- bootstrap之Flick
- bootstrap之Drag
- bootstrap之Pinch
- bootstrap之鼠標操作
- bootstrap之文本框的操作
- bootstrap之GetName&&GetAttribute&&GetDeviceSize&&GetSize&&GetLocation&&GetDataDir
- bootstrap之ScrollTo
- bootstrap之Wake&&PressBack&&TakeScreenshot&&OpenNotification
- bootstrap之PressKeyCode&&LongPressKeyCode
- bootstrap之DumpWindowHierarchy
- bootstrap之UpdateStrings
- bootstrap之MultiPointerGesture