Best JavaScript code snippet using appium-android-driver
js-wdio.js
Source:js-wdio.js
...129 codeFor_pullFile (varNameIgnore, varIndexIgnore, pathToPullFrom) {130 return `let data = await driver.pullFile('${pathToPullFrom}');`;131 }132 codeFor_pullFolder (varNameIgnore, varIndexIgnore, folderToPullFrom) {133 return `let data = await driver.pullFolder('${folderToPullFrom}');`;134 }135 codeFor_toggleAirplaneMode () {136 return `await driver.toggleAirplaneMode();`;137 }138 codeFor_toggleData () {139 return `await driver.toggleData();`;140 }141 codeFor_toggleWiFi () {142 return `await driver.toggleWiFi();`;143 }144 codeFor_toggleLocationServices () {145 return `await driver.toggleLocationServices();`;146 }147 codeFor_sendSMS () {...
js-wd.js
Source:js-wd.js
...121 codeFor_pullFile (varNameIgnore, varIndexIgnore, pathToPullFrom) {122 return `let fileBase64 = await driver.pullFile('${pathToPullFrom}');`;123 }124 codeFor_pullFolder (varNameIgnore, varIndexIgnore, folderToPullFrom) {125 return `let fileBase64 = await driver.pullFolder('${folderToPullFrom}');`;126 }127 codeFor_toggleAirplaneMode () {128 return `await driver.toggleAirplaneMode();`;129 }130 codeFor_toggleData () {131 return `await driver.toggleData();`;132 }133 codeFor_toggleWiFi () {134 return `await driver.toggleWiFi();`;135 }136 codeFor_toggleLocationServices () {137 return `await driver.toggleLocationServices();`;138 }139 codeFor_sendSMS (varNameIgnore, varIndexIgnore, phoneNumber, text) {...
js-oxygen.js
Source:js-oxygen.js
1import Framework from './framework';2class JsOxygenFramework extends Framework {3 get language () {4 return 'js';5 }6 wrapWithBoilerplate (code) {7 let caps = JSON.stringify(this.caps);8 let url = JSON.stringify(`${this.scheme}://${this.host}:${this.port}${this.path}`);9 return `// Requires the Oxygen HQ client library10// (npm install oxygen-cli -g)11// Then paste this into a .js file and run with:12// oxygen <file>.js13mob.init(${caps}, ${url});14${code}15`;16 }17 codeFor_findAndAssign (strategy, locator, localVar, isArray) {18 // wdio has its own way of indicating the strategy in the locator string19 switch (strategy) {20 case 'xpath': break; // xpath does not need to be updated21 case 'accessibility id': locator = `~${locator}`; break;22 case 'id': locator = `id=${locator}`; break;23 case 'name': locator = `name=${locator}`; break;24 case 'class name': locator = `css=${locator}`; break;25 case '-android uiautomator': locator = `android=${locator}`; break;26 case '-android datamatcher': locator = `android=${locator}`; break;27 case '-ios predicate string': locator = `ios=${locator}`; break;28 case '-ios class chain': locator = `ios=${locator}`; break; // TODO: Handle IOS class chain properly. Not all libs support it. Or take it out29 default: throw new Error(`Can't handle strategy ${strategy}`);30 }31 if (isArray) {32 return `let ${localVar} = mob.findElements(${JSON.stringify(locator)});`;33 } else {34 return `let ${localVar} = mob.findElement(${JSON.stringify(locator)});`;35 }36 }37 codeFor_click (varName, varIndex) {38 return `mob.click(${this.getVarName(varName, varIndex)});`;39 }40 codeFor_clear (varName, varIndex) {41 return `mob.clear(${this.getVarName(varName, varIndex)});`;42 }43 codeFor_sendKeys (varName, varIndex, text) {44 return `mob.type(${this.getVarName(varName, varIndex)}, ${JSON.stringify(text)});`;45 }46 codeFor_back () {47 return `mob.back();`;48 }49 codeFor_tap (varNameIgnore, varIndexIgnore, x, y) {50 return `mob.tap(${x}, ${y});`;51 }52 codeFor_swipe (varNameIgnore, varIndexIgnore, x1, y1, x2, y2) {53 return `mob.swipeScreen(${x1}, ${y1}, ${x2}, ${y2});`;54 }55 codeFor_getCurrentActivity () {56 return `let activityName = mob.getCurrentActivity();`;57 }58 codeFor_getCurrentPackage () {59 return `let packageName = mob.getCurrentPackage();`;60 }61 codeFor_installAppOnDevice (varNameIgnore, varIndexIgnore, app) {62 return `mob.installApp('${app}');`;63 }64 codeFor_isAppInstalledOnDevice (varNameIgnore, varIndexIgnore, app) {65 return `let isAppInstalled = mob.isAppInstalled("${app}");`;66 }67 codeFor_launchApp () {68 return `mob.launchApp();`;69 }70 codeFor_backgroundApp (varNameIgnore, varIndexIgnore, timeout) {71 return `mob.driver().background(${timeout});`;72 }73 codeFor_closeApp () {74 return `mob.closeApp();`;75 }76 codeFor_resetApp () {77 return `mob.resetApp();`;78 }79 codeFor_removeAppFromDevice (varNameIgnore, varIndexIgnore, app) {80 return `mob.removeApp('${app}')`;81 }82 codeFor_getAppStrings (varNameIgnore, varIndexIgnore, language, stringFile) {83 return `let appStrings = mob.driver().getAppStrings(${language ? `${language}, ` : ''}${stringFile ? `"${stringFile}` : ''});`;84 }85 codeFor_getClipboard () {86 return `let clipboardText = mob.driver().getClipboard();`;87 }88 codeFor_setClipboard (varNameIgnore, varIndexIgnore, clipboardText) {89 return `mob.driver().setClipboard('${clipboardText}')`;90 }91 codeFor_pressKeycode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {92 return `mob.driver().longPressKeyCode(${keyCode}, ${metaState}, ${flags});`;93 }94 codeFor_longPressKeycode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {95 return `mob.driver().longPressKeyCode(${keyCode}, ${metaState}, ${flags});`;96 }97 codeFor_hideDeviceKeyboard () {98 return `mob.driver().hideKeyboard();`;99 }100 codeFor_isKeyboardShown () {101 return `//isKeyboardShown not supported`;102 }103 codeFor_pushFileToDevice (varNameIgnore, varIndexIgnore, pathToInstallTo, fileContentString) {104 return `mob.driver().pushFile('${pathToInstallTo}', '${fileContentString}');`;105 }106 codeFor_pullFile (varNameIgnore, varIndexIgnore, pathToPullFrom) {107 return `let data = mob.driver().pullFile('${pathToPullFrom}');`;108 }109 codeFor_pullFolder (varNameIgnore, varIndexIgnore, folderToPullFrom) {110 return `let data = mob.driver().pullFolder('${folderToPullFrom}');`;111 }112 codeFor_toggleAirplaneMode () {113 return `mob.driver().toggleAirplaneMode();`;114 }115 codeFor_toggleData () {116 return `mob.driver().toggleData();`;117 }118 codeFor_toggleWiFi () {119 return `mob.driver().toggleWiFi();`;120 }121 codeFor_toggleLocationServices () {122 return `mob.driver().toggleLocationServices();`;123 }124 codeFor_sendSMS () {125 return `// Not supported: sendSms;`;126 }127 codeFor_gsmCall () {128 return `// Not supported: gsmCall`;129 }130 codeFor_gsmSignal () {131 return `// Not supported: gsmSignal`;132 }133 codeFor_gsmVoice () {134 return `// Not supported: gsmVoice`;135 }136 codeFor_shake () {137 return `mob.shake();`;138 }139 codeFor_lock (varNameIgnore, varIndexIgnore, seconds) {140 return `mob.driver().lock(${seconds});`;141 }142 codeFor_unlock () {143 return `mob.driver().unlock();`;144 }145 codeFor_isLocked () {146 return `let isLocked = mob.driver().isLocked();`;147 }148 codeFor_rotateDevice (varNameIgnore, varIndexIgnore, x, y, radius, rotation, touchCount, duration) {149 return `mob.driver().rotateDevice(${x}, ${y}, ${radius}, ${rotation}, ${touchCount}, ${duration});`;150 }151 codeFor_getPerformanceData () {152 return `// Not supported: getPerformanceData`;153 }154 codeFor_getSupportedPerformanceDataTypes () {155 return `// Not supported: getSupportedPerformanceDataTypes`;156 }157 codeFor_performTouchId (varNameIgnore, varIndexIgnore, match) {158 return `mob.driver().touchId(${match});`;159 }160 codeFor_toggleTouchIdEnrollment (varNameIgnore, varIndexIgnore, enroll) {161 return `mob.driver().toggleEnrollTouchId(${enroll});`;162 }163 codeFor_openNotifications () {164 return `mob.driver().openNotifications();`;165 }166 codeFor_getDeviceTime () {167 return `let time = mob.getDeviceTime();`;168 }169 codeFor_fingerprint (varNameIgnore, varIndexIgnore, fingerprintId) {170 return `mob.driver().fingerPrint(${fingerprintId});`;171 }172 codeFor_sessionCapabilities () {173 return `let caps = mob.driver().capabilities;`;174 }175 codeFor_setPageLoadTimeout (varNameIgnore, varIndexIgnore, ms) {176 return `mob.driver().setTimeout({'pageLoad': ${ms}});`;177 }178 codeFor_setAsyncScriptTimeout (varNameIgnore, varIndexIgnore, ms) {179 return `mob.driver().setTimeout({'script': ${ms}});`;180 }181 codeFor_setImplicitWaitTimeout (varNameIgnore, varIndexIgnore, ms) {182 return `mob.driver().setTimeout({'implicit': ${ms}});`;183 }184 codeFor_setCommandTimeout () {185 return `// Not supported: setCommandTimeout`;186 }187 codeFor_getOrientation () {188 return `let orientation = mob.driver().getOrientation();`;189 }190 codeFor_setOrientation (varNameIgnore, varIndexIgnore, orientation) {191 return `mob.driver().setOrientation("${orientation}");`;192 }193 codeFor_getGeoLocation () {194 return `let location = mob.driver().getGeoLocation();`;195 }196 codeFor_setGeoLocation (varNameIgnore, varIndexIgnore, latitude, longitude, altitude) {197 return `mob.driver().setGeoLocation({latitude: ${latitude}, longitude: ${longitude}, altitude: ${altitude}});`;198 }199 codeFor_logTypes () {200 return `let logTypes = mob.driver().getLogTypes();`;201 }202 codeFor_log (varNameIgnore, varIndexIgnore, logType) {203 return `let logs = mob.driver().getLogs('${logType}');`;204 }205 codeFor_updateSettings (varNameIgnore, varIndexIgnore, settingsJson) {206 return `mob.driver().updateSettings(${settingsJson});`;207 }208 codeFor_settings () {209 return `let settings = mob.driver().getSettings();`;210 }211}212JsOxygenFramework.readableName = 'JS - Oxygen HQ';...
file-movement-specs.js
Source:file-movement-specs.js
1"use strict";2var setup = require("../common/setup-base")3 , env = require('../../helpers/env')4 , getAppPath = require('sample-apps')5 , fs = require('fs')6 , path = require('path')7 , Readable = require('stream').Readable8 , Simulator = require('../../../lib/devices/ios/simulator.js')9 , xcode = require('../../../lib/future.js').xcode10 , exec = require('child_process').exec11 , getSimUdid = require('../../helpers/sim-udid.js').getSimUdid12 , Unzip = require('unzip')13 , _ = require('underscore');14describe('file movements - pullFile and pushFile @skip-real-device', function () {15 var driver;16 var desired = {17 app: getAppPath('TestApp', env.REAL_DEVICE)18 };19 setup(this, desired).then(function (d) { driver = d; });20 it('should not be able to fetch a file from the file system at large', function (done) {21 driver22 .pullFile(__filename)23 .should.be.rejected24 .nodeify(done);25 });26 it('should be able to fetch the Address book', function (done) {27 driver28 .pullFile('Library/AddressBook/AddressBook.sqlitedb')29 .then(function (data) {30 var stringData = new Buffer(data, 'base64').toString();31 return stringData.indexOf('SQLite').should.not.equal(-1);32 })33 .nodeify(done);34 });35 it('should not be able to fetch something that does not exist', function (done) {36 driver37 .pullFile('Library/AddressBook/nothere.txt')38 .should.eventually.be.rejectedWith(/13/)39 .nodeify(done);40 });41 it('should be able to push and pull a file', function (done) {42 var stringData = "random string data " + Math.random();43 var base64Data = new Buffer(stringData).toString('base64');44 var remotePath = 'Library/AppiumTest/remote.txt';45 driver46 .pushFile(remotePath, base64Data)47 .pullFile(remotePath)48 .then(function (remoteData64) {49 var remoteData = new Buffer(remoteData64, 'base64').toString();50 remoteData.should.equal(stringData);51 })52 .nodeify(done);53 });54 describe('for a .app @skip-ci', function () {55 // TODO: skipping ci because of local files use, to review.56 var fileContent = "IAmTheVeryModelOfAModernMajorTestingTool";57 var fileName = "someFile.tmp";58 var fullPath = "";59 var appPath = getAppPath('TestApp', env.REAL_DEVICE);60 var appName = _.last(appPath.split(path.sep));61 before(function (done) {62 var pv = env.CAPS.platformVersion || '7.1';63 var ios8 = parseFloat(pv) >= 8;64 xcode.getMaxIOSSDK(function (err, sdk) {65 if (err) return done(err);66 var next = function (udid) {67 var sim = new Simulator({68 platformVer: pv,69 sdkVer: sdk,70 udid: udid71 });72 var simRoots = sim.getDirs();73 if (simRoots.length < 1) {74 return done(new Error("Didn't find any simulator directories"));75 }76 var basePath;77 if (ios8) {78 // ios8 apps are stored in a different directory structure, need79 // to navigate down a few more here80 basePath = path.resolve(simRoots[0], 'Containers', 'Bundle',81 'Application');82 } else {83 basePath = path.resolve(simRoots[0], 'Applications');84 }85 basePath = basePath.replace(/\s/g, '\\ ');86 var findCmd = 'find ' + basePath + ' -name "' + appName + '"';87 exec(findCmd, function (err, stdout) {88 if (err) return done(err);89 if (!stdout) return done(new Error("Could not find", appName));90 var appRoot = stdout.replace(/\n$/, '');91 fullPath = path.resolve(appRoot, fileName);92 fs.writeFile(fullPath, fileContent, done);93 });94 };95 if (parseFloat(sdk) >= 8) {96 xcode.getVersion(function (err, xcodeVersion) {97 if (err) return done(err);98 getSimUdid(xcodeVersion, sdk, env.CAPS, function (err, udid) {99 if (err) return done(err);100 next(udid);101 });102 });103 } else {104 next();105 }106 });107 });108 it('should be able to fetch a file from the app directory', function (done) {109 var arg = path.resolve('/' + appName, fileName);110 driver111 .pullFile(arg)112 .then(function (data) {113 var stringData = new Buffer(data, 'base64').toString();114 return stringData.should.equal(fileContent);115 })116 .nodeify(done);117 });118 });119 describe('file movements - pullFolder', function () {120 it('should pull all the files in Library/AddressBook', function (done) {121 var entryCount = 0;122 driver123 .pullFolder('Library/AddressBook')124 .then(function (data) {125 var zipStream = new Readable();126 zipStream._read = function noop() {};127 zipStream128 .pipe(Unzip.Parse())129 .on('entry', function (entry) {130 entryCount++;131 entry.autodrain();132 })133 .on('close', function () {134 entryCount.should.be.above(1);135 done();136 });137 zipStream.push(data, 'base64');138 zipStream.push(null);139 });140 });141 it('should not pull folders from file system', function (done) {142 driver143 .pullFolder(__dirname)144 .should.be.rejected145 .nodeify(done);146 });147 it('should not be able to fetch a folder that does not exist', function (done) {148 driver149 .pullFolder('Library/Rollodex')150 .should.eventually.be.rejectedWith(/13/)151 .nodeify(done);152 });153 });...
file-movement-e2e-specs.js
Source:file-movement-e2e-specs.js
...45 });46 });47 describe('folders', () => {48 it('should not pull folders from file system', async () => {49 await driver.pullFolder(__dirname).should.eventually.be.rejected;50 });51 it('should not be able to fetch a folder that does not exist', async () => {52 await driver.pullFolder('Library/Rollodex')53 .should.eventually.be.rejectedWith(/13/);54 });55 it('should pull all the files in Library/AddressBook', async () => {56 let entryCount = 0;57 let data = await driver.pullFolder('Library/AddressBook');58 await new B((resolve) => {59 let zipStream = new stream.Readable();60 zipStream._read = function noop () {};61 zipStream62 .pipe(unzip.Parse())63 .on('entry', function (entry) {64 entryCount++;65 entry.autodrain();66 })67 .on('close', function () {68 entryCount.should.be.above(1);69 resolve();70 });71 zipStream.push(data, 'base64');...
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var fs = require('fs');4var path = require('path');5var desired = {6};7var driver = wd.promiseChainRemote('localhost', 4723);8driver.on('status', function(info) {9 console.log(info);10});11driver.on('command', function(meth, path, data) {12 console.log(' > ' + meth + ' ' + path + ' ' + (data || ''));13});14driver.on('http', function(meth, path, data) {15 console.log(' > ' + meth + ' ' + path + ' ' + (data || ''));16});17 .init(desired)18 .then(function() {19 return driver.pullFolder('/sdcard/DCIM/Camera');20 })21 .then(function(res) {22 console.log('pullFolder result is: ' + res);23 fs.writeFileSync(path.resolve(__dirname, 'downloaded_folder.zip'), res, 'base64');24 })25 .fin(function() { return driver.quit(); })26 .done();27var wd = require('wd');28var assert = require('assert');29var fs = require('fs');30var path = require('path');31var desired = {32};33var driver = wd.promiseChainRemote('localhost', 4723);34driver.on('status', function(info) {35 console.log(info);36});37driver.on('command', function(meth, path, data) {38 console.log(' > ' + meth + ' ' + path + ' ' + (data || ''));39});40driver.on('http', function(meth, path, data) {41 console.log(' > ' + meth + ' ' + path + ' ' + (data || ''));42});43 .init(desired
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var path = require('path');4var fs = require('fs');5var app = path.resolve(__dirname, 'test.apk');6var desired = {7};8var driver = wd.promiseChainRemote('localhost', 4723);9 .init(desired)10 .then(function() {11 return driver.pullFolder('/sdcard/Download');12 })13 .then(function(res) {14 console.log(res);15 })16 .fin(function() {17 return driver.quit();18 })19 .done();
Using AI Code Generation
1var wd = require('wd');2var path = require('path');3var desired = {4 app: path.resolve(__dirname, 'ApiDemos-debug.apk')5};6var driver = wd.promiseChainRemote('localhost', 4723);7 .init(desired)8 .pullFolder('/sdcard/Download')9 .then(function(res) {10 console.log(res);11 })12 .fin(function() { return driver.quit(); })13 .done();14commands.pullFolder = async function (remotePath) {15 let files = await this.adb.ls(remotePath);16 return await B.all(files.map((file) => {17 return this.pullFile(path.posix.resolve(remotePath, file));18 }));19};
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!!