1. 创建assets文件夹
工程上右键New-->Folder-->Assets Folder
2. 在assets文件夹中创建prop文件
在assets文件夹中右键New-->File,输入名称xxx.prop
3. 在prop文件中添加参数,格式为 key=Value ,如
time = 100
name = qq
4. 封装读取差数方法
方法一(通过Context):
public String getProperties(Context c, String s) {
String getValue = null;
Properties properties = new Properties();
try {
properties.load(c.getAssets().open("***.prop"));
getValue = properties.getProperty(s);
} catch (Exception e) {
e.printStackTrace();
}
return getValue;
}
5. 在Uiautomator测试脚本中调用
getProperties(InstrumentationRegistry.getTargetContext(), "key");
注意,这里需要用到getTargetContext()而不是getContext()
getContext(): 返回instrumentation对应包的Context
getTargetContext(): 返回一个目标应用程序的Context
通俗一点说明就是:
如果是使用应用apk相关的内容就用getTargetContext(), 如果是测试apk相关的就用getContext()
我们这里的prop文件是在应用路径下,所以是getTargetContext()
具体区别可以参考:https://stackoverflow.com/questions/29969545/whats-the-difference-between-gettargetcontext-and-getcontext-on-instrumentat
方法二(通过类加载器):
public String getProperties(String s) {
Properties prop = new Properties();
try {
prop.load(FileClass.class.getResourceAsStream("/assets/test.prop"));
} catch (IOException e) {
e.printStackTrace();
}
String getValue = prop.getProperty(s);
return getValue;
}
其中,FileClass为当前类。然后通过普通的调用即可读取到配置文件内容。