Tuesday 28 June 2022

How to perform video/screen recording using Appium tests? Write util class to perform screen recording for automation failed test cases using Appium?





Table of Contents:

  • Introduction to Appium
  • Installation of Appium
  • Start Appium Server
  • Add VideoRecordUtil class to your package
  • Call the Start and STop recording methods

Introduction to Appium

Appium is an open-source framework for automating native, mobile web, and hybrid applications on Android mobile, iOS mobile, and Windows desktop platforms. For starters, Appium was developed by Dan Cuellar and Jason Huggins and today has about 15.2K stars and 5.7K Fork on GitHub. It has the highest market share compared to all the other mobile automation frameworks.


Installation of Appium via Desktop App

Simply download the latest version of Appium Desktop from the releases page of Appium official website.

Appium Desktop



Start Appium Server

  1. we can start by clicking on the ‘Start Server’ button inside of Appium Desktop as shown below.
  2. ‘Start Server’



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.

Step-1: Add the Class VideoRecordUtils in Util package


Package com.appium.utils;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;


import org.apache.commons.codec.binary.Base64;
import org.testng.ITestResult;


import static com.appium.constants.FrameworkConstants.YES;
import com.appium.manager.DateTimeManager;
import com.appium.manager.DriverManager;


import io.appium.java_client.screenrecording.CanRecordScreen;


public class VideoRecordUtils {

//To record the screen, we need to call the startRecordingScreen() method from the respective class.

public static void startRecording() {


if (ConfigLoader.getInstance().getFailedTestsVideo().equalsIgnoreCase(YES)) {

((CanRecordScreen) DriverManager.getDriver()).startRecordingScreen();
}
}

// In order to stop the recording, we need to call the stopRecordingScreen() method from the respective classes.

public static void stopRecording(ITestResult result) {

if (ConfigLoader.getInstance().getFailedTestsVideo().equalsIgnoreCase(YES)) {
/* Do only when Test is failed */

if (result.getStatus() == 2) {

String media = ((CanRecordScreen) DriverManager.getDriver()).stopRecordingScreen();


Map<String, String> params = result.getTestContext().getCurrentXmlTest().getAllParameters();

String dir = "Videos" + File.separator + File.separator + params.get("platformName") + "_"
+ params.get("deviceName") + "_" + params.get("udid") + File.separator
+ DateTimeManager.getDateTime() + File.separator
+ result.getTestClass().getRealClass().getSimpleName();


createFolderCopyFile(result, media, dir);
}
}
}


private static void createFolderCopyFile(ITestResult result, String media, String dir) {
File videoDir = new File(dir);
if (!videoDir.exists()) {
videoDir.mkdirs();
}
FileOutputStream stream = null;

try {
stream = new FileOutputStream(videoDir + File.separator + result.getName() + ".mp4");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}

try {
stream.write(Base64.decodeBase64(media));
} catch (IOException e) {
e.printStackTrace();
}
}
}




******************

What are the Locators used for Mobile Automation Testing?


Ans: Click Here For Mobile Testing Locators


**************************




STEP-2: Call the StartRecording and StopRecording Methods


Then we need to update BeforeMethod and AfterMethod to start and stop the recording, in the BaseTest, this class is supposed to be extends in your test classes, here we will call the method created in step-1

      @BeforeMethod
public void beforeMethod() {
VideoRecordUtils.startRecording();
}

@AfterMethod
public void afterMethod(ITestResult result) {
VideoRecordUtils.stopRecording(result);
}




REVISION:==>

==>. How we can verify the Appium installation, what is Appium doctor?


Ans:   To verify that all of Appium's dependencies are met we can use appium-doctor.
Install it with commandnpm install -g appium-doctor, then run the appium-doctor command, supplying the --ios or --android flags to verify that all of the dependencies are set up correctly.


==>. What is the default port for Appium?

Ans: 4723 

This port information is important since we will have to direct your test client to make sure to connect to Appium on this port. If we want to change, the port, we can do so by using the -p flag when starting Appium.

==> What are the important Appium Desired Capabilities?

Ans

{

"platformName": "iOS", "platformVersion": "11.0", "deviceName": "iPhone 11", "automationName": "XCUITest", "app": "/path to app" }

==> Mention the Automation engine for android and IOS?

Ans:
Android: UiAutomator2
IOS : XCUITest

Learn (API-Microservice)Testing+(CoreJava+UI)-SDET with Self Paced Videos and one LIVE Doubt Session

TRANING VIDEOS AVAILABLE with Live Doubt Session @4500/-(course-1 below,API TRaining Videos With ClassNotes and Coding Set) and 6500/- (API+UI, both course-1 & 2 below) Check Training Page for Course Content or reach out @whatsapp +91-9619094122

Entire course content can be found below:  COURSE CONTENT
Check below link for question and answers with code:

Top API Interview Question 1-10

*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions
Mobile Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Mobile%20Testing

Python Interview Question Set for QAE - SDET - SDE:
https://automationreinvented.blogspot.com/search/label/Python

No comments:

Post a Comment

All Time Popular Posts

Most Featured Post

Introduction to Web Testing with Playwright

  Introduction to Web Testing with Playwright 📌  Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 📌  YouTube channel:  https://lnkd.in/gGUG...