多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

Robotium 跨进程(底层设备事件注入)-自动化测试

更新时间:2022-04-27 09:09:48 作者:多测师 浏览:222

  Android跨进程注入最大的问题在于Android上层对跨进程注入事件的权限限制,那么能不能绕开上层直接操作底层设备呢?答案是肯定的。

Robotium 跨进程(底层设备事件注入)-自动化测试

  传统方法:

  1、Monkey Server 利用Monkey进程跨进程注入事件的权限

  2、利用Uiautomator:Android提供了接口返回Uiautomation实例,在Robotium Case中可直接使用

  To get an instance of UiAutomation, call Instrumentation.getUiAutomation()

  UiAutomation mUiAutomation = getInstrumentation.getUiAutomation();

  3、利用adb注入事件

  4、抛开以上曲线救国的方法,这里重点要讲的是以下内容:

  直接往linux底层/dev/input/event*写事件

  该部分需要用C来完成,ndk-build编译成.so供上层调用

  Like this:

  C:

  JNIEXPORT void JNICALL Java_com_example_jnidemo_MainActivity_inject(JNIEnv* env, jobject thiz, uint16_t type, uint16_t code, int32_t value)

  {

  struct uinput_event event;

  int fd_kb = open("/dev/input/event21",O_RDWR);

  memset(&event, 0, sizeof(event));

  event.type = type;

  event.code = code;

  event.value = value;

  write(fd_kb, &event, sizeof(event));

  }

  Android:

  public native String inject(int type,int code,int value);

  这儿有一个坑,不同手机分管事件的设备不一样,比如上面的event21再其他手机上可能就不是这个了

  解决方法:遍历dev/input下所有设备往里面写数据(有点小暴力)

  Ok,事件注入到这就算没问题了,为了方便我们可以再在上层写一个Server程序供程序供Robotium Case中调用

  当然这就涉及到IPC通信

  Android IPC通信实现方式大概分为:广播、aidl等,封装完了剩下的就是Case中调用了(这里就不讲了相信大家都会)

  以上内容为大家介绍了自动化测试中的Robotium 跨进程(底层设备事件注入),本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部