Best JavaScript code snippet using appium-base-driver
proxy-specs.js
Source:proxy-specs.js
...156 describe('req/res proxy', () => {157 it('should successfully proxy via req and send to res', async () => {158 let j = mockProxy();159 let [req, res] = buildReqRes('/status', 'GET');160 await j.proxyReqRes(req, res);161 res.headers['Content-type'].should.equal('application/json');162 res.sentCode.should.equal(200);163 res.sentBody.should.eql({status: 0, value: {foo: 'bar'}});164 });165 it('should rewrite the inner session id so it doesnt change', async () => {166 let j = mockProxy({sessionId: '123'});167 let [req, res] = buildReqRes('/element/200/value', 'GET');168 await j.proxyReqRes(req, res);169 res.sentBody.should.eql({status: 0, value: 'foobar', sessionId: '123'});170 });171 it('should rewrite the inner session id with sessionId in url', async () => {172 let j = mockProxy({sessionId: '123'});173 let [req, res] = buildReqRes('/wd/hub/session/456/element/200/value', 'POST');174 await j.proxyReqRes(req, res);175 res.sentBody.should.eql({status: 0, value: 'foobar', sessionId: '456'});176 });177 it('should pass through urls that do not require session IDs', async () => {178 let j = mockProxy({sessionId: '123'});179 let [req, res] = buildReqRes('/wd/hub/status', 'GET');180 await j.proxyReqRes(req, res);181 res.sentBody.should.eql({status: 0, value: {'foo':'bar'}});182 });183 it('should proxy strange responses', async () => {184 let j = mockProxy({sessionId: '123'});185 let [req, res] = buildReqRes('/nochrome', 'GET');186 await j.proxyReqRes(req, res);187 res.sentCode.should.equal(100);188 res.sentBody.should.eql({status: 0, value: {message: 'chrome not reachable'}});189 });190 });...
winappdriver.js
Source:winappdriver.js
...136 async sendCommand (url, method, body) {137 return await this.jwproxy.command(url, method, body);138 }139 async proxyReq (req, res) {140 return await this.jwproxy.proxyReqRes(req, res);141 }142 async killAll () {143 let cmd;144 // js hint cannot handle backticks, even escaped, within template literals145 cmd = "FOR /F \"usebackq tokens=5\" %a in (`netstat -nao ^| " +146 "findstr /R /C:\"" + this.proxyPort + " \"`) do (" +147 "FOR /F \"usebackq\" %b in (`TASKLIST /FI \"PID eq %a\" ^| " +148 "findstr /I winappdriver.exe`) do (IF NOT %b==\"\" TASKKILL " +149 "/F /PID %a))";150 log.info(`Killing any old WinAppDrivers, running: ${cmd}`);151 try {152 // use cp.exec instead of teen process because of crazy windows quoting153 await (B.promisify(cp.exec))(cmd);154 log.info("Successfully cleaned up old WinAppDrivers");...
appium-for-mac.js
Source:appium-for-mac.js
...102 }103 return res;104 }105 async proxyReq (req, res) {106 return await this.jwproxy.proxyReqRes(req, res);107 }108 async killAll () {109 const processName = 'AppiumForMac';110 // js hint cannot handle backticks, even escaped, within template literals111 log.info(`Killing any old AppiumForMac`);112 await process.killProcess(processName);113 log.info('Successfully cleaned up old Appium4Mac servers');114 }115 async deleteSession () {116 log.debug('Deleting AppiumForMac server session');117 // rely on jwproxy's intelligence to know what we're talking about and118 // delete the current session119 try {120 await this.sendCommand('/', 'DELETE');...
driver.js
Source:driver.js
1import { BaseDriver } from 'appium-base-driver';2import { fs } from 'appium-support';3import { installApp, launch } from 'node-simctl';4import { extractBundleId } from 'ios-app-utils';5import WebDriverAgent from './webdriveragent';6import log from './logger';7import { simBooted, createSim } from './simulatorManagement.js';8import { killAllSimulators, getSimulator } from 'appium-ios-simulator';9import { retryInterval } from 'asyncbox';10class WebDriverAgentDriver extends BaseDriver {11 constructor (opts = {}, shouldValidateCaps = true) {12 super(opts, shouldValidateCaps);13 // TODO set up desired cap constraints14 //this.desiredCapConstraints = desiredCapConstraints;15 this.wda = null;16 this.sim = null;17 this.jwpProxyActive = false;18 this.proxyReqRes = null;19 this.jwpProxyAvoid = [];20 }21 get driverData () {22 // TODO fill out resource info here23 return {};24 }25 async createSession (caps) {26 try {27 // TODO add validation on caps28 // TODO handle otherSessionData for multiple sessions29 let sessionId;30 [sessionId] = await super.createSession(caps);31 // fail very early if the app doesn't actually exist32 await this.checkAppPresent();33 if (!this.opts.bundleId) {34 this.opts.bundleId = await extractBundleId(this.opts.app);35 }36 // start sim, or use running sim37 log.info('starting simlator (if not booted)');38 this.sim = await this.startSim();39 this.wda = new WebDriverAgent({40 udid: this.sim.udid,41 platformVersion: this.opts.platformVersion,42 host: this.opts.host,43 agentPath: this.opts.agentPath44 });45 await this.wda.launch(sessionId);46 log.info("Installing the app");47 // TODO don't re-install (need to check if version numbers match)?48 await installApp(this.sim.udid, this.opts.app);49 log.info("Launching the app");50 await this.launchApp(this.sim.udid, this.opts.bundleId);51 this.proxyReqRes = this.wda.proxyReqRes.bind(this.wda);52 this.jwpProxyActive = true;53 return [sessionId, caps];54 } catch (e) {55 await this.deleteSession();56 throw e;57 }58 }59 async deleteSession () {60 this.jwpProxyActive = false;61 this.proxyReqRes = null;62 await this.wda.proc.stop();63 // TODO: this kills all simulators, so needs to be changed if running multiple sims64 // TODO: add handling of 'reset' desired caps (no-reset, full-reset, etc.)65 await this.sim.shutdown();66 if (!this.caps.udid) {67 await this.sim.delete();68 }69 await super.deleteSession();70 }71 async checkAppPresent () {72 log.debug("Checking whether app is actually present");73 if (!(await fs.exists(this.opts.app))) {74 log.errorAndThrow(`Could not find app at ${this.opts.app}`);75 }76 }77 async startSim () {78 let sim;79 // TODO for now just kill all sims unless specified udid is booted.80 // if booted, use it. if not booted, start it up81 // if no udid, well lets see if we can start one up based on desired caps82 // if we support multiple sims we need to change this83 if (this.opts.udid) {84 sim = await getSimulator(this.opts.udid);85 if (!await simBooted(this.opts.udid)) {86 log.info(`simulator with udid ${this.opts.udid} not booted. Booting up now`);87 await killAllSimulators();88 await sim.run();89 } else {90 log.info(`simulator ${this.opts.udid} already booted`);91 }92 return sim;93 }94 log.info(`simulator udid not provided, using desired caps to create a new sim`);95 // create sim for caps96 await killAllSimulators();97 sim = await createSim(this.caps, this.sessionId);98 log.info(`created simulator ${sim.udid}. Booting it up`);99 await sim.run();100 log.info(`simulator booted`);101 return sim;102 }103 async launchApp () {104 const APP_LAUNCH_TIMEOUT = 20 * 1000;105 await launch(this.sim.udid, this.opts.bundleId);106 let checkStatus = async () => {107 let response = await this.wda.jwproxy.command('/status', 'GET');108 let currentApp = response.currentApp.bundleID;109 if (currentApp !== this.opts.bundleId) {110 throw new Error(`${this.opts.bundleId} not in foreground. ${currentApp} is in foreground`);111 }112 };113 log.info(`waiting for ${this.opts.bundleId} to be in foreground`);114 await retryInterval(APP_LAUNCH_TIMEOUT / 200, 200, checkStatus);115 log.info(`${this.opts.bundleId} is in foreground`);116 }117}...
selendroid.js
Source:selendroid.js
1import { JWProxy } from 'appium-base-driver';2import { retryInterval } from 'asyncbox';3import logger from './logger';4import path from 'path';5import { fs } from 'appium-support';6import { SE_APK_PATH, SE_MANIFEST_PATH } from './installer';7const REQD_PARAMS = [8 'adb', 'appPackage', 'appActivity', 'tmpDir', 'apk', 'host', 'systemPort',9 'devicePort',10];11class SelendroidServer {12 constructor (opts = {}) {13 for (let req of REQD_PARAMS) {14 if (!opts || !opts[req]) {15 throw new Error(`Option '${req}' is required!`);16 }17 this[req] = opts[req];18 }19 // new package name for repackaged selendroid server20 this.modServerPkg = `selendroid.${this.appPackage}`;21 // path to the repackaged selendroid server specific to this app22 this.modServerPath = path.resolve(this.tmpDir, `${this.modServerPkg}.apk`);23 this.jwproxy = new JWProxy({server: this.host, port: this.systemPort});24 this.proxyReqRes = this.jwproxy.proxyReqRes.bind(this.jwproxy);25 }26 async prepareModifiedServer () {27 // TODO might have a race condition if we try building this with multiple28 // sessions at the same time. OTOH we probably want to share the mod29 // server...30 let needsUninstall = false;31 if (!(await fs.exists(this.modServerPath))) {32 await this.buildNewModServer();33 needsUninstall = true;34 }35 needsUninstall = await this.checkAndSignCert(this.modServerPath) || needsUninstall;36 if (needsUninstall) {37 logger.info('New server was built, uninstalling any instances of it');38 await this.adb.uninstallApk(this.modServerPkg);39 }40 }41 async installModifiedServer () {42 let installed = await this.adb.isAppInstalled(this.modServerPkg);43 if (!installed) {44 await this.adb.install(this.modServerPath);45 }46 }47 async buildNewModServer () {48 logger.info(`Repackaging selendroid for: '${this.appPackage}'`);49 let packageTmpDir = path.resolve(this.tmpDir, this.appPackage);50 let newManifestPath = path.resolve(this.tmpDir, 'AndroidManifest.xml');51 logger.info(`Creating new manifest: '${newManifestPath}'`);52 await fs.mkdir(packageTmpDir);53 await fs.copyFile(SE_MANIFEST_PATH, newManifestPath);54 await this.adb.initAapt(); // TODO this should be internal to adb55 await this.adb.compileManifest(newManifestPath, this.modServerPkg,56 this.appPackage);57 await this.adb.insertManifest(newManifestPath, SE_APK_PATH,58 this.modServerPath);59 logger.info(`Repackaged selendroid ready: '${this.modServerPath}'`);60 }61 async checkAndSignCert (apk) {62 let signed = await this.adb.checkApkCert(apk, this.appPackage);63 if (!signed) {64 await this.adb.sign(apk);65 }66 // return whether the apk was signed67 return !signed;68 }69 async startSession (caps) {70 let instrumentWith = `${this.modServerPkg}/` +71 `io.selendroid.server.ServerInstrumentation`;72 logger.info(`Starting selendroid server with instrumentation: ` +73 `${instrumentWith}`);74 await this.adb.instrument(this.appPackage, this.appActivity, instrumentWith);75 logger.info('Waiting for Selendroid to be online...');76 // wait 20s for Selendroid to be online77 await retryInterval(20, 1000, async () => {78 await this.jwproxy.command('/status', 'GET');79 });80 await this.jwproxy.command('/session', 'POST', {desiredCapabilities: caps});81 }82 async deleteSession () {83 logger.debug('Deleting Selendroid server session');84 // rely on jwproxy's intelligence to know what we're talking about and85 // delete the current session86 try {87 await this.jwproxy.command('/', 'DELETE');88 } catch (err) {89 logger.warn(`Did not get confirmation Selendroid deleteSession worked; ` +90 `Error was: ${err}`);91 }92 }93}...
proxy.js
Source:proxy.js
...39 // both40 var handler = function (err) {41 logger.error(err.message);42 };43 var p = req.device.proxyReqRes(req, res, handler);44 if (p.catch) {45 p.catch(handler);46 }47 return;48 }49 logger.debug("Proxying command to " + req.device.proxyHost + ":" +50 req.device.proxyPort);51 var sessRe = new RegExp('^/wd/hub/session/([^/]+)');52 var sessionRegxMatch = sessRe.exec(req.originalUrl);53 var newPath, origSessId = null;54 // there might be no session id in the orig. req.55 if (sessionRegxMatch) {56 origSessId = sessionRegxMatch[1];57 var sessId = req.device.proxySessionId ? req.device.proxySessionId :...
appium-for-awtk.js
Source:appium-for-awtk.js
...60 }61 return res;62 }63 async proxyReq (req, res) {64 return await this.jwproxy.proxyReqRes(req, res);65 }66 async killAll () {67 const processName = 'AppiumForAwtk';68 // js hint cannot handle backticks, even escaped, within template literals69 log.info(`Killing any old AppiumForAwtk`);70 await process.killProcess(processName);71 log.info('Successfully cleaned up old Appium4Awtk servers');72 }73 async deleteSession () {74 log.debug('Deleting AppiumForAwtk server session');75 // rely on jwproxy's intelligence to know what we're talking about and76 // delete the current session77 try {78 await this.sendCommand('/', 'DELETE');...
webdriveragent.js
Source:webdriveragent.js
1import _ from 'lodash';2import path from 'path';3import { spawnSubProcess } from 'node-simctl';4import { JWProxy } from 'appium-jsonwp-proxy';5import { fs } from 'appium-support';6import log from './logger';7import { getLogger } from 'appium-logger';8import { simBooted } from './simulatorManagement.js';9const agentLog = getLogger('WebDriverAgent');10const BIN_PATH = path.resolve(__dirname, '..', '..', 'bin');11const REQ_ARGS = ['udid', 'platformVersion', 'host'];12class WebDriverAgent {13 // agentPath (optional): Path to WebdriverAgent Executable (inside WebDriverAgent.app)14 constructor (args = {}) {15 for (let reqArg of REQ_ARGS) {16 if (_.isUndefined(args[reqArg])) {17 throw new Error(`You must send in the '${reqArg}' argument`);18 }19 }20 if (args.agentPath) {21 log.info(`Custom agent path specified: ${args.agentPath}`);22 } else {23 log.info(`Using default agent`);24 }25 this.udid = args.udid;26 this.platformVersion = args.platformVersion;27 this.host = args.host;28 this.agentPath = args.agentPath || path.resolve(BIN_PATH, `WebDriverAgent-${this.platformVersion}`);29 }30 async launch (sessionId) {31 log.info("Launching WebDriverAgent on the device");32 if (!await fs.exists(this.agentPath)) {33 throw new Error(`Trying to use WebDriverAgent binary at ${this.agentPath} but it ` +34 `does not exist. Check your platformVersion?`);35 }36 if (!await simBooted(this.udid)) {37 log.errorAndThrow(new Error(`simulator ${this.udid} is not in 'booted' state`));38 }39 this.proc = await spawnSubProcess(this.udid, this.agentPath);40 this.proc.on('output', (d, e) => {41 // NSLog logs to stderr, so nothing hits stdout. *shrug*42 if (d.length) {43 agentLog.info(d);44 }45 if (e.length) {46 agentLog.info(e);47 }48 });49 let port;50 let startupDetector = (stdout, stderr) => {51 if (stderr.indexOf('WebDriverAgent started on port') > -1) {52 let r = /WebDriverAgent started on port (\d+)/;53 let match = r.exec(stderr);54 if (match) {55 port = match[1];56 log.info(`detected that WebDriverAgent is running on port ${port}`);57 } else {58 log.errorAndThrow(new Error('No port detected from WebDriverAgent'));59 }60 return true;61 }62 };63 await this.proc.start(startupDetector);64 this.port = port;65 this.jwproxy = new JWProxy({host: this.host, port: this.port, base: ''});66 this.jwproxy.sessionId = sessionId;67 this.proxyReqRes = this.jwproxy.proxyReqRes.bind(this.jwproxy);68 }69}...
Using AI Code Generation
1const appiumBaseDriver = require('appium-base-driver');2const AppiumBaseDriver = appiumBaseDriver.AppiumBaseDriver;3const proxyReqRes = AppiumBaseDriver.proxyReqRes;4const proxy = require('appium-base-driver').proxy;5const appiumBaseDriver = require('appium-base-driver');6const AppiumBaseDriver = appiumBaseDriver.AppiumBaseDriver;7const proxyReqRes = AppiumBaseDriver.proxyReqRes;8const proxy = require('appium-base-driver').proxy;9const appiumBaseDriver = require('appium-base-driver');10const AppiumBaseDriver = appiumBaseDriver.AppiumBaseDriver;11const proxyReqRes = AppiumBaseDriver.proxyReqRes;12const proxy = require('appium-base-driver').proxy;13const appiumBaseDriver = require('appium-base-driver');14const AppiumBaseDriver = appiumBaseDriver.AppiumBaseDriver;15const proxyReqRes = AppiumBaseDriver.proxyReqRes;16const proxy = require('appium-base-driver').proxy;17const appiumBaseDriver = require('appium-base-driver');18const AppiumBaseDriver = appiumBaseDriver.AppiumBaseDriver;19const proxyReqRes = AppiumBaseDriver.proxyReqRes;20const proxy = require('appium-base-driver').proxy;21const appiumBaseDriver = require('appium-base-driver');22const AppiumBaseDriver = appiumBaseDriver.AppiumBaseDriver;23const proxyReqRes = AppiumBaseDriver.proxyReqRes;24const proxy = require('appium-base-driver').proxy;25const appiumBaseDriver = require('appium-base-driver');26const AppiumBaseDriver = appiumBaseDriver.AppiumBaseDriver;27const proxyReqRes = AppiumBaseDriver.proxyReqRes;28const proxy = require('appium-base-driver').proxy;29const appiumBaseDriver = require('appium-base-driver');30const AppiumBaseDriver = appiumBaseDriver.AppiumBaseDriver;
Using AI Code Generation
1const { AppiumServer, AppiumDriver, AppiumBaseDriver } = require('appium');2const { startServer } = require('appium/build/lib/main');3const { AndroidDriver } = require('appium-android-driver');4const { AndroidUiautomator2Driver } = require('appium-uiautomator2-driver');5const server = new AppiumServer();6const driver = new AndroidUiautomator2Driver();7const baseDriver = new AppiumBaseDriver();8const proxyReqRes = async function (driver, proxyReq, proxyRes, req, res) {9 if (req.url.includes('/session/')) {10 if (proxyRes.statusCode === 200) {11 const body = JSON.parse(proxyRes.body.toString('utf8'));12 body.value = body.value.map((el) => {13 el.udid = '123456789';14 return el;15 });16 proxyRes.body = JSON.stringify(body);17 }18 }19};20driver.proxyReqRes = proxyReqRes;21startServer(server, driver, baseDriver).then(() => {22 console.log('Appium Server started');23});24const { AppiumServer, AppiumDriver, AppiumBaseDriver } = require('appium');25const { startServer } = require('appium/build/lib/main');26const { AndroidDriver } = require('appium-android-driver');27const { AndroidUiautomator2Driver } = require('appium-uiautomator2-driver');28const server = new AppiumServer();29const driver = new AndroidUiautomator2Driver();30const baseDriver = new AppiumBaseDriver();31const proxyReqRes = async function (driver, proxyReq, proxyRes, req, res) {32 if (req.url.includes('/session/')) {33 if (proxyRes.statusCode === 200) {34 const body = JSON.parse(proxyRes.body.toString('utf8'));35 body.value = body.value.map((el) => {36 el.udid = '123456789';37 return el;38 });39 proxyRes.body = JSON.stringify(body);40 }41 }42};43baseDriver.proxyReqRes = proxyReqRes;44startServer(server, driver
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!!