Recording Screen in Appium Test Execution
  One of the best feature in Appium is Screen Recording for Appium Tests,
      we can record our Appium script execution and store it in local path.
  Screen Recording in Appium is inbuilt unlike configuring things externally like how we do with Selenium.
  
  We can record a complete session of Appium and recorded files can be
      saved to local drive.
  
  
  
Start Recording Screen:
  To record the screen, we need to call the startRecordingScreen() method from the respective class.
driver.startRecordingScreen();
  
  
  
  this above can be called without passing recording options like width,
      height and duration.
  
  
driver.startRecordingScreen(new BaseStartScreenRecordingOptions(....));
  
  
  
  
  
 
  this above method, we need to use when we have specific requirement like
      width and height and duration of the recording.
  Where
  Video Size : The video size  of the generated media
      file. The format is WIDTHxHieght. The Default value is the device’s native
      display resolution(if supported). If not 1280x720.
  Duration: The maximum recording time. The default and maximum
      value is 180 seconds(3 minutes).
   Setting values greater than this or less than zero(0) will cause an
      exception. The minimum time resolution unit is one second.
  Since Appium 1.8.2 version, the time limit can be up to 1800 seconds (30
      minutes). Appium automatically try to merge the 3-minutes chunks recorded,
      however, this requires FFMEG utility to be installed and available in PATH
      on server machine. If the utility is not present then the most recent
      screen recording chunk is going to be returned as result
  
  
                            
       
  Example:
driver.startRecordingScreen(
                  new AndroidStartScreenRecordingOptions()
                      .withVideoSize("1280x720") .withTimeLimit(Duration.ofSeconds(200)));
  In above example, we are setting width as 1280 and height as 720 and
      duration of the recording is 200 seconds.
  Stopping Screen record:
  In order to stop the recording, we need to call the stopRecordingScreen()
      method from the respective classes.
Driver.stopRecordingScreen();
  
 
  The stopRecordingScreen() method returns a Base64 String. Using
      this string we need to build our video. There are many ways to do it, I
      have used methods from Java FileUtils and Base64 Decoder.
String video =driver.stopRecordingScreen();
           byte[] decode = Base64.getDecoder().decode(video);
           FileUtils.writeByteArrayToFile(new File("appiumRecorded.mp4"), decode);
  Now Let’s see the example by writing full program and see the result:
  Program:
package tests;
 
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Base64;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidStartScreenRecordingOptions;
import java.time.Duration;
 
public class ScreenRecordExample {
 
         public static AndroidDriver driver;
 
         public static void main(String args[]) throws InterruptedException, IOException {
 
                  DesiredCapabilities cap = new DesiredCapabilities();
                  cap.setCapability("deviceName", "emulator-5554"); // your device name from adb devices
                  cap.setCapability("udid", "emulator-5554");// your udid from adb devices
                  cap.setCapability("appActivity", "com.android.calculator2.Calculator");
                  cap.setCapability("appPackage", "com.google.android.calculator");
                  cap.setCapability("platformName", "Android");
                  cap.setCapability("platformVersion", "R"); // your device platform version
                  driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), cap);
                  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                  Thread.sleep(5000);
 
                  try {
 
                           // driver.startRecordingScreen();
                           driver.startRecordingScreen(new AndroidStartScreenRecordingOptions().withVideoSize("1280x720")
                                             .withTimeLimit(Duration.ofSeconds(200)));
                           driver.findElement(By.id("com.google.android.calculator:id/digit_8")).click();
                           driver.findElement(By.id("com.google.android.calculator:id/op_add")).click();
                           driver.findElement(By.id("com.google.android.calculator:id/digit_2")).click();
                           driver.findElement(By.id("com.google.android.calculator:id/eq")).click();
                           Thread.sleep(5000);
 
                  } catch (Exception ex) {
                           ex.printStackTrace();
                           System.out.println(driver.getPageSource());
 
                  }
 
                  finally {
 
                           String video = driver.stopRecordingScreen();
                           byte[] decode = Base64.getDecoder().decode(video);
                           FileUtils.writeByteArrayToFile(new File("appiumRecorded.mp4"), decode);
                           driver.quit();
                  }
 
         }
 
}
  Output: this recorded file will be stored in your project main
      folder.
  
  
  
  
If any issues encountered let me know in the comment section:
  
  
  
Thank you so much for this code :) :)
ReplyDelete