Recording live automation using appium

Some important points

  1. We can record a session of Appium
  2. Recorded files can be saved to local drive
  3. Sometimes developers may restrict recording the screen so You will not be able to record certain apps
  4. startRecordingScreen method of AppiumDriver is used to record the screen
  5. Video size of 1280x720 needs to be passed for certain emulators otherwise recording will fail

Below code demonstrates how you can record the screen of Android emulator using appium.

package org.softpost.android.simulator;

import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.AndroidStartScreenRecordingOptions;
import io.appium.java_client.screenrecording.BaseStartScreenRecordingOptions;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.net.URL;
import java.time.Duration;
import java.util.Base64;
import java.util.concurrent.TimeUnit;

/**
 * Created by Sagar on 10-07-2016.
 */
public class RecordSessionTest {
    AndroidDriver driver;

    @Test
    public void test1() throws Exception{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "android");

        caps.setCapability("deviceName", "Pixel 9");
        caps.setCapability("appPackage", "com.android.settings");
        caps.setCapability("appActivity","com.android.settings.Settings");
        caps.setCapability("automationName","uiautomator2");

    try {
        driver = new AndroidDriver<AndroidElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.startRecordingScreen(
                new AndroidStartScreenRecordingOptions()
                        .withVideoSize("1280x720")
                        .withTimeLimit(Duration.ofSeconds(200)));
    WebElement battery = driver.findElement(By.xpath("//android.widget.TextView[contains(@text,'Battery')]"));
        WebElement sound = driver.findElement(By.xpath("//android.widget.TextView[contains(@text,'Sound')]"));
        battery.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("androidclip.mp4"), decode);
        driver.quit();
    }
    }
}

Recording iPhone Simulators in Appium

Below example shows how to do recording of iPhone Simulators.

package org.softpost.ios.simulator;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.AndroidStartScreenRecordingOptions;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSStartScreenRecordingOptions;
import io.appium.java_client.ios.IOSStopScreenRecordingOptions;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.io.File;
import java.net.URL;
import java.time.Duration;
import java.util.Base64;
import java.util.concurrent.TimeUnit;

/**
 * Created by Sagar on 10-07-2016.
 */
public class RecordSessionTest {
    IOSDriver driver;

    @Test
    public void test1() throws Exception{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "iOS");
        caps.setCapability("platformVersion", "11.0");
        caps.setCapability("deviceName", "iPhone 8");
        caps.setCapability("AutomationName" , "XCUITest");
        caps.setCapability("bundleId", "com.apple.MobileAddressBook");

    try {
        driver = new IOSDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        driver.startRecordingScreen(new IOSStartScreenRecordingOptions()
                .withVideoScale("1280x720")
                .withTimeLimit(Duration.ofSeconds(100)));

        WebElement e = driver.findElement(By.xpath("//XCUIElementTypeButton[@name="Add"]"));
        e.click();

    }catch (Exception ex) {
        ex.printStackTrace();
        System.out.println(driver.getPageSource());

    }finally {
        String video =driver.stopRecordingScreen();
        byte[] decode = Base64.getDecoder().decode(video);
        String fileName = "ios.mp4";
        FileUtils.writeByteArrayToFile(new File(fileName), decode);
        driver.quit();
    }
    }
}



Web development and Automation testing

solutions delivered!!