The ScreenRecord command is able to record the display of devices running Android 4.4 (API level 19) and higher. The tool is recording the app interaction to an MPEG-4 file, which can be downloaded from the device using the command
1 |
adb pull |
. This video can then be used as part of your bug report as well.
The following sample will start a recording session of the connected device.
1 |
$ adb shell screenrecord /sdcard/NotAbleToLogin.mp4 |
To stop the screen recording press
1 |
Ctrl-C |
, otherwise the recording will stop automatically after three minutes. However, you can also define a time limit with the paramter
1 |
--time-limit |
.
The tool is recording the video in the native display resolution and orientation by default. However, there are some limitations of the screenrecord command that you should know when using it:
To begin recording your device screen, run the
1 |
screenrecord |
command to record the video. Then, run the
1 |
pull |
command to download the video from the device to the host computer. Here’s an example recording session:
1 2 3 4 5 |
$ adb shell shell@ $ screenrecord --verbose /sdcard/demo.mp4 (press Control + C to stop) shell@ $ exit $ adb pull /sdcard/demo.mp4 |
The
1 |
screenrecord |
utility can record at any supported resolution and bit rate you request, while retaining the aspect ratio of the device display. The utility records at the native display resolution and orientation by default, with a maximum length of three minutes.
1 |
screenrecord |
options
Options | Description | ||||
---|---|---|---|---|---|
|
Displays command syntax and options | ||||
|
Sets the video size:
|
||||
|
Sets the video bit rate for the video, in megabits per second. The default value is 4Mbps. You can increase the bit rate to improve video quality, but doing so results in larger movie files. The following example sets the recording bit rate to 6Mbps:
|
||||
|
Sets the maximum recording time, in seconds. The default and maximum value is 180 (3 minutes). | ||||
|
Rotates the output 90 degrees. This feature is experimental. | ||||
|
Displays log information on the command-line screen. If you do not set this option, the utility does not display any information while running. |
Refer the below script to record the Android device, It will be created as video file and move it in to our project directory, and finally it remove the recorded file in android device.
ScreenRecorder.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
package Generic; 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. // "cmd /c adb shell screenrecord --bit-rate 1000000 //sdcard//" Runtime.getRuntime().exec( "cmd /c adb shell screenrecord /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; } } |
Start Recording:
1 |
ScreenRecorder.StartScreenRecording("RecordFileName"); |
The Recorded file will be created by given name.
Stop Recording:
1 |
ScreenRecorder.StopScreenRecording("RecordFileName","/recording_Files",true); |
We have to give three Parameters to stop the Recording, The first Parameter is a file name which we created at that time of start recording, Second parameter is a directory path to where we want move the file.
1 2 3 4 |
// Pulling Screen Recording to PC/Machine Runtime.getRuntime().exec( "cmd /c adb pull /sdcard/" + CurrentTestMethodName + ".mp4 " + DirectoryToSaveRecordedScreen); |
And third Parameter set it as ‘true’ to remove the recorded file in Mobile device.
1 2 3 4 5 6 |
if (RemoveRecordedScreenFromDevice) { // Deleting ScreenRecord from Device Runtime.getRuntime().exec( "cmd /c adb shell rm /sdcard/" + CurrentTestMethodName + ".mp4"); } |
Sample Test case for Screen Record.
ViewPageTC_ScreenRecord.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.appiumtest; import Generic.BaseTest; import Generic.ScreenRecorder; import Generic.deviceCommon; import com.Pages.ViewPage1; import io.qameta.allure.*; import listener.TestListener; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.gradle.internal.impldep.aQute.lib.getopt.Description; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Listeners; import org.testng.annotations.Test; import java.io.IOException; @Listeners({TestListener.class}) @Epic("Regression Tests") @Feature("News") public class ViewPageTC_ScreenRecord extends BaseTest { public static Logger logger = LogManager.getLogger(ViewPageTC_ScreenRecord.class.getName()); int random = (int)(Math.random() * 50 + 1); String RecordName = "RecFile"+random; @BeforeTest public void BeforeTestMethod() throws IOException { //To Start Screen Recorder ScreenRecorder.StartScreenRecording(RecordName); } @Test @Severity(SeverityLevel.BLOCKER) @Description("Test Description: Click Login Button and Assert Page Title") @Story("View page") public void ViewPageTC() throws Exception { logger.info("Logging with Log 4J starts"); ViewPage1 vpage =new ViewPage1(driver); vpage.TestURName(); } @AfterTest public void AfterTestMethod() throws IOException, InterruptedException { //To Stop Screen Recorder ScreenRecorder.StopScreenRecording(RecordName,"recording_Files",true); } } |
Very often we would like to record our interaction with the screen to demo our feature. We can do this easily with ADB (Android Debug Bridge).
Note: To do that, the pre-requisite is to first connect our device to our computer, and allow it to be debuggable. Also we need to have Android Studio installed.
Reference:
http://adbshell.com/commands/adb-shell-screenrecord