How to use j.proxyReqRes method in Appium Base Driver

Best JavaScript code snippet using appium-base-driver

proxy-specs.js

Source:proxy-specs.js Github

copy

Full Screen

...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  });...

Full Screen

Full Screen

winappdriver.js

Source:winappdriver.js Github

copy

Full Screen

1import events from 'events';2import { JWProxy } from 'appium-base-driver';3import log from './logger';4import { SubProcess } from 'teen_process';5import { WAD_INSTALL_PATH, verifyWAD } from './installer';6//import { retryInterval } from 'asyncbox';7import cp from 'child_process';8import B from 'bluebird';9const REQD_PARAMS = ['app'];10const DEFAULT_HOST = '127.0.0.1';11const DEFAULT_PORT = "4823"; //  should be non-4723 to avoid conflict on the same box12class WinAppDriver extends events.EventEmitter {13  constructor (opts = {}) {14    const {host, port} = opts;15    super();16    for (let req of REQD_PARAMS) {17      if (!opts || !opts[req]) {18        throw new Error(`Option '${req}' is required!`);19      }20      this[req] = opts[req];21    }22    this.proxyHost = host || DEFAULT_HOST;23    this.proxyPort = port || DEFAULT_PORT;24    this.proc = null;25    this.state = WinAppDriver.STATE_STOPPED;26    this.jwproxy = new JWProxy({server: this.proxyHost, port: this.proxyPort});27  }28  async start () {29    if (!await verifyWAD()) {30      throw new Error("Could not verify WinAppDriver install; re-run install");31    }32        33    this.changeState(WinAppDriver.STATE_STARTING);34    35    // XXXYD TODO: would be better if WinAppDriver didn't require passing in /wd/hub as a param36    let args = [this.proxyPort + "/wd/hub"];37    const startDetector = (stdout) => {38      return stdout.indexOf("listening for requests") !== -1;        39    };40    let processIsAlive = false;41    try {42      await this.killAll();43      // set up our subprocess object44      this.proc = new SubProcess(WAD_INSTALL_PATH, args, {45        encoding: 'ucs2'46      });47      processIsAlive = true;48      // handle log output49      for (let stream of ['STDOUT', 'STDERR']) {50        this.proc.on(`lines-${stream.toLowerCase()}`, (lines) => {51          for (let l of lines) {52            log.info(`[${stream}] ${l.trim()}`);53          }54        });55      }56      // handle out-of-bound exit by simply emitting a stopped state57      this.proc.on('exit', (code, signal) => {58        processIsAlive = false;59        if (this.state !== WinAppDriver.STATE_STOPPED &&60            this.state !== WinAppDriver.STATE_STOPPING) {61          let msg = `WinAppDriver exited unexpectedly with code ${code}, ` +62                    `signal ${signal}`;63          log.error(msg);64          this.changeState(WinAppDriver.STATE_STOPPED);65        }66      });67      log.info(`Spawning winappdriver with: ${this.winappdriver} ` +68               `${args.join(' ')}`);69      // start subproc and wait for startDetector70      await this.proc.start(startDetector);71      // XXXYD TODO: bring this back once WinAppDriver supports status correctly72      await this.waitForOnline();73      this.changeState(WinAppDriver.STATE_ONLINE);74    } catch (e) {75      this.emit(WinAppDriver.EVENT_ERROR, e);76      // just because we had an error doesn't mean the winappdriver process77      // finished; we should clean up if necessary78      if (processIsAlive) {79        await this.proc.stop();80      }81      log.errorAndThrow(e);82    }83  }      84  sessionId () {85    if (this.state !== WinAppDriver.STATE_ONLINE) {86      return null;87    }88    return this.jwproxy.sessionId;89  }90  async waitForOnline () {91    // TODO WAD doesn't support the status command correctly, so just return92    // true for now93    return true;94    // we need to make sure WAD hasn't crashed95    /*96    let winappdriverStopped = false;97    await retryInterval(20, 200, async () => {98      if (this.state === WinAppDriver.STATE_STOPPED) {99        // we are either stopped or stopping, so something went wrong100        winappdriverStopped = true;101        return;102      }103      await this.getStatus();104    });105    if (winappdriverStopped) {106      throw new Error('WinAppDriver crashed during startup.');107    }*/108  }109  async getStatus () {110    return await this.jwproxy.command('/status', 'GET');111  }112  async startSession (caps) {113    this.proxyReqRes = this.jwproxy.proxyReqRes.bind(this.jwproxy);114    await this.jwproxy.command('/session', 'POST', {desiredCapabilities: caps});115  }116  async stop (emitStates = true) {117    if (emitStates) {118      this.changeState(WinAppDriver.STATE_STOPPING);119    }120    try {121      if (this.proc) {122        await this.proc.stop();123      }124      if (emitStates) {125        this.changeState(WinAppDriver.STATE_STOPPED);126      }127    } catch (e) {128      log.error(e);129    }130  }131  changeState (state) {132    this.state = state;133    log.debug(`WinAppDriver changed state to '${state}'`);134    this.emit(WinAppDriver.EVENT_CHANGED, {state});135  }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");155    } catch (err) {156      log.info("No old WinAppDrivers seemed to exist");157    }158  }159  async deleteSession () {160    log.debug('Deleting WinAppDriver server session');161    // rely on jwproxy's intelligence to know what we're talking about and162    // delete the current session163    try {164      await this.jwproxy.command('/', 'DELETE');165    } catch (err) {166      log.warn(`Did not get confirmation WinAppDriver deleteSession worked; ` +167        `Error was: ${err}`);168    }169  }170}171WinAppDriver.EVENT_ERROR = 'winappdriver_error';172WinAppDriver.EVENT_CHANGED = 'stateChanged';173WinAppDriver.STATE_STOPPED = 'stopped';174WinAppDriver.STATE_STARTING = 'starting';175WinAppDriver.STATE_ONLINE = 'online';176WinAppDriver.STATE_STOPPING = 'stopping';...

Full Screen

Full Screen

appium-for-mac.js

Source:appium-for-mac.js Github

copy

Full Screen

1import { JWProxy } from 'appium-base-driver';2import log from './logger';3import { SubProcess } from 'teen_process';4import { fs, logger, process } from 'appium-support';5import path from 'path';6const DEFAULT_A4M_HOST = '127.0.0.1';7const DEFAULT_A4M_PORT = 4622;8const REQ_A4M_APP_PATH = '/Applications/AppiumForMac.app';9const A4M_APP_BUNDLE_ID = 'com.appium.AppiumForMac';10const a4mLog = logger.getLogger('Appium4Mac');11class AppiumForMac {12  constructor (opts = {}) {13    this.proxyHost = opts.a4mHost;14    this.proxyPort = opts.a4mPort;15    this.a4mAppPath = opts.a4mAppPath;16    this.killAllA4MAppBeforeStart = opts.killAllA4MAppBeforeStart || true;17    this.proc = null;18    this.jwproxy = new JWProxy({server: this.proxyHost, port: this.proxyPort});19  }20  async start () {21    if (!await fs.exists(this.a4mAppPath)) {22      throw new Error(`Could not verify AppiumForMacDriver install in '${this.a4mAppPath}'; please install it to '${this.a4mAppPath}'`);23    }24    const startDetector = (stdout, stderr) => stderr.includes('Started HTTP server');25    let processIsAlive = false;26    try {27      if (this.killAllA4MAppBeforeStart) {28        await this.killAll();29      }30      // set up our subprocess object31      const a4mBinary = path.resolve(this.a4mAppPath, 'Contents', 'MacOS', 'AppiumForMac');32      this.proc = new SubProcess(a4mBinary, []);33      processIsAlive = true;34      // handle log output35      for (let stream of ['STDOUT', 'STDERR']) {36        this.proc.on(`lines-${stream.toLowerCase()}`, (lines) => {37          for (let l of lines) {38            a4mLog.info(`[${stream}] ${l.trim()}`);39          }40        });41      }42      // handle out-of-bound exit by simply logging43      // TODO add ability for driver to handle this gracefully and maybe44      // restart45      this.proc.on('exit', (code, signal) => {46        processIsAlive = false;47        let msg = `AppiumForMac exited unexpectedly with code ${code}, ` +48                  `signal ${signal}`;49        log.error(msg);50      });51      log.info(`Spawning AppiumForMac with: ${this.a4mAppPath}`);52      // start subproc and wait for startDetector53      await this.proc.start(startDetector);54      await this.waitForOnline();55    } catch (e) {56      this.emit(AppiumForMac.EVENT_ERROR, e);57      // just because we had an error doesn't mean the winappdriver process58      // finished; we should clean up if necessary59      if (processIsAlive) {60        await this.proc.stop();61      }62      log.errorAndThrow(e);63    }64  }65  sessionId () {66    if (this.state !== AppiumForMac.STATE_ONLINE) {67      return null;68    }69    return this.jwproxy.sessionId;70  }71  async waitForOnline () { // eslint-disable-line require-await72    // TODO: Actually check via HTTP73    return true;74  }75  async getStatus () {76    return await this.sendCommand('/status', 'GET');77  }78  async startSession (caps) {79    this.proxyReqRes = this.jwproxy.proxyReqRes.bind(this.jwproxy);80    await this.sendCommand('/session', 'POST', {desiredCapabilities: caps});81  }82  async stop () {83    try {84      if (this.proc) {85        await this.proc.stop();86      }87    } catch (e) {88      log.error(e);89    }90  }91  async sendCommand (url, method, body) {92    let res;93    // need to cover over A4M's bad handling of responses, which sometimes94    // don't have 'value' properties95    try {96      res = await this.jwproxy.command(url, method, body);97    } catch (e) {98      if (e.message.indexOf('Did not get a valid response object') === -1 ||99          e.message.indexOf('value') !== -1) {100        throw e;101      }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');121    } catch (err) {122      log.warn(`Did not get confirmation AppiumForMac deleteSession worked; ` +123        `Error was: ${err}`);124    }125  }126}127export { AppiumForMac, DEFAULT_A4M_HOST, DEFAULT_A4M_PORT,128  A4M_APP_BUNDLE_ID, REQ_A4M_APP_PATH };...

Full Screen

Full Screen

selendroid.js

Source:selendroid.js Github

copy

Full Screen

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}...

Full Screen

Full Screen

proxy.js

Source:proxy.js Github

copy

Full Screen

1"use strict";2var _s = require('underscore.string')3  , logger = require('./logger.js').get('appium')4  , status = require('./status.js')5  , doRequest = require('../devices/common.js').doRequest6  , respondError = require('./responses.js').respondError7  , _ = require('underscore')8  , safely = require('./helpers').safely;9module.exports.shouldProxy = function (req) {10  if (req.device === null) return false;11  if (!req.device.isProxy) return false;12  var deviceAvoids = req.device.avoidProxy || [];13  var avoid = [14    ['POST', new RegExp('^/wd/hub/session$')]15    , ['DELETE', new RegExp('^/wd/hub/session/[^/]+$')]16  ].concat(deviceAvoids);17  var method = req.method.toUpperCase();18  var path = req.originalUrl;19  var shouldAvoid = false;20  // check for POST /execute mobile:21  if (method === 'POST' &&22      new RegExp('^/wd/hub/session/.+/execute$').test(path) &&23      _s.startsWith(req.body.script, "mobile: ")) {24    shouldAvoid = true;25  }26  _.each(avoid, function (pathToAvoid) {27    if (method === pathToAvoid[0] && pathToAvoid[1].exec(path)) {28      shouldAvoid = true;29    }30  });31  return !shouldAvoid;32};33module.exports.doProxy = function (req, res) {34  if (req.device.proxyReqRes) {35    // this section is triggered when we have defined the proxy device to use36    // the appium-jsonwp-proxy method proxyReqRes. Ultimately we'll be moving37    // everything to this paradigm and will delete the code after this block.38    // proxyReqRes might be a promise or a callback-based function, so handle39    // 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 :58                                             origSessId;59    newPath = req.originalUrl.replace(origSessId, sessId);60  } else {61    newPath = req.originalUrl;62  }63  var url = 'http://' + req.device.proxyHost + ':' + req.device.proxyPort +64            newPath;65  doRequest(url, req.method.toUpperCase(), req.body,66          req.headers['content-type'], function (err, response, body) {67    if (typeof body === "string") {68      try {69        body = JSON.parse(body);70      } catch (e) {}71    }72    if (err) {73      return respondError(req, res, status.codes.UnknownError.code,74        "Did not successfully proxy server command");75    }76    if (body && body.value) {77      var resStatusCode = body.status;78      if (resStatusCode !== 0) {79        var resErrorMessage = body.value.message;80        return respondError(req, res, resStatusCode, resErrorMessage);81      }82    }83    var sbody = body ? JSON.stringify(body).slice(0, 10000) : body;84    logger.debug("Proxied response received with status " +85                 response.statusCode + ": " +86                 sbody);87    // if the proxied response contains a sessionId that the downstream88    // driver has generated, we don't want to return that to the client.89    // Instead, return the id for the current appium session90    if (body && body.sessionId && origSessId) {91      body.sessionId = origSessId;92    }93    safely(req, function () {94      res.headers = response.headers;95      res.set('Content-Type', response.headers['content-type']);96      res.status(response.statusCode).send(body);97    });98  });...

Full Screen

Full Screen

appium-for-awtk.js

Source:appium-for-awtk.js Github

copy

Full Screen

1import { JWProxy } from 'appium-base-driver';2import log from './logger';3import { SubProcess } from 'teen_process';4import { fs, logger, process } from 'appium-support';5import path from 'path';6const DEFAULT_A4A_HOST = '127.0.0.1';7const DEFAULT_A4A_PORT = 4622;8const REQ_A4A_APP_PATH = '/Applications/AppiumForAwtk.app';9const A4A_APP_BUNDLE_ID = 'com.appium.AppiumForAwtk';10const a4aLog = logger.getLogger('Appium4Awtk');11class AppiumForAwtk {12  constructor (opts = {}) {13    this.proxyHost = opts.a4aHost;14    this.proxyPort = opts.a4aPort;15    this.a4aAppPath = opts.a4aAppPath;16    this.killAllA4AAppBeforeStart = opts.killAllA4AAppBeforeStart || true;17    this.proc = null;18    this.jwproxy = new JWProxy({server: this.proxyHost, port: this.proxyPort});19  }20  async start () {21    console.log('start')22  }23  sessionId () {24    if (this.state !== AppiumForAwtk.STATE_ONLINE) {25      return null;26    }27    return this.jwproxy.sessionId;28  }29  async waitForOnline () { // eslint-disable-line require-await30    // TODO: Actually check via HTTP31    return true;32  }33  async getStatus () {34    return await this.sendCommand('/status', 'GET');35  }36  async startSession (caps) {37    this.proxyReqRes = this.jwproxy.proxyReqRes.bind(this.jwproxy);38    await this.sendCommand('/session', 'POST', {desiredCapabilities: caps});39  }40  async stop () {41    try {42      if (this.proc) {43        await this.proc.stop();44      }45    } catch (e) {46      log.error(e);47    }48  }49  async sendCommand (url, method, body) {50    let res;51    // need to cover over A4A's bad handling of responses, which sometimes52    // don't have 'value' properties53    try {54      res = await this.jwproxy.command(url, method, body);55    } catch (e) {56      if (e.message.indexOf('Did not get a valid response object') === -1 ||57          e.message.indexOf('value') !== -1) {58        throw e;59      }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');79    } catch (err) {80      log.warn(`Did not get confirmation AppiumForAwtk deleteSession worked; ` +81        `Error was: ${err}`);82    }83  }84}85export { AppiumForAwtk, DEFAULT_A4A_HOST, DEFAULT_A4A_PORT,86  A4A_APP_BUNDLE_ID, REQ_A4A_APP_PATH };...

Full Screen

Full Screen

webdriveragent.js

Source:webdriveragent.js Github

copy

Full Screen

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}...

Full Screen

Full Screen

driver.js

Source:driver.js Github

copy

Full Screen

1import _ from 'lodash';2import { BaseDriver } from '@appium/base-driver';3import SafariDriverServer from './safari';4import { desiredCapConstraints } from './desired-caps';5import commands from './commands/index';6import { formatCapsForServer } from './utils';7const NO_PROXY = [8  ['GET', new RegExp('^/session/[^/]+/appium')],9  ['POST', new RegExp('^/session/[^/]+/appium')],10  ['POST', new RegExp('^/session/[^/]+/element/[^/]+/elements?$')],11  ['POST', new RegExp('^/session/[^/]+/elements?$')],12  ['DELETE', new RegExp('^/session/[^/]+/cookie$')],13];14class SafariDriver extends BaseDriver {15  constructor (opts = {}) {16    super(opts);17    this.desiredCapConstraints = desiredCapConstraints;18    this.locatorStrategies = [19      'xpath',20      'tag name',21      'link text',22      'partial link text',23      'css selector',24      // Let these two reach Safari Driver and fail there with a proper error message25      'id',26      'name',27    ];28    this.resetState();29    for (const [cmd, fn] of _.toPairs(commands)) {30      SafariDriver.prototype[cmd] = fn;31    }32  }33  resetState () {34    this.safari = null;35    this.proxyReqRes = null;36    this.isProxyActive = false;37    this._screenRecorder = null;38  }39  proxyActive () {40    return this.isProxyActive;41  }42  getProxyAvoidList () {43    return NO_PROXY;44  }45  canProxy () {46    return true;47  }48  async createSession (...args) {49    const [sessionId, caps] = await super.createSession(...args);50    this.safari = new SafariDriverServer(this.log);51    try {52      await this.safari.start(formatCapsForServer(caps));53    } catch (e) {54      await this.deleteSession();55      throw e;56    }57    this.proxyReqRes = this.safari.proxy.proxyReqRes.bind(this.safari.proxy);58    this.isProxyActive = true;59    return [sessionId, caps];60  }61  async deleteSession () {62    this.log.info('Ending Safari session');63    await this._screenRecorder?.stop(true);64    await this.safari?.stop();65    this.resetState();66    await super.deleteSession();67  }68}...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var webdriverio = require('webdriverio');2var options = {3    desiredCapabilities: {4    }5};6var client = webdriverio.remote(options);7    .init()8    .getTitle().then(function(title) {9        console.log('Title was: ' + title);10    })11    .end();12var webdriverio = require('webdriverio');13var options = {14    desiredCapabilities: {15    }16};17var client = webdriverio.remote(options);18    .init()19    .getTitle().then(function(title) {20        console.log('Title was: ' + title);21    })22    .end();23var webdriverio = require('webdriverio');24var options = {25    desiredCapabilities: {26    }27};28var client = webdriverio.remote(options);29    .init()30    .getTitle().then(function(title) {31        console.log('Title was: ' + title);32    })33    .end();

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd');2var assert = require('assert');3var path = require('path');4var serverConfig = {5};6var desired = {7  app: path.resolve(__dirname, 'UICatalog.app.zip'),8};9var driver = wd.promiseChainRemote(serverConfig);10  .init(desired)11  .context('WEBVIEW_1')12  .then(function() {13    return driver.proxyReqRes({14      payload: {15        script: 'return document.readyState;',16      }17    });18  })19  .then(function(res) {20    console.log(res);21  })22  .fin(function() { return driver.quit(); })23  .done();24info: --> POST /wd/hub/session/1234567890/execute {"script":"return document.readyState;","args":[]}25info: [debug] Pushing command to appium work queue: "au.mainApp().getTreeForXML()"26info: [debug] Sending command to instruments: au.mainApp().getTreeForXML()27info: [debug] [INST] 2015-05-26 14:23:18 +0000 Debug: Got new command 2 from instruments: au.mainApp().getTreeForXML()28info: [debug] [INST] 2015-05-26 14:23:18 +0000 Debug: evaluating au.mainApp().getTreeForXML()

Full Screen

Using AI Code Generation

copy

Full Screen

1const request = require('request');2const chai = require('chai');3const chaiAsPromised = require('chai-as-promised');4const chaiSubset = require('chai-subset');5const path = require('path');6const B = require('bluebird');7const _ = require('lodash');8const { withRetries } = require('asyncbox');9const { fs, tempDir, zip, mkdirp } = require('appium-support');10const { withMocks } = require('appium-test-support');11const { MOCHA_TIMEOUT } = require('./helpers');12const { util, system } = require('appium-support');13const { AppiumDriver, errors } = require('..');14chai.should();15chai.use(chaiAsPromised);16chai.use(chaiSubset);17describe('proxyReqRes', function () {18  this.timeout(MOCHA_TIMEOUT);19  let driver;20  let proxySpy;21  let proxySpyRes;22  let proxySpyReq;23  let proxySpyReqBody;24  let proxySpyResBody;25  let proxySpyResHeaders;26  let proxySpyResStatusCode;27  let proxySpyResStatusMessage;28  let proxySpyResTime;29  let proxySpyResHAR;30  before(async function () {31    proxySpy = withMocks({request}, (mocks) => {32      mocks.request.expects('get')33        .callsFake((args) => {34          proxySpyReq = args;35          return new B((resolve, reject) => {36            request.get(args, (err, res, body) => {37              if (err) {38                return reject(err);39              }40              proxySpyRes = res;41              proxySpyResHeaders = res.headers;42              proxySpyResStatusCode = res.statusCode;43              proxySpyResStatusMessage = res.statusMessage;44              proxySpyResTime = res.elapsedTime;45              proxySpyResHAR = res.har;46              proxySpyResBody = body;47              resolve(res);48            });49          });50        });51      mocks.request.expects('post')52        .callsFake((args) => {53          proxySpyReq = args;54          proxySpyReqBody = args.body;55          return new B((resolve, reject) => {56            request.post(args, (err, res, body) => {57              if (err) {58                return reject(err);59              }60              proxySpyRes = res;61              proxySpyResHeaders = res.headers;

Full Screen

Using AI Code Generation

copy

Full Screen

1var request = require('request');2var appium = require('appium');3var driver = new appium.AppiumDriver();4var proxyReqRes = driver.proxyReqRes.bind(driver);5var options = {6};7proxyReqRes(options, function (err, response) {8    if (err) {9        console.log(err);10    } else {11        console.log(response.statusCode);12        console.log(response.body);13    }14});

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Appium Base Driver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful