1. 创建assets文件夹

工程上右键New-->Folder-->Assets Folder

Uiautomator读取properties文件

2. 在assets文件夹中创建prop文件

在assets文件夹中右键New-->File,输入名称xxx.prop

Uiautomator读取properties文件

 3. 在prop文件中添加参数,格式为 key=Value ,如

time = 100
name = qq

4. 封装读取差数方法:

    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

相关文章:

  • 2021-09-24
  • 2022-02-03
  • 2021-06-13
  • 2021-08-05
猜你喜欢
  • 2021-12-17
  • 2022-01-19
  • 2022-12-23
  • 2022-01-16
  • 2021-05-19
相关资源
相似解决方案