Search code examples
androidshellautomationadbappium

Stop an Android screen recording in an automated script


I'm building automated test scenarios in Appium (for an Android app), and am trying to figure out a way to start recording the screen during a specific moment, running some actions and then stopping the recording.

I could not find any screen record solution implemented in Appium, but found a neat adb shell command, screenrecord, that does exactly what it states. Now my problem is that the only obvious ways to stop the recording are to either set the exact time limit I need in the arguments, or to press Ctrl-C in the interactive shell:

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.

I could use --time-limit, but then I need to estimate how long any given recording should take within the tests, the memory usage will be far from optimal, etc.

Using Ctrl-C offers its own limitations, as it requires an interactive shell session, and my automations should include only simple shell commands:

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

Does anyone know of a way to actively stop a recording?

Alternatively, does anyone know of how to record the screen using Appium?


Solution

  • If you save the process whichever you ran the screenrecord, you can do kill with signal SIGINT that will work as you want.

    For example:

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

    and then when you want to kill it

    process.stdin = pkill -2 screenrecord
    

    Should work, give it a try