Best JavaScript code snippet using appium-base-driver
helpers.js
Source:helpers.js
...131 // create some unwanted service files/cached inside of that folder or its subfolders.132 // Ofc, validating the hash sum of each file (or at least of file path) would be much133 // more precise, but we don't need to be very precise here and also don't want to134 // 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}...
Using AI Code Generation
1const wdio = require('webdriverio');2const opts = {3 capabilities: {4 },5};6async function main () {7 const driver = await wdio.remote(opts);8 await driver.calculateFolderIntegrity('path/to/your.apk');9 await driver.deleteSession();10}11main();
Using AI Code Generation
1const AppiumBaseDriver = require('appium-base-driver');2const appiumBaseDriver = new AppiumBaseDriver();3const appiumBaseDriverMethods = Object.getOwnPropertyNames(Object.getPrototypeOf(appiumBaseDriver));4console.log(appiumBaseDriverMethods);5const calculateFolderIntegrity = appiumBaseDriver.calculateFolderIntegrity;6const folderIntegrity = calculateFolderIntegrity('/Users/kazuaki/GitHub/appium-base-driver/test');7console.log(folderIntegrity);8const AppiumBaseDriver = require('appium-base-driver');9const appiumBaseDriver = new AppiumBaseDriver();10const appiumBaseDriverMethods = Object.getOwnPropertyNames(Object.getPrototypeOf(appiumBaseDriver));11console.log(appiumBaseDriverMethods);12const calculateFolderIntegrity = appiumBaseDriver.calculateFolderIntegrity;13const folderIntegrity = calculateFolderIntegrity('/Users/kazuaki/GitHub/appium-base-driver/test');14console.log(folderIntegrity);15const AppiumBaseDriver = require('appium-base-driver');16const appiumBaseDriver = new AppiumBaseDriver();17const appiumBaseDriverMethods = Object.getOwnPropertyNames(Object.getPrototypeOf(appiumBaseDriver));18console.log(appiumBaseDriverMethods);19const calculateFolderIntegrity = appiumBaseDriver.calculateFolderIntegrity;20const folderIntegrity = calculateFolderIntegrity('/Users/kazuaki/GitHub/appium-base-driver/test');21console.log(folderIntegrity);22const AppiumBaseDriver = require('appium-base-driver');23const appiumBaseDriver = new AppiumBaseDriver();24const appiumBaseDriverMethods = Object.getOwnPropertyNames(Object.getPrototypeOf(appiumBaseDriver));25console.log(appiumBaseDriverMethods);26const calculateFolderIntegrity = appiumBaseDriver.calculateFolderIntegrity;
Using AI Code Generation
1const { BaseDriver } = require('appium-base-driver');2const { fs } = require('appium-support');3async function calculateFolderIntegrity (folderPath) {4 const files = await fs.glob(`${folderPath}/**/*.*`);5 const fileHashes = await Promise.all(files.map(async (file) => {6 const hash = await fs.hash(file);7 return `${hash}:${file}`;8 }));9 return fileHashes.sort().join('|');10}11async function main () {
Using AI Code Generation
1const { AppiumBaseDriver } = require('appium-base-driver');2const calculateFolderIntegrity = AppiumBaseDriver.prototype.calculateFolderIntegrity;3const folderIntegrity = calculateFolderIntegrity('path/to/folder');4console.log(folderIntegrity);5const { AppiumBaseDriver } = require('appium-base-driver');6const calculateFolderIntegrity = AppiumBaseDriver.prototype.calculateFolderIntegrity;7const folderIntegrity = calculateFolderIntegrity('path/to/folder');8console.log(folderIntegrity);9const { AppiumBaseDriver } = require('appium-base-driver');10const calculateFolderIntegrity = AppiumBaseDriver.prototype.calculateFolderIntegrity;11const folderIntegrity = calculateFolderIntegrity('path/to/folder');12console.log(folderIntegrity);13const { AppiumBaseDriver } = require('appium-base-driver');14const calculateFolderIntegrity = AppiumBaseDriver.prototype.calculateFolderIntegrity;15const folderIntegrity = calculateFolderIntegrity('path/to/folder');16console.log(folderIntegrity);17const { AppiumBaseDriver } = require('appium-base-driver');18const calculateFolderIntegrity = AppiumBaseDriver.prototype.calculateFolderIntegrity;19const folderIntegrity = calculateFolderIntegrity('path/to/folder');20console.log(folderIntegrity);21const { AppiumBaseDriver } = require('appium-base-driver');22const calculateFolderIntegrity = AppiumBaseDriver.prototype.calculateFolderIntegrity;23const folderIntegrity = calculateFolderIntegrity('path/to/folder');24console.log(folderIntegrity);25const { AppiumBaseDriver } = require('appium-base-driver');26const calculateFolderIntegrity = AppiumBaseDriver.prototype.calculateFolderIntegrity;27const folderIntegrity = calculateFolderIntegrity('path/to/folder');28console.log(folderIntegrity);29const { AppiumBaseDriver } = require('appium-base-driver');30const calculateFolderIntegrity = AppiumBaseDriver.prototype.calculateFolderIntegrity;31const folderIntegrity = calculateFolderIntegrity('path/to/folder');32console.log(folderIntegrity);33const { AppiumBaseDriver } = require('appium-base-driver');34const calculateFolderIntegrity = AppiumBaseDriver.prototype.calculateFolderIntegrity;35const folderIntegrity = calculateFolderIntegrity('path/to/folder');36console.log(folderIntegrity);37const { AppiumBaseDriver } = require
Using AI Code Generation
1var appiumBaseDriver = require('appium-base-driver');2var driver = new appiumBaseDriver.Basedriver();3driver.calculateFolderIntegrity('/Users/abc/Desktop/test');4driver.calculateFolderIntegrity('/Users/abc/Desktop/test');5driver.calculateFolderIntegrity('/Users/abc/Desktop/test');6driver.calculateFolderIntegrity('/Users/abc/Desktop/test');7driver.calculateFolderIntegrity('/Users/abc/Desktop/test');8driver.calculateFolderIntegrity('/Users/abc/Desktop/test');9driver.calculateFolderIntegrity('/Users/abc/Desktop/test');10driver.calculateFolderIntegrity('/Users/abc/Desktop/test');11driver.calculateFolderIntegrity('/Users/abc/Desktop/test');12driver.calculateFolderIntegrity('/Users/abc/Desktop/test');13driver.calculateFolderIntegrity('/Users/abc/Desktop/test');
Using AI Code Generation
1var wd = require('wd');2var path = require('path');3var os = require('os');4var assert = require('assert');5var _ = require('underscore');6var appRoot = path.resolve(__dirname, '../..');7var appDir = path.resolve(appRoot, 'test/functional');8var testDir = path.resolve(appDir, 'test');9var appium = require('appium');10var appiumServer = appium.main;11var serverPort = 4723;12var desiredCaps = {13 app: path.resolve(appDir, 'ApiDemos-debug.apk'),14};15var driver = wd.promiseChainRemote('localhost', serverPort);16var folderIntegrity = require(path.resolve(appRoot, 'lib/appium.js')).calculateFolderIntegrity;17var folderIntegrity1 = folderIntegrity(testDir, appDir);18console.log(folderIntegrity1);19driver.init(desiredCaps)20 .then(function () {21 console.log('Appium session created with sessionId: ' + driver.sessionID);22 return driver.quit();23 })24 .fin(function () {25 process.exit(0);26 })27 .done();28var Appium = function () {29 this.appium = require('appium');30 this.appiumServer = this.appium.main;31 this.serverPort = 4723;32 this.desiredCaps = {33 app: path.resolve(appDir, 'ApiDemos-debug.apk'),34 };35 this.driver = wd.promiseChainRemote('localhost', this.serverPort);
Using AI Code Generation
1const { BaseDriver } = require('appium-base-driver');2const { calculateFolderIntegrity } = BaseDriver;3const folderPath = '/path/to/folder';4const folderIntegrity = calculateFolderIntegrity(folderPath);5console.log(folderIntegrity);6const { BaseDriver } = require('appium-base-driver');7const { calculateFolderIntegrity } = BaseDriver;8const folderPath = '/path/to/folder';9const folderIntegrity = calculateFolderIntegrity(folderPath);10console.log(folderIntegrity);11const { BaseDriver } = require('appium-base-driver');12const { calculateFolderIntegrity } = BaseDriver;13const folderPath = '/path/to/folder';14const folderIntegrity = calculateFolderIntegrity(folderPath);15console.log(folderIntegrity);16const { BaseDriver } = require('appium-base-driver');17const { calculateFolderIntegrity } = BaseDriver;18const folderPath = '/path/to/folder';19const folderIntegrity = calculateFolderIntegrity(folderPath);20console.log(folderIntegrity);21const { BaseDriver } = require('appium-base-driver');22const { calculateFolderIntegrity } = BaseDriver;23const folderPath = '/path/to/folder';24const folderIntegrity = calculateFolderIntegrity(folderPath);25console.log(folderIntegrity);26const { BaseDriver } = require('appium-base-driver');27const { calculateFolderIntegrity } = BaseDriver;28const folderPath = '/path/to/folder';
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!!