Best JavaScript code snippet using appium-xcuitest-driver
utils.js
Source:utils.js
1import B from 'bluebird';2import { utilities } from 'appium-ios-device';3import { fs, util, net, plist } from 'appium-support';4import path from 'path';5import { utils as iosUtils } from 'appium-ios-driver';6import { exec } from 'teen_process';7import xcode from 'appium-xcode';8import _ from 'lodash';9import log from './logger';10import iosGenericSimulators from './ios-generic-simulators';11import url from 'url';12import os from 'os';13import semver from 'semver';14const DEFAULT_TIMEOUT_KEY = 'default';15const XCTEST_LOG_FILES_PATTERNS = [16 /^Session-WebDriverAgentRunner.*\.log$/i,17 /^StandardOutputAndStandardError\.txt$/i,18];19const XCTEST_LOGS_CACHE_FOLDER_PREFIX = 'com.apple.dt.XCTest';20async function detectUdid () {21 log.debug('Auto-detecting real device udid...');22 const udids = await utilities.getConnectedDevices();23 if (_.isEmpty(udids)) {24 throw new Error('No device is connected to the host');25 }26 const udid = _.last(udids);27 if (udids.length > 1) {28 log.warn(`Multiple devices found: ${udids.join(', ')}`);29 log.warn(`Choosing '${udid}'. If this is wrong, manually set with 'udid' desired capability`);30 }31 log.debug(`Detected real device udid: '${udid}'`);32 return udid;33}34async function getAndCheckXcodeVersion () {35 let version;36 try {37 version = await xcode.getVersion(true);38 } catch (err) {39 log.debug(err);40 log.errorAndThrow(`Could not determine Xcode version: ${err.message}`);41 }42 // we do not support Xcodes < 7.3,43 if (version.versionFloat < 7.3) {44 log.errorAndThrow(`Xcode version '${version.versionString}'. Support for ` +45 `Xcode ${version.versionString} is not supported. ` +46 `Please upgrade to version 7.3 or higher`);47 }48 return version;49}50async function getAndCheckIosSdkVersion () {51 try {52 return await xcode.getMaxIOSSDK();53 } catch (err) {54 log.errorAndThrow(`Could not determine iOS SDK version: ${err.message}`);55 }56}57/**58 * Get the generic simulator for a given IOS version and device type (iPhone, iPad)59 *60 * @param {string|number} platformVersion IOS version. e.g.) 13.061 * @param {string} deviceName Type of IOS device. Can be iPhone, iPad (possibly more in the future)62 *63 * @returns {string} Generic iPhone or iPad simulator (if applicable)64 */65function getGenericSimulatorForIosVersion (platformVersion, deviceName) {66 let genericSimulators = iosGenericSimulators[deviceName];67 if (genericSimulators) {68 genericSimulators = genericSimulators.sort(([simOne], [simTwo]) => util.compareVersions(simOne, '<', simTwo) ? -1 : 1);69 // Find the highest iOS version in the list that is below the provided version70 let genericIosSimulator;71 for (const [platformVersionFromList, iosSimulator] of genericSimulators) {72 if (util.compareVersions(platformVersionFromList, '>', platformVersion)) {73 break;74 }75 genericIosSimulator = iosSimulator;76 }77 return genericIosSimulator;78 }79}80function translateDeviceName (platformVersion, deviceName = '') {81 const deviceNameTranslated = getGenericSimulatorForIosVersion(platformVersion, deviceName.toLowerCase().trim());82 if (deviceNameTranslated) {83 log.debug(`Changing deviceName from '${deviceName}' to '${deviceNameTranslated}'`);84 return deviceNameTranslated;85 }86 return deviceName;87}88// This map contains derived data logs folders as keys89// and values are the count of times the particular90// folder has been scheduled for removal91const derivedDataCleanupMarkers = new Map();92async function markSystemFilesForCleanup (wda) {93 if (!wda || !await wda.retrieveDerivedDataPath()) {94 log.warn('No WebDriverAgent derived data available, so unable to mark system files for cleanup');95 return;96 }97 const logsRoot = path.resolve(await wda.retrieveDerivedDataPath(), 'Logs');98 let markersCount = 0;99 if (derivedDataCleanupMarkers.has(logsRoot)) {100 markersCount = derivedDataCleanupMarkers.get(logsRoot);101 }102 derivedDataCleanupMarkers.set(logsRoot, ++markersCount);103}104async function clearSystemFiles (wda) {105 // only want to clear the system files for the particular WDA xcode run106 if (!wda || !await wda.retrieveDerivedDataPath()) {107 log.warn('No WebDriverAgent derived data available, so unable to clear system files');108 return;109 }110 const logsRoot = path.resolve(await wda.retrieveDerivedDataPath(), 'Logs');111 if (derivedDataCleanupMarkers.has(logsRoot)) {112 let markersCount = derivedDataCleanupMarkers.get(logsRoot);113 derivedDataCleanupMarkers.set(logsRoot, --markersCount);114 if (markersCount > 0) {115 log.info(`Not cleaning '${logsRoot}' folder, because the other session does not expect it to be cleaned`);116 return;117 }118 }119 derivedDataCleanupMarkers.set(logsRoot, 0);120 // Cleaning up big temporary files created by XCTest: https://github.com/appium/appium/issues/9410121 const globPattern = `${os.tmpdir()}/${XCTEST_LOGS_CACHE_FOLDER_PREFIX}*/`;122 const dstFolders = await fs.glob(globPattern);123 if (_.isEmpty(dstFolders)) {124 log.debug(`Did not find the temporary XCTest logs root at '${globPattern}'`);125 } else {126 // perform the cleanup asynchronously127 for (const dstFolder of dstFolders) {128 let scheduledFilesCount = 0;129 B.resolve(fs.walkDir(dstFolder, true, (itemPath, isDir) => {130 if (isDir) {131 return;132 }133 const fileName = path.basename(itemPath);134 if (!XCTEST_LOG_FILES_PATTERNS.some((p) => p.test(fileName))) {135 return;136 }137 // delete the file asynchronously138 fs.unlink(itemPath).catch((e) => {139 log.info(e.message);140 });141 scheduledFilesCount++;142 })).finally(() => {143 if (scheduledFilesCount > 0) {144 log.info(`Scheduled ${scheduledFilesCount} temporary XCTest log ` +145 `${util.pluralize('file', scheduledFilesCount)} for cleanup in '${dstFolder}'`);146 }147 }).catch((e) => {148 log.info(e.message);149 });150 }151 log.debug(`Started background XCTest logs cleanup in '${dstFolders}'`);152 }153 if (await fs.exists(logsRoot)) {154 log.info(`Cleaning test logs in '${logsRoot}' folder`);155 await iosUtils.clearLogs([logsRoot]);156 return;157 }158 log.info(`There is no ${logsRoot} folder, so not cleaning files`);159}160async function checkAppPresent (app) {161 log.debug(`Checking whether app '${app}' is actually present on file system`);162 if (!(await fs.exists(app))) {163 log.errorAndThrow(`Could not find app at '${app}'`);164 }165 log.debug('App is present');166}167async function getDriverInfo () {168 const stat = await fs.stat(path.resolve(__dirname, '..'));169 const built = stat.mtime.getTime();170 // get the package.json and the version from it171 const pkg = require(__filename.includes('build/lib/utils') ? '../../package.json' : '../package.json');172 const version = pkg.version;173 return {174 built,175 version,176 };177}178function normalizeCommandTimeouts (value) {179 // The value is normalized already180 if (typeof value !== 'string') {181 return value;182 }183 let result = {};184 // Use as default timeout for all commands if a single integer value is provided185 if (!isNaN(value)) {186 result[DEFAULT_TIMEOUT_KEY] = _.toInteger(value);187 return result;188 }189 // JSON object has been provided. Let's parse it190 try {191 result = JSON.parse(value);192 if (!_.isPlainObject(result)) {193 throw new Error();194 }195 } catch (err) {196 log.errorAndThrow(`"commandTimeouts" capability should be a valid JSON object. "${value}" was given instead`);197 }198 for (let [cmd, timeout] of _.toPairs(result)) {199 if (!_.isInteger(timeout) || timeout <= 0) {200 log.errorAndThrow(`The timeout for "${cmd}" should be a valid natural number of milliseconds. "${timeout}" was given instead`);201 }202 }203 return result;204}205async function printUser () {206 try {207 let {stdout} = await exec('whoami');208 log.debug(`Current user: '${stdout.trim()}'`);209 } catch (err) {210 log.debug(`Unable to get username running server: ${err.message}`);211 }212}213/**214 * Get the IDs of processes listening on the particular system port.215 * It is also possible to apply additional filtering based on the216 * process command line.217 *218 * @param {string|number} port - The port number.219 * @param {?Function} filteringFunc - Optional lambda function, which220 * receives command line string of the particular process221 * listening on given port, and is expected to return222 * either true or false to include/exclude the corresponding PID223 * from the resulting array.224 * @returns {Array<string>} - the list of matched process ids.225 */226async function getPIDsListeningOnPort (port, filteringFunc = null) {227 const result = [];228 try {229 // This only works since Mac OS X El Capitan230 const {stdout} = await exec('lsof', ['-ti', `tcp:${port}`]);231 result.push(...(stdout.trim().split(/\n+/)));232 } catch (e) {233 return result;234 }235 if (!_.isFunction(filteringFunc)) {236 return result;237 }238 return await B.filter(result, async (x) => {239 const {stdout} = await exec('ps', ['-p', x, '-o', 'command']);240 return await filteringFunc(stdout);241 });242}243/**244 * @typedef {Object} UploadOptions245 *246 * @property {?string} user - The name of the user for the remote authentication. Only works if `remotePath` is provided.247 * @property {?string} pass - The password for the remote authentication. Only works if `remotePath` is provided.248 * @property {?string} method - The http multipart upload method name. The 'PUT' one is used by default.249 * Only works if `remotePath` is provided.250 * @property {?Object} headers - Additional headers mapping for multipart http(s) uploads251 * @property {?string} fileFieldName [file] - The name of the form field, where the file content BLOB should be stored for252 * http(s) uploads253 * @property {?Object|Array<Pair>} formFields - Additional form fields for multipart http(s) uploads254 */255/**256 * Encodes the given local file to base64 and returns the resulting string257 * or uploads it to a remote server using http/https or ftp protocols258 * if `remotePath` is set259 *260 * @param {string} localPath - The path to an existing local file261 * @param {?string} remotePath - The path to the remote location, where262 * this file should be uploaded263 * @param {?UploadOptions} uploadOptions - Set of upload options264 * @returns {string} Either an empty string if the upload was successful or265 * base64-encoded file representation if `remotePath` is falsy266 */267async function encodeBase64OrUpload (localPath, remotePath = null, uploadOptions = {}) {268 if (!await fs.exists(localPath)) {269 log.errorAndThrow(`The file at '${localPath}' does not exist or is not accessible`);270 }271 if (_.isEmpty(remotePath)) {272 const {size} = await fs.stat(localPath);273 log.debug(`The size of the file is ${util.toReadableSizeString(size)}`);274 return (await util.toInMemoryBase64(localPath)).toString();275 }276 const {user, pass, method, headers, fileFieldName, formFields} = uploadOptions;277 const options = {278 method: method || 'PUT',279 headers,280 fileFieldName,281 formFields,282 };283 if (user && pass) {284 options.auth = {user, pass};285 }286 await net.uploadFile(localPath, remotePath, options);287 return '';288}289/**290 * Stops and removes all web socket handlers that are listening291 * in scope of the currect session.292 *293 * @param {Object} server - The instance of NodeJs HTTP server,294 * which hosts Appium295 * @param {string} sessionId - The id of the current session296 */297async function removeAllSessionWebSocketHandlers (server, sessionId) {298 if (!server || !_.isFunction(server.getWebSocketHandlers)) {299 return;300 }301 const activeHandlers = await server.getWebSocketHandlers(sessionId);302 for (const pathname of _.keys(activeHandlers)) {303 await server.removeWebSocketHandler(pathname);304 }305}306/**307 * @typedef {Object} PlatformOpts308 *309 * @property {boolean} isSimulator - Whether the destination platform is a Simulator310 * @property {boolean} isTvOS - Whether the destination platform is a Simulator311 */312/**313 * Verify whether the given application is compatible to the314 * platform where it is going to be installed and tested.315 *316 * @param {string} app - The actual path to the application bundle317 * @param {PlatformOpts} expectedPlatform318 * @throws {Error} If bundle architecture does not match the expected device architecture.319 */320async function verifyApplicationPlatform (app, expectedPlatform) {321 log.debug('Verifying application platform');322 const infoPlist = path.resolve(app, 'Info.plist');323 if (!await fs.exists(infoPlist)) {324 log.debug(`'${infoPlist}' does not exist`);325 return;326 }327 const {CFBundleSupportedPlatforms} = await plist.parsePlistFile(infoPlist);328 log.debug(`CFBundleSupportedPlatforms: ${JSON.stringify(CFBundleSupportedPlatforms)}`);329 if (!_.isArray(CFBundleSupportedPlatforms)) {330 log.debug(`CFBundleSupportedPlatforms key does not exist in '${infoPlist}'`);331 return;332 }333 const {334 isSimulator,335 isTvOS,336 } = expectedPlatform;337 const prefix = isTvOS ? 'AppleTV' : 'iPhone';338 const suffix = isSimulator ? 'Simulator' : 'OS';339 const dstPlatform = `${prefix}${suffix}`;340 if (!CFBundleSupportedPlatforms.includes(dstPlatform)) {341 throw new Error(`${isSimulator ? 'Simulator' : 'Real device'} architecture is unsupported by the '${app}' application. ` +342 `Make sure the correct deployment target has been selected for its compilation in Xcode.`);343 }344}345/**346 * Returns true if the urlString is localhost347 * @param {?string} urlString348 * @returns {boolean} Return true if the urlString is localhost349 */350function isLocalHost (urlString) {351 try {352 const {hostname} = url.parse(urlString);353 return ['localhost', '127.0.0.1', '::1', '::ffff:127.0.0.1'].includes(hostname);354 } catch (ign) {355 log.warn(`'${urlString}' cannot be parsed as a valid URL`);356 }357 return false;358}359/**360 * Normalizes platformVersion to a valid iOS version string361 *362 * @param {string} originalVersion - Loose version number, that can be parsed by semver363 * @return {string} iOS version number in <major>.<minor> format364 * @throws {Error} if the version number cannot be parsed365 */366function normalizePlatformVersion (originalVersion) {367 const normalizedVersion = semver.coerce(originalVersion);368 if (!normalizedVersion) {369 throw new Error(`The platform version '${originalVersion}' should be a valid version number`);370 }371 return `${normalizedVersion.major}.${normalizedVersion.minor}`;372}373export { detectUdid, getAndCheckXcodeVersion, getAndCheckIosSdkVersion, getGenericSimulatorForIosVersion,374 checkAppPresent, getDriverInfo,375 clearSystemFiles, translateDeviceName, normalizeCommandTimeouts,376 DEFAULT_TIMEOUT_KEY, markSystemFilesForCleanup, printUser,377 getPIDsListeningOnPort, encodeBase64OrUpload, removeAllSessionWebSocketHandlers,...
recordscreen.js
Source:recordscreen.js
...237 if (!await fs.exists(videoPath)) {238 log.errorAndThrow(`The screen recorder utility has failed ` +239 `to store the actual screen recording at '${videoPath}'`);240 }241 return await encodeBase64OrUpload(videoPath, remotePath, {242 user,243 pass,244 method245 });246 } finally {247 await this._recentScreenRecorder.interrupt(true);248 await this._recentScreenRecorder.cleanup();249 this._recentScreenRecorder = null;250 }251};252export { commands };...
performance.js
Source:performance.js
...24 await proc.stop();25}26async function uploadTrace (localFile, remotePath = null, uploadOptions = {}) {27 try {28 return await encodeBase64OrUpload(localFile, remotePath, uploadOptions);29 } finally {30 await fs.rimraf(localFile);31 }32}33/**34 * @typedef {Object} StartPerfRecordOptions35 *36 * @property {?number|string} timeout [300000] - The maximum count of milliseconds to record the profiling information.37 * @property {?string} profileName [Activity Monitor] - The name of existing performance profile to apply.38 * Execute `instruments -s` to show the list of available profiles.39 * Note, that not all profiles are supported on mobile devices.40 * @property {?string|number} pid - The ID of the process to meassure the performance for.41 * Set it to `current` in order to meassure the performance of42 * the process, which belongs to the currently active application....
record-audio.js
Source:record-audio.js
...224 await this._audioRecorder.cleanup();225 this._audioRecorder = null;226 throw e;227 }228 return await encodeBase64OrUpload(resultPath);229};230export { commands };...
pcap.js
Source:pcap.js
...139 await this._trafficCapture.cleanup();140 this._trafficCapture = null;141 throw e;142 }143 return await encodeBase64OrUpload(resultPath);144};145export { commands };...
Using AI Code Generation
1const { encodeBase64OrUpload } = require('appium-xcuitest-driver/lib/commands/utils');2const { encodeBase64OrUpload } = require('appium-xcuitest-driver/lib/commands/utils');3const { encodeBase64OrUpload } = require('appium-xcuitest-driver/lib/commands/utils');4const { encodeBase64OrUpload } = require('appium-xcuitest-driver/lib/commands/utils');5const { encodeBase64OrUpload } = require('appium-xcuitest-driver/lib/commands/utils');6const { encodeBase64OrUpload } = require('appium-xcuitest-driver/lib/commands/utils');7const { encodeBase64OrUpload } = require('appium-xcuitest-driver/lib/commands/utils');8const { encodeBase64OrUpload } = require('appium-xcuitest-driver/lib/commands/utils');9const { encodeBase64OrUpload } = require('appium-xcuitest-driver/lib/commands/utils');10const { encodeBase64OrUpload } = require('appium-xcuitest-driver/lib/commands/utils');11const { encodeBase64OrUpload } = require('appium-xcuitest-driver/lib/commands/utils');12const { encodeBase64OrUpload } = require('appium-xcuitest-driver/lib/commands/utils');
Using AI Code Generation
1var fs = require('fs');2var path = require('path');3var driver = require('appium-xcuitest-driver');4var XCUITestDriver = driver.XCUITestDriver;5var XCUITestDriver = new XCUITestDriver();6var caps = {7};8var image = fs.readFileSync('/Users/username/Desktop/image.png');9var imageBase64 = image.toString('base64');10var imageObj = {image: imageBase64};11XCUITestDriver.encodeBase64OrUpload(imageObj).then(function (res) {12 console.log(res);13}).catch(function (err) {14 console.log(err);15});16var fs = require('fs');17var path = require('path');18var driver = require('appium-ios-driver');19var IOSDriver = driver.IOSDriver;20var IOSDriver = new IOSDriver();21var caps = {22};23var image = fs.readFileSync('/Users/username/Desktop/image.png');24var imageBase64 = image.toString('base64');25var imageObj = {image: imageBase64};26IOSDriver.encodeBase64OrUpload(imageObj).then(function (res) {27 console.log(res);28}).catch(function (err) {29 console.log(err);30});31{ image: 'storage:filename=7d0a8a3a-9b3a-4c7d-8e2c-7a2a2a0c7e2d.png' }
Using AI Code Generation
1const { remote } = require('webdriverio');2const fs = require('fs');3const opts = {4 capabilities: {5 }6};7(async () => {8 const client = await remote(opts);9 const base64EncodedAppIcon = await client.execute('mobile: encodeBase64OrUpload', { path: '/Applications/UICatalog.app/Icon.png' });10 fs.writeFileSync('icon.png', base64EncodedAppIcon, 'base64');11 await client.deleteSession();12})();13commands.mobileEncodeBase64OrUpload = async function (opts = {}) {14 const { path } = opts;15 if (!path) {16 throw new Error('The \'path\' argument is mandatory');17 }18 const isBase64 = path.startsWith('data:');19 if (isBase64) {20 return path;21 }22 const isUrl = path.startsWith('http');23 if (isUrl) {24 const { status, value } = await this.proxyCommand(`/url`, 'POST', { url: path });25 if (status !== 0) {26 throw new Error(`Could not fetch '${path}': ${value}`);27 }28 return value;29 }30 const { status, value } = await this.proxyCommand(`/file`, 'POST', { path });31 if (status !== 0) {32 throw new Error(`Could not read '${path}': ${value}`);33 }34 return value;35};36commands.uploadFile = async function (localPath, remotePath = null) {37 const { status, value } = await this.proxyCommand(`/file`, 'POST', { path: localPath });38 if (status !== 0) {39 throw new Error(`Could not read '${localPath}': ${
Using AI Code Generation
1const driver = await wdio.remote(options);2const image = await driver.takeScreenshot();3const base64 = await driver.encodeBase64OrUpload(image);4console.log(base64);5const driver = await wdio.remote(options);6const image = await driver.takeScreenshot();7const base64 = await driver.encodeBase64OrUpload(image);8console.log(base64);9const driver = await wdio.remote(options);10const image = await driver.takeScreenshot();11const base64 = await driver.encodeBase64OrUpload(image);12console.log(base64);13const driver = await wdio.remote(options);14const image = await driver.takeScreenshot();15const base64 = await driver.encodeBase64OrUpload(image);16console.log(base64);17const driver = await wdio.remote(options);18const image = await driver.takeScreenshot();19const base64 = await driver.encodeBase64OrUpload(image);20console.log(base64);21const driver = await wdio.remote(options);22const image = await driver.takeScreenshot();23const base64 = await driver.encodeBase64OrUpload(image);24console.log(base64);25const driver = await wdio.remote(options);26const image = await driver.takeScreenshot();27const base64 = await driver.encodeBase64OrUpload(image);28console.log(base64);29const driver = await wdio.remote(options);30const image = await driver.takeScreenshot();31const base64 = await driver.encodeBase64OrUpload(image);32console.log(base64);33const driver = await wdio.remote(options);34const image = await driver.takeScreenshot();
Using AI Code Generation
1var { encodeBase64OrUpload } = require('appium-xcuitest-driver/lib/commands/element');2var { util } = require('appium-support');3async function test() {4 var image = await util.toInMemoryBase64('/Users/username/Desktop/image.png');5 var encodedImage = await encodeBase64OrUpload.call(this, image);6 console.log(encodedImage);7}8test();
Using AI Code Generation
1var encodedImage = await driver.execute('mobile: encodeBase64OrUpload', {filePath: 'test.png'});2var decodedImage = await driver.execute('mobile: decodeBase64OrDownload', {data: encodedImage, filePath: 'test.png'});3await driver.execute('mobile: saveBase64Image', {data: encodedImage, filePath: 'test.png'});4await driver.execute('mobile: deleteFile', {filePath: 'test.png'});5await driver.execute('mobile: deleteDirectory', {directoryPath: 'test'});6await driver.execute('mobile: createDirectory', {directoryPath: 'test'});7var files = await driver.execute('mobile: findFiles', {directoryPath: 'test'});8var deviceTime = await driver.execute('mobile: getDeviceTime', {});9await driver.execute('mobile: setDeviceTime', {date: '2018-05-15T10:00:00.000Z'});10var deviceTime = await driver.execute('mobile: getDeviceTime', {});
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!!