自动化测试:如何自动化的刷快手极速版的视频

发表于:2020-2-25 11:41

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:软件测试艺术    来源:软件测试艺术

  近两年短视频出现了井喷式的发展,我们熟知常见的有抖音、快手、微视、火山小视频等,看短视频已经成了年轻人的习惯,每天闲暇的时间就会拿出手机刷刷刷,打发无聊的时间。
  其实,抖音和快手都推出了极速版,在娱乐的同时还可以刷视频赚钱了,不过纯属娱乐,一天也没多少钱。
  但是我们作为一个测试人员,有没有办法让视频自动刷起来呢。方法是有的,今天我们就来开发一个能自动刷短视频的软件。如果我们学会了,可以用到其他的一些能赚钱的应用上。
  首先我们能想到的是开发自动化脚本,使用android adb命令模拟滑动,命令为adb shell input swipe <x1> <y1> <x2> <y2>,然后设置一个循环在等待固定的时间就滑动一次就行了,使用windows的批处理或者使用python开发一个脚本就行了。但是缺点非常命令,需要一直开着电脑,不能随时随地的刷,没有意义。
  另一种方法可能想到是开发一个android的app应用,然后在应用中使用Android的Runtime方法调用adb滑动命令,如Runtime.getRuntime().exec(“input swipe <x1> <y1> <x2> <y2>”),实际等你试了也不会成功,因为在应用中想要调用adb命令需要root权限,我们自己使用的普通手机都使用不起来的。
  还有第三种方法就是android的辅助服务AccessibilityService。辅助服务的设计初衷提供给无法和界面进行交互的残疾用户来协助帮助他们进行一些用户操作,比如点击,返回,长按,获取屏幕信息等能力。后来被开发者另辟蹊径,用于一些插件开发,做一些监听第三方应用的插件。
  我们不会将辅助服务AccessibilityService如何使用,也不会将怎么打开应用的时候引导用户开辅助服务,这些网上都可以搜到,只需要移植到过来即可。下面我们来讲在辅助服务中如果实现滑动、双击点赞、跳过广告等设计。
  首先是关于滑动实现,主要使用的是AccessibilityService在android7.0上新增的dispatchGesture 和GestureDescription类。关于两个类介绍如下,网上也有。
  首先是dispatchGesture方法的解释:
 boolean dispatchGesture (GestureDescription gesture, AccessibilityService.GestureResultCallback callback, Handler handler)
  这个方法有三个参数:
  参数GestureDescription:翻译过来就是手势的描述,如果要实现模拟,首先要描述你的腰模拟的手势;
  参数GestureResultCallback:翻译过来就是手势的回调,手势模拟执行以后回调结果;
  参数handler:大部分情况我们不用的话传空就可以了。
  一般我们关注GestureDescription这个参数就够了,下边重点介绍一下这个参数:
  构建一个手势描述的关键代码:
   GestureDescription.StrokeDescription(Path path, long startTime, long duration);
  例如:
  GestureDescription.Builder builder = GestureDescription.Builder();
  GestureDescription gestureDescription = builder
  .addStroke(GestureDescription.StrokeDescription(path, 100, 400)).build();
  参数介绍如下:
  参数path:笔画路径,也就是滑动的路径,可以通过path.moveTo和path.lineTo实现;
  参数startTime:时间 (以毫秒为单位),从手势开始到开始笔划的时间,非负数;
  参数duration:笔划经过路径的持续时间(以毫秒为单位),非负数;
  介绍了上面的基础知识,我们就来看下滑动的代码需要如何开发,见下面:
   private void mockSwipe(){
  final Path path = new Path();
  path.moveTo(X, Y); //滑动的起始位置,例如屏幕的中心点X、Y
  path.lineTo(X, 0); //需要滑动的位置,如从中心点滑到屏幕的顶部
  GestureDescription.Builder builder = new GestureDescription.Builder();
  GestureDescription gestureDescription = builder.addStroke(
  new GestureDescription.StrokeDescription(path, 100, 400)
  ).build(); //移动到中心点,100ms后开始滑动,滑动的时间持续400ms,可以调整
  dispatchGesture(gestureDescription, new GestureResultCallback() {
  @Override
  //如果滑动成功,会回调如下函数,可以在下面记录是否滑动成功,滑动成功或失败都要关闭该路径笔画
  public void onCompleted(GestureDescription gestureDescription) {
  super.onCompleted(gestureDescription);
  Log.d(TAG, "swipe  success.");
  path.close();
  }
  @Override
  public void onCancelled(GestureDescription gestureDescription) {
  super.onCancelled(gestureDescription);
  Log.d(TAG, " swipe  fail.");
  path.close();
  }
  }, null); //handler为空即可
  }
  那滑动实现了,双击点赞是如何实现的呢,也许你想到了,是的就是通过构造path实现,只需要将起始点和终点设置一样就可以了,但是不是设置lineTo和moveTo的坐标一样就行,双击的实现方式如下:
   private void mockDoubleClick(){
  final Path path = new Path();
  path.moveTo((int)(X/2), (int)(Y/2));  //X和Y是需要双击的按钮坐标
  GestureDescription.Builder builder = new GestureDescription.Builder();
  GestureDescription gestureDescription = builder.addStroke(
  new GestureDescription.StrokeDescription(path, 0, 100)).build();
  dispatchGesture(gestureDescription, new GestureResultCallback() {
  @Override
  public void onCompleted(GestureDescription gestureDescription) {
  super.onCompleted(gestureDescription);
  Path path2 = new Path();
  path2.moveTo((int)(X/2), (int)(Y/2));
  //以下代码是完成第二个手势操作
  GestureDescription.Builder builder2 = new GestureDescription.Builder();
  GestureDescription gestureDescription2 = builder2.addStroke(
  new GestureDescription.StrokeDescription(path2, 0, 100)).build();
  AccessibilityServiceTest.this.dispatchGesture(gestureDescription2, null, null);
  Log.d(TAG, "double click finish.");
  path.close();
  path2.close();
  }
  @Override
  public void onCancelled(GestureDescription gestureDescription) {
  super.onCancelled(gestureDescription);
  Log.d(TAG, "scroll cancell.");
  }
  }, null);
  }
  至于查找控件,使用辅助服务AccessibilityService的AccessibilityNodeInfo和findAccessibilityNodeInfosByText 即可,具体的操作如下:
   rootInfo =AccessibilityServiceTest.this.getRootInActiveWindow();
  List<AccessibilityNodeInfo> listInfo = rootInfo.findAccessibilityNodeInfosByText("查找文本");
  备注:关于app的其他开发方面没有提及,如如果添加辅助服务以及说明,请看原文中的AndroidManifest.xml和simulatekey.xml(辅助服务的设置)文件。

      上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号