Best JavaScript code snippet using appium-android-driver
recordscreen.js
Source:recordscreen.js
...106 ? recordingProperties.currentTimeLimit107 : MAX_RECORDING_TIME_SEC;108 log.debug(`Starting the next ${chunkDuration}s-chunk ` +109 `of screen recording in order to achieve ${timeLimitInt}s total duration`);110 scheduleScreenRecord(adb, recordingProperties)111 .catch((e) => {112 log.error(e.stack);113 recordingProperties.stopped = true;114 });115 });116 await recordingProc.start(0);117 try {118 await waitForCondition(async () => await adb.fileExists(pathOnDevice),119 {waitMs: RETRY_TIMEOUT, intervalMs: RETRY_PAUSE});120 } catch (e) {121 throw new Error(`The expected screen record file '${pathOnDevice}' does not exist after ${RETRY_TIMEOUT}ms. ` +122 `Is ${SCREENRECORD_BINARY} utility available and operational on the device under test?`);123 }124 recordingProperties.records.push(pathOnDevice);125 recordingProperties.recordingProcess = recordingProc;126}127async function mergeScreenRecords (mediaFiles) {128 try {129 await fs.which(FFMPEG_BINARY);130 } catch (e) {131 throw new Error(`${FFMPEG_BINARY} utility is not available in PATH. Please install it from https://www.ffmpeg.org/`);132 }133 const configContent = mediaFiles134 .map((x) => `file '${x}'`)135 .join('\n');136 const configFile = path.resolve(path.dirname(mediaFiles[0]), 'config.txt');137 await fs.writeFile(configFile, configContent, 'utf8');138 log.debug(`Generated ffmpeg merging config '${configFile}' with items:\n${configContent}`);139 const result = path.resolve(path.dirname(mediaFiles[0]), `merge_${Math.floor(new Date())}${DEFAULT_EXT}`);140 const args = ['-safe', '0', '-f', 'concat', '-i', configFile, '-c', 'copy', result];141 log.info(`Initiating screen records merging using the command '${FFMPEG_BINARY} ${args.join(' ')}'`);142 await exec(FFMPEG_BINARY, args);143 return result;144}145async function terminateBackgroundScreenRecording (adb, force = true) {146 const pids = (await adb.getPIDsByName(SCREENRECORD_BINARY))147 .map((p) => `${p}`);148 if (_.isEmpty(pids)) {149 return false;150 }151 try {152 await adb.shell(['kill', force ? '-15' : '-2', ...pids]);153 await waitForCondition(async () => _.isEmpty(await adb.getPIDsByName(SCREENRECORD_BINARY)), {154 waitMs: PROCESS_SHUTDOWN_TIMEOUT,155 intervalMs: 500,156 });157 return true;158 } catch (err) {159 throw new Error(`Unable to stop the background screen recording: ${err.message}`);160 }161}162/**163 * @typedef {Object} StartRecordingOptions164 *165 * @property {?string} remotePath - The path to the remote location, where the captured video should be uploaded.166 * The following protocols are supported: http/https, ftp.167 * Null or empty string value (the default setting) means the content of resulting168 * file should be encoded as Base64 and passed as the endpount response value.169 * An exception will be thrown if the generated media file is too big to170 * fit into the available process memory.171 * This option only has an effect if there is screen recording process in progreess172 * and `forceRestart` parameter is not set to `true`.173 * @property {?string} user - The name of the user for the remote authentication. Only works if `remotePath` is provided.174 * @property {?string} pass - The password for the remote authentication. Only works if `remotePath` is provided.175 * @property {?string} method - The http multipart upload method name. The 'PUT' one is used by default.176 * Only works if `remotePath` is provided.177 * @property {?string} videoSize - The format is widthxheight.178 * The default value is the device's native display resolution (if supported),179 * 1280x720 if not. For best results,180 * use a size supported by your device's Advanced Video Coding (AVC) encoder.181 * For example, "1280x720"182 * @property {?boolean} bugReport - Set it to `true` in order to display additional information on the video overlay,183 * such as a timestamp, that is helpful in videos captured to illustrate bugs.184 * This option is only supported since API level 27 (Android P).185 * @property {?string|number} timeLimit - The maximum recording time, in seconds. The default value is 180 (3 minutes).186 * The maximum value is 1800 (30 minutes). If the passed value is greater than 180 then187 * the algorithm will try to schedule multiple screen recording chunks and merge the188 * resulting videos into a single media file using `ffmpeg` utility.189 * If the utility is not available in PATH then the most recent screen recording chunk is190 * going to be returned.191 * @property {?string|number} bitRate - The video bit rate for the video, in megabits per second.192 * The default value is 4. You can increase the bit rate to improve video quality,193 * but doing so results in larger movie files.194 * @property {?boolean} forceRestart - Whether to try to catch and upload/return the currently running screen recording195 * (`false`, the default setting) or ignore the result of it and start a new recording196 * immediately (`true`).197 */198/**199 * Record the display of a real devices running Android 4.4 (API level 19) and higher.200 * Emulators are supported since API level 27 (Android P).201 * It records screen activity to an MPEG-4 file. Audio is not recorded with the video file.202 * If screen recording has been already started then the command will stop it forcefully and start a new one.203 * The previously recorded video file will be deleted.204 *205 * @param {?StartRecordingOptions} options - The available options.206 * @returns {string} Base64-encoded content of the recorded media file if207 * any screen recording is currently running or an empty string.208 * @throws {Error} If screen recording has failed to start or is not supported on the device under test.209 */210commands.startRecordingScreen = async function startRecordingScreen (options = {}) {211 await verifyScreenRecordIsSupported(this.adb, this.isEmulator());212 let result = '';213 const {videoSize, timeLimit = DEFAULT_RECORDING_TIME_SEC, bugReport, bitRate, forceRestart} = options;214 if (!forceRestart) {215 result = await this.stopRecordingScreen(options);216 }217 if (await terminateBackgroundScreenRecording(this.adb, true)) {218 log.warn(`There were some ${SCREENRECORD_BINARY} process leftovers running ` +219 `in the background. Make sure you stop screen recording each time after it is started, ` +220 `otherwise the recorded media might quickly exceed all the free space on the device under test.`);221 }222 if (!_.isEmpty(this._screenRecordingProperties)) {223 for (const record of (this._screenRecordingProperties.records || [])) {224 await this.adb.rimraf(record);225 }226 this._screenRecordingProperties = null;227 }228 const timeout = parseFloat(timeLimit);229 if (isNaN(timeout) || timeout > MAX_TIME_SEC || timeout <= 0) {230 throw new Error(`The timeLimit value must be in range [1, ${MAX_TIME_SEC}] seconds. ` +231 `The value of '${timeLimit}' has been passed instead.`);232 }233 this._screenRecordingProperties = {234 startTimestamp: process.hrtime(),235 videoSize,236 timeLimit,237 currentTimeLimit: timeLimit,238 bitRate,239 bugReport,240 records: [],241 recordingProcess: null,242 stopped: false,243 };244 await scheduleScreenRecord(this.adb, this._screenRecordingProperties);245 return result;246};247/**248 * @typedef {Object} StopRecordingOptions249 *250 * @property {?string} remotePath - The path to the remote location, where the resulting video should be uploaded.251 * The following protocols are supported: http/https, ftp.252 * Null or empty string value (the default setting) means the content of resulting253 * file should be encoded as Base64 and passed as the endpount response value.254 * An exception will be thrown if the generated media file is too big to255 * fit into the available process memory.256 * @property {?string} user - The name of the user for the remote authentication.257 * @property {?string} pass - The password for the remote authentication.258 * @property {?string} method - The http multipart upload method name. The 'PUT' one is used by default....
Using AI Code Generation
1var webdriverio = require('webdriverio');2var options = {3 desiredCapabilities: {4 }5};6var client = webdriverio.remote(options);7 .init()8 .then(function () {9 return client.startScreenRecord();10 })11 .then(function () {12 return client.pause(5000);13 })14 .then(function () {15 return client.stopScreenRecord();16 })17 .then(function (base64Data) {18 return client.end();19 })20 .catch(function (err) {21 console.log(err);22 return client.end();23 });24{25 "scripts": {26 },27 "dependencies": {28 }29}
Using AI Code Generation
1const { AndroidDriver } = require('appium-android-driver');2const adb = require('appium-adb');3const { util } = require('appium-support');4const path = require('path');5const { exec } = require('teen_process');6const { fs } = require('appium-support');7const adbPath = '/Users/saikrishna/Downloads/platform-tools/adb';8const adbExec = new adb.ADB({adb: adbPath});9const androidDriver = new AndroidDriver({adb: adbExec});10const adbPath = '/Users/saikrishna/Downloads/platform-tools/adb';11const adbExec = new adb.ADB({adb: adbPath});12const androidDriver = new AndroidDriver({adb: adbExec});13const opts = {14};15const apkPath = '/Users/saikrishna/Desktop/ApiDemos-debug.apk';16const appPackage = 'io.appium.android.apis';17const appActivity = '.view.TextFields';18const appWaitActivity = '.view.TextFields';19const startRecording = async function (opts = {}) {20 const {21 } = opts;22 const adbExec = await adb.getAdbWithCorrectAdbPath(remoteAdbHost, remoteAdbPort);23 const cmd = ['shell', 'screenrecord', '--verbose', '/sdcard/demo.mp4'];24 if (bugReport) {25 cmd.push('--bugreport');26 }27 if (videoSize) {28 cmd.push(`--size ${videoSize}`);29 }
Using AI Code Generation
1driver.scheduleScreenRecord();2driver.stopScreenRecord();3driver.startRecordingScreen();4driver.stopRecordingScreen();5driver.startRecordingScreen();6driver.stopRecordingScreen();7driver.startRecordingScreen();8driver.stopRecordingScreen();9driver.startRecordingScreen();10driver.stopRecordingScreen();11driver.startRecordingScreen();12driver.stopRecordingScreen();13driver.startRecordingScreen();14driver.stopRecordingScreen();15driver.startRecordingScreen();16driver.stopRecordingScreen();17driver.startRecordingScreen();18driver.stopRecordingScreen();19driver.startRecordingScreen();20driver.stopRecordingScreen();21driver.startRecordingScreen();22driver.stopRecordingScreen();
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var fs = require('fs');4var path = require('path');5var async = require('async');6var test = require('selenium-webdriver/testing');7var browser = wd.promiseChainRemote('localhost', 4723);8test.describe('Android Screen Recording', function() {9 this.timeout(300000);10 test.before(function() {11 var desired = {12 };13 return browser.init(desired);14 });15 test.after(function() {16 return browser.quit();17 });18 test.it('should record the screen', function() {19 .sleep(10000)20 .scheduleScreenRecord()21 .then(function(screenRecordPath) {22 console.log('Screen recording saved at: ' + screenRecordPath);23 });24 });25});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!