Best JavaScript code snippet using appium-base-driver
helpers.js
Source:helpers.js
...134 // overuse RAM and have a performance drop.135 if (isDir && await calculateFolderIntegrity(currentPath) >= expectedIntegrity?.folder) {136 return true;137 }138 if (!isDir && await calculateFileIntegrity(currentPath) === expectedIntegrity?.file) {139 return true;140 }141 return false;142}143async function configureApp (app, supportedAppExtensions) {144 if (!_.isString(app)) {145 // immediately shortcircuit if not given an app146 return;147 }148 if (!_.isArray(supportedAppExtensions)) {149 supportedAppExtensions = [supportedAppExtensions];150 }151 let newApp = app;152 let shouldUnzipApp = false;153 let archiveHash = null;154 const remoteAppProps = {155 lastModified: null,156 immutable: false,157 maxAge: null,158 };159 const {protocol, pathname} = url.parse(newApp);160 const isUrl = ['http:', 'https:'].includes(protocol);161 const cachedAppInfo = APPLICATIONS_CACHE.get(app);162 return await APPLICATIONS_CACHE_GUARD.acquire(app, async () => {163 if (isUrl) {164 // Use the app from remote URL165 logger.info(`Using downloadable app '${newApp}'`);166 const headers = await retrieveHeaders(newApp);167 if (!_.isEmpty(headers)) {168 if (headers['last-modified']) {169 remoteAppProps.lastModified = new Date(headers['last-modified']);170 }171 logger.debug(`Last-Modified: ${headers['last-modified']}`);172 if (headers['cache-control']) {173 remoteAppProps.immutable = /\bimmutable\b/i.test(headers['cache-control']);174 const maxAgeMatch = /\bmax-age=(\d+)\b/i.exec(headers['cache-control']);175 if (maxAgeMatch) {176 remoteAppProps.maxAge = parseInt(maxAgeMatch[1], 10);177 }178 }179 logger.debug(`Cache-Control: ${headers['cache-control']}`);180 }181 const cachedPath = getCachedApplicationPath(app, remoteAppProps, cachedAppInfo);182 if (cachedPath) {183 if (await isAppIntegrityOk(cachedPath, cachedAppInfo?.integrity)) {184 logger.info(`Reusing previously downloaded application at '${cachedPath}'`);185 return verifyAppExtension(cachedPath, supportedAppExtensions);186 }187 logger.info(`The application at '${cachedPath}' does not exist anymore ` +188 `or its integrity has been damaged. Deleting it from the internal cache`);189 APPLICATIONS_CACHE.del(app);190 }191 let fileName = null;192 const basename = fs.sanitizeName(path.basename(decodeURIComponent(pathname)), {193 replacement: SANITIZE_REPLACEMENT194 });195 const extname = path.extname(basename);196 // to determine if we need to unzip the app, we have a number of places197 // to look: content type, content disposition, or the file extension198 if (ZIP_EXTS.includes(extname)) {199 fileName = basename;200 shouldUnzipApp = true;201 }202 if (headers['content-type']) {203 const ct = headers['content-type'];204 logger.debug(`Content-Type: ${ct}`);205 // the filetype may not be obvious for certain urls, so check the mime type too206 if (ZIP_MIME_TYPES.some((mimeType) => new RegExp(`\\b${_.escapeRegExp(mimeType)}\\b`).test(ct))) {207 if (!fileName) {208 fileName = `${DEFAULT_BASENAME}.zip`;209 }210 shouldUnzipApp = true;211 }212 }213 if (headers['content-disposition'] && /^attachment/i.test(headers['content-disposition'])) {214 logger.debug(`Content-Disposition: ${headers['content-disposition']}`);215 const match = /filename="([^"]+)/i.exec(headers['content-disposition']);216 if (match) {217 fileName = fs.sanitizeName(match[1], {218 replacement: SANITIZE_REPLACEMENT219 });220 shouldUnzipApp = shouldUnzipApp || ZIP_EXTS.includes(path.extname(fileName));221 }222 }223 if (!fileName) {224 // assign the default file name and the extension if none has been detected225 const resultingName = basename226 ? basename.substring(0, basename.length - extname.length)227 : DEFAULT_BASENAME;228 let resultingExt = extname;229 if (!supportedAppExtensions.includes(resultingExt)) {230 logger.info(`The current file extension '${resultingExt}' is not supported. ` +231 `Defaulting to '${_.first(supportedAppExtensions)}'`);232 resultingExt = _.first(supportedAppExtensions);233 }234 fileName = `${resultingName}${resultingExt}`;235 }236 const targetPath = await tempDir.path({237 prefix: fileName,238 suffix: '',239 });240 newApp = await downloadApp(newApp, targetPath);241 } else if (await fs.exists(newApp)) {242 // Use the local app243 logger.info(`Using local app '${newApp}'`);244 shouldUnzipApp = ZIP_EXTS.includes(path.extname(newApp));245 } else {246 let errorMessage = `The application at '${newApp}' does not exist or is not accessible`;247 // protocol value for 'C:\\temp' is 'c:', so we check the length as well248 if (_.isString(protocol) && protocol.length > 2) {249 errorMessage = `The protocol '${protocol}' used in '${newApp}' is not supported. ` +250 `Only http: and https: protocols are supported`;251 }252 throw new Error(errorMessage);253 }254 if (shouldUnzipApp) {255 const archivePath = newApp;256 archiveHash = await calculateFileIntegrity(archivePath);257 if (archiveHash === cachedAppInfo?.archiveHash) {258 const {fullPath} = cachedAppInfo;259 if (await isAppIntegrityOk(fullPath, cachedAppInfo?.integrity)) {260 if (archivePath !== app) {261 await fs.rimraf(archivePath);262 }263 logger.info(`Will reuse previously cached application at '${fullPath}'`);264 return verifyAppExtension(fullPath, supportedAppExtensions);265 }266 logger.info(`The application at '${fullPath}' does not exist anymore ` +267 `or its integrity has been damaged. Deleting it from the cache`);268 APPLICATIONS_CACHE.del(app);269 }270 const tmpRoot = await tempDir.openDir();271 try {272 newApp = await unzipApp(archivePath, tmpRoot, supportedAppExtensions);273 } finally {274 if (newApp !== archivePath && archivePath !== app) {275 await fs.rimraf(archivePath);276 }277 }278 logger.info(`Unzipped local app to '${newApp}'`);279 } else if (!path.isAbsolute(newApp)) {280 newApp = path.resolve(process.cwd(), newApp);281 logger.warn(`The current application path '${app}' is not absolute ` +282 `and has been rewritten to '${newApp}'. Consider using absolute paths rather than relative`);283 app = newApp;284 }285 verifyAppExtension(newApp, supportedAppExtensions);286 if (app !== newApp && (archiveHash || _.values(remoteAppProps).some(Boolean))) {287 const cachedFullPath = cachedAppInfo?.fullPath;288 if (cachedFullPath && cachedFullPath !== newApp && await fs.exists(cachedFullPath)) {289 await fs.rimraf(cachedFullPath);290 }291 const integrity = {};292 if ((await fs.stat(newApp)).isDirectory()) {293 integrity.folder = await calculateFolderIntegrity(newApp);294 } else {295 integrity.file = await calculateFileIntegrity(newApp);296 }297 APPLICATIONS_CACHE.set(app, {298 ...remoteAppProps,299 timestamp: Date.now(),300 archiveHash,301 integrity,302 fullPath: newApp,303 });304 }305 return newApp;306 });307}308async function downloadApp (app, targetPath) {309 const {href} = url.parse(app);...
Using AI Code Generation
1var appiumBaseDriver = require('appium-base-driver');2var calculateFileIntegrity = appiumBaseDriver.calculateFileIntegrity;3var fileIntegrity = calculateFileIntegrity('path/to/file');4console.log(fileIntegrity);5var appiumBaseDriver = require('appium-base-driver');6var calculateFileIntegrity = appiumBaseDriver.calculateFileIntegrity;7var fileIntegrity = calculateFileIntegrity('path/to/file');8console.log(fileIntegrity);9var appiumBaseDriver = require('appium-base-driver');10var calculateFileIntegrity = appiumBaseDriver.calculateFileIntegrity;11var fileIntegrity = calculateFileIntegrity('path/to/file');12console.log(fileIntegrity);13var appiumBaseDriver = require('appium-base-driver');14var calculateFileIntegrity = appiumBaseDriver.calculateFileIntegrity;15var fileIntegrity = calculateFileIntegrity('path/to/file');16console.log(fileIntegrity);17var appiumBaseDriver = require('appium-base-driver');18var calculateFileIntegrity = appiumBaseDriver.calculateFileIntegrity;19var fileIntegrity = calculateFileIntegrity('path/to/file');20console.log(fileIntegrity);21var appiumBaseDriver = require('appium-base-driver');22var calculateFileIntegrity = appiumBaseDriver.calculateFileIntegrity;23var fileIntegrity = calculateFileIntegrity('path/to/file');24console.log(fileIntegrity);25var appiumBaseDriver = require('appium-base-driver');26var calculateFileIntegrity = appiumBaseDriver.calculateFileIntegrity;27var fileIntegrity = calculateFileIntegrity('path/to/file');28console.log(fileIntegrity);29var appiumBaseDriver = require('appium-base-driver');
Using AI Code Generation
1# from appium_base_driver import calculateFileIntegrity2# fileIntegrity = calculateFileIntegrity('/path/to/file')3# print(fileIntegrity)4# from appium_base_driver import calculateFolderIntegrity5# folderIntegrity = calculateFolderIntegrity('/path/to/folder')6# print(folderIntegrity)
Using AI Code Generation
1var appium = require('appium-base-driver');2var driver = new appium.AppiumDriver();3var fileIntegrity = driver.calculateFileIntegrity('/path/to/file');4var uiauto = require('appium-uiauto');5var driver = new uiauto.UiAutoDriver();6var fileIntegrity = driver.calculateFileIntegrity('/path/to/file');7var uiauto = require('appium-uiauto');8var driver = new uiauto.UiAutoDriver();9var fileIntegrity = driver.calculateFileIntegrity('/path/to/file');10var uiauto = require('appium-uiauto');11var driver = new uiauto.UiAutoDriver();12var fileIntegrity = driver.calculateFileIntegrity('/path/to/file');13var uiauto = require('appium-uiauto');14var driver = new uiauto.UiAutoDriver();15var fileIntegrity = driver.calculateFileIntegrity('/path/to/file');16var uiauto = require('appium-uiauto');17var driver = new uiauto.UiAutoDriver();18var fileIntegrity = driver.calculateFileIntegrity('/path/to/file');19var uiauto = require('appium-uiauto');20var driver = new uiauto.UiAutoDriver();21var fileIntegrity = driver.calculateFileIntegrity('/path/to/file');22var uiauto = require('appium-uiauto');23var driver = new uiauto.UiAutoDriver();24var fileIntegrity = driver.calculateFileIntegrity('/path/to/file');25var uiauto = require('appium-uiauto');26var driver = new uiauto.UiAutoDriver();27var fileIntegrity = driver.calculateFileIntegrity('/path/to/file');28var uiauto = require('appium-uiauto');
Using AI Code Generation
1const { BaseDriver } = require('appium-base-driver');2const { calculateFileIntegrity } = BaseDriver;3calculateFileIntegrity('/Users/selendroid/selendroid-test-app.apk', 'md5')4 .then((result) => console.log(result))5 .catch((err) => console.log(err));6const { BaseDriver } = require('appium-base-driver');7const { calculateFileIntegrity } = BaseDriver;8class MyDriver extends BaseDriver {9 async calculateFileIntegrity (remotePath, algorithm) {10 const localPath = await this.pullFile(remotePath);11 return calculateFileIntegrity(localPath, algorithm);12 }13}14module.exports = MyDriver;
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!!