安卓流畅度测试--利用chimpchat模拟Monkeyrunner快速滑动页面

背景

项目组所用的流畅度测试脚本是python写的,利用monkeyrunner做快速页面滑动,与公司内部的安卓UI测试框架(Java编写)不兼容,而且内部UI框架的页面滑动较慢,为了与内部测试平台打通,接手了这样一个任务。

jar包准备

下列所展示的jar包都是来自于安卓sdk/tools/bin目录,具体的版本号与自己所用的安卓sdk版本有关系,博主获取的包大都是26的

chimpchat-26.0.0-dev.jar
sdklib-26.0.0-dev.jar
guava-22.0.jar
ddmlib-26.0.0-dev.jar
common-26.0.0-dev.jar

Java实现快速上下滑动

#导入包
import com.android.chimpchat.adb.AdbBackend;
import com.android.chimpchat.adb.AdbChimpDevice;

@Override
	protected boolean test() throws Exception {
		logger.info("步骤1、启动app,进入首页");
		logger.info("步骤2、获取手机分辨率");
		int width = resolution.x;
		int height = resolution.y;
		logger.info("步骤3、获取当前设备id,可以使用adb devices获取");
		AdbBackend adb = new AdbBackend();
		AdbChimpDevice device = (AdbChimpDevice) adb.waitForConnection(5000, "deviceid");
		
		if (device == null) {
			throw new RuntimeException("设备连接失败");
		}
		device.wake();
		logger.info("步骤4、先执行上滑页面,加载到合适位置");
		device.drag(width / 2, height * 4 / 5, width / 2, height * 1 / 5, 9, 100);
		logger.info("步骤5、循环500次,上滑4次,下滑4次");
		for (int i = 0 ; i < 20; i++) {
			logger.info("子步骤1、上滑4次");
			for (int j = 0; j < 4; j++) {
				device.drag(width / 2, height * 4 / 5, width / 2, height * 3 / 10, 9, 100);
			}
			logger.info("子步骤2、下滑4次");
			for (int j = 0; j < 4; j++) {
				device.drag(width / 2, height * 3 / 10, width / 2, height * 4 / 5, 9, 100);
			}
		}
		adb.shutdown();
		return true;
	}

主要过程就是用内部框架启动app,然后获取手机的分辨率,然后使用chimpbackend 获取设备对象,然后通过调用device.drage参数,输入的参数分别是起始x,y坐标,终点x,y坐标以及步数和耗时,下面贴出drag方法的具体实现。在实现代码过程最后有一行代码

adb.shutdown();

这一句必不可少,不然整个测试脚本执行完后无法结束还是会占用进程(这个可以通过jdk下的jvisualvm查看)
安卓流畅度测试--利用chimpchat模拟Monkeyrunner快速滑动页面_第1张图片
导致资源无法回收,再次执行会报错,所以一定要加上(博主的辛酸泪)

final long iterationTime = ms / steps;

可以看到steps和ms控制周期时间,所以我们可以通过修改这两个参数实现滑动速度的调整,最后可以选择一对合适的参数来做测试

源码展示


  public IChimpDevice waitForConnection(long timeoutMs, String deviceIdRegex)
  {
    do {
      IDevice device = findAttachedDevice(deviceIdRegex);

      if ((device != null) && (device.getState() == IDevice.DeviceState.ONLINE)) {
        IChimpDevice chimpDevice = new AdbChimpDevice(device);
        this.devices.add(chimpDevice);
        return chimpDevice;
      }
      try
      {
        Thread.sleep(200L);
      } catch (InterruptedException e) {
        LOG.log(Level.SEVERE, "Error sleeping", e);
      }
      timeoutMs -= 200L;
    }while (timeoutMs > 0L);

    return null;
  }

public void drag(int startx, int starty, int endx, int endy, int steps, long ms)
  {
    final long iterationTime = ms / steps;

    LinearInterpolator lerp = new LinearInterpolator(steps);
    LinearInterpolator.Point start = new LinearInterpolator.Point(startx, starty);
    LinearInterpolator.Point end = new LinearInterpolator.Point(endx, endy);
    lerp.interpolate(start, end, new LinearInterpolator.Callback()
    {
      public void step(LinearInterpolator.Point point) {
        try {
          AdbChimpDevice.this.manager.touchMove(point.getX(), point.getY());
        } catch (IOException e) {
          AdbChimpDevice.LOG.log(Level.SEVERE, "Error sending drag start event", e);
        }
        try
        {
          Thread.sleep(iterationTime);
        } catch (InterruptedException e) {
          AdbChimpDevice.LOG.log(Level.SEVERE, "Error sleeping", e);
        }
      }

      public void start(LinearInterpolator.Point point)
      {
        try {
          AdbChimpDevice.this.manager.touchDown(point.getX(), point.getY());
          AdbChimpDevice.this.manager.touchMove(point.getX(), point.getY());
        } catch (IOException e) {
          AdbChimpDevice.LOG.log(Level.SEVERE, "Error sending drag start event", e);
        }
        try
        {
          Thread.sleep(iterationTime);
        } catch (InterruptedException e) {
          AdbChimpDevice.LOG.log(Level.SEVERE, "Error sleeping", e);
        }
      }

      public void end(LinearInterpolator.Point point)
      {
        try {
          AdbChimpDevice.this.manager.touchMove(point.getX(), point.getY());
          AdbChimpDevice.this.manager.touchUp(point.getX(), point.getY());
        } catch (IOException e) {
          AdbChimpDevice.LOG.log(Level.SEVERE, "Error sending drag end event", e);
        }
      }
    });
  }

你可能感兴趣的:(Java,系统功能测试)