Enabling video/screen recording for your Appium tests
One of the hidden features that Appium has is screen recording. It is inbuilt and unlike configuring things externally like how we do with Selenium, the APIs are available out of the box.
START RECORDING SCREEN
In order to start the recording, we just need to call the startRecordingScreen() method from the respective classes.
For IOS, please install ffmpeg (brew install ffmpeg)
((CanRecordScreen)driver).startRecordingScreen();
We can add screen recording configurations like time limit, video size etc during the start of your video recording.
// For Android
((CanRecordScreen)driver).startRecordingScreen(new AndroidStartScreenRecordingOptions()....);// For IOS
((CanRecordScreen)driver).startRecordingScreen(new IOSStartScreenRecordingOptions()....);
STOP RECORDING SCREEN
In order to stop the recording, we need to call the stopRecordingScreen() method from the respective classes.
((CanRecordScreen)driver).stopRecordingScreen();
We can add screen recording configurations like upload options when we stop the video recording as well.
// For Android
((CanRecordScreen)driver).stopRecordingScreen(new AndroidStopScreenRecordingOptions().withUploadOptions(ScreenRecordingUploadOptions.uploadOptions()....));// For IOS
((CanRecordScreen)driver).stopRecordingScreen(new IOSStopScreenRecordingOptions().withUploadOptions(ScreenRecordingUploadOptions.uploadOptions()....));
Now, coming to the most important question! where is our video?
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 NIO package.
@After
public void tearDown()
{
String base64String = ((CanRecordScreen)driver).stopRecordingScreen();
byte[] data = Base64.decodeBase64(base64String);
String
destinationPath="target/filename.mp4";
Path path = Paths.get(destinationPath);
Files.write(path, data);
}
If you are using Allure reports, then
attachVideo(destinationPath);@Attachment(value = "video",type="video/mp4")
public byte[] attachVideo(String path) throws Exception {
return getFile(path);
}
public byte[] getFile(String fileName) throws Exception {
File file = new File(fileName);
return Files.readAllBytes(Paths.get(file.getAbsolutePath()));
}