以自动脚本停止Android屏幕录制

时间:2016-01-18 10:05:19

标签: android shell automation adb appium

我正在Appium(针对Android应用)构建自动化测试场景,并且我试图想出一种方法,在特定时刻开始录制屏幕,运行一些操作然后停止录制。

我找不到在Appium中实现的任何屏幕记录解决方案,但找到了一个整洁的adb shell命令screenrecord,它完全符合它所声明的内容。现在我的问题是,停止录制的唯一明显方法是在参数中设置我需要的确切时间限制,或者在交互式shell中按Ctrl-C:

2|shell@mako:/ $ screenrecord --help
Usage: screenrecord [options] <filename>

Android screenrecord v1.2.  Records the device's display to a .mp4 file.

Options:
--size WIDTHxHEIGHT
    Set the video size, e.g. "1280x720".  Default is the device's main
    display resolution (if supported), 1280x720 if not.  For best results,
    use a size supported by the AVC encoder.
--bit-rate RATE
    Set the video bit rate, in bits per second.  Value may be specified as
    bits or megabits, e.g. '4000000' is equivalent to '4M'.  Default 4Mbps.
--bugreport
    Add additional information, such as a timestamp overlay, that is helpful
    in videos captured to illustrate bugs.
--time-limit TIME
    Set the maximum recording time, in seconds.  Default / maximum is 180.
--verbose
    Display interesting information on stdout.
--help
    Show this message.

Recording continues until Ctrl-C is hit or the time limit is reached.

我可以使用--time-limit,但是我需要估计在测试中任何给定录制应该花多长时间,内存使用量将远非最佳等等。

使用Ctrl-C提供了自己的限制,因为它需要一个交互式shell会话,我的自动化应该只包含简单的shell命令:

adb -s DEVICE_UDID shell screenrecord /sdcard/Recordings/video.mp4

有没有人知道主动停止录制的方法?

或者,是否有人知道如何使用Appium录制屏幕?

2 个答案:

答案 0 :(得分:3)

如果您保存了运行屏幕记录的过程,您可以使用符合您需要的信号SIGINT进行终止。

例如:

process = adb shell
process.stdin = screenrecord test.mp4 &

然后当你要杀了它

process.stdin = pkill -2 screenrecord

应该工作,试一试

答案 1 :(得分:-1)

一旦开始进行屏幕录制,就会启动adb流程。因此,如果你可以杀死启动的adb进程,那么屏幕录制停止。为此,您可以在开始屏幕录制之前先存储所有adb进程,然后在屏幕录制之后启动任何adb进程,只需将其删除即可。我在下面的课程中实现了屏幕录制。代码是自我解释的。请检查。你也可以查看 - http://easytestautomation.blogspot.in/2016/06/easy-to-record-screen-in-appium-for.html

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class ScreenRecorder {


 private static List<String> ADBProcessIDsBeforeScreenRecording = null;
 private static List<String> ADBProcessIDsAfterScreenRecording = null;


 public static void StartScreenRecording(String CurrentTestMethodName)
   throws IOException {


  // Storing all ADB process ids before starting Screen Recording.
  ADBProcessIDsBeforeScreenRecording = getProcessIDs("adb.exe");


  // "Starting Screen Recording for Current TestMethod.
  Runtime.getRuntime().exec(
    "cmd /c adb shell screenrecord --bit-rate 1000000 //sdcard//"
      + CurrentTestMethodName + ".mp4");


 }


 public static void StopScreenRecording(String CurrentTestMethodName,
   String DirectoryToSaveRecordedScreen,
   boolean RemoveRecordedScreenFromDevice) throws IOException,
   InterruptedException {


  // Storing all ADB process ids after Screen Recording.
  ADBProcessIDsAfterScreenRecording = getProcessIDs("adb.exe");


  // killing ADB task using process id.
  // First we are trying to get ADB process id that is started due to ADB
  // screenrecord then killing the same.
  for (String id : ADBProcessIDsAfterScreenRecording) {
   boolean found = false;
   for (String tgtid : ADBProcessIDsBeforeScreenRecording) {
    if (tgtid.equals(id)) {
     found = true;
     break;
    }
   }
   if (!found) {
    Runtime.getRuntime().exec("taskkill /F /PID " + id);
    break;
   }
  }


  // Sleep time to save the recorded video in Device properly
  Thread.sleep(2000);


  // Pulling Screen Recording to PC/Machine
  Runtime.getRuntime().exec(
    "cmd /c adb pull //sdcard//" + CurrentTestMethodName + ".mp4 "
      + DirectoryToSaveRecordedScreen);
  // Sleep time to pull video from device to destination directory
  Thread.sleep(5000);


  if (RemoveRecordedScreenFromDevice) {
   // Deleting ScreenRecord from Device
   Runtime.getRuntime().exec(
     "cmd /c adb shell rm //sdcard//" + CurrentTestMethodName
       + ".mp4");
  }


 }


 //Method to get List of Process Ids using Process Name
 static List<String> getProcessIDs(String processName) {
  List<String> processIDs = new ArrayList<String>();
  try {
   String line;
   Process p = Runtime.getRuntime().exec("tasklist /v /fo csv");
   BufferedReader input = new BufferedReader(new InputStreamReader(
     p.getInputStream()));
   while ((line = input.readLine()) != null) {
    if (!line.trim().equals("")) {
     // Pid is after the 1st ", thus it's argument 3 after
     // splitting
     String currentProcessName = line.split("\"")[1];
     // Pid is after the 3rd ", thus it's argument 3 after
     // splitting
     String currentPID = line.split("\"")[3];
     if (currentProcessName.equalsIgnoreCase(processName)) {
      processIDs.add(currentPID);
     }
    }
   }
   input.close();
  } catch (Exception err) {
   err.printStackTrace();
  }
  return processIDs;
 }


}