How to use getImgElFromArgs method in Appium Base Driver

Best JavaScript code snippet using appium-base-driver

driver.js

Source:driver.js Github

copy

Full Screen

...274    }275    // If we don't have this command, it must not be implemented276    // If the target element is ImageElement, we must try to call `ImageElement.execute` which exist following lines277    // since ImageElement supports few commands by itself278    const imgElId = getImgElFromArgs(args);279    if (!this[cmd] && !imgElId) {280      throw new errors.NotYetImplementedError();281    }282    let unexpectedShutdownListener;283    const commandExecutor = async () => imgElId284      ? await ImageElement.execute(this, cmd, imgElId, ...args)285      : await B.race([286        this[cmd](...args),287        new B((resolve, reject) => {288          unexpectedShutdownListener = reject;289          this.eventEmitter.on(ON_UNEXPECTED_SHUTDOWN_EVENT, unexpectedShutdownListener);290        })291      ]).finally(() => {292        if (unexpectedShutdownListener) {...

Full Screen

Full Screen

image-element-specs.js

Source:image-element-specs.js Github

copy

Full Screen

...236describe('getImgElFromArgs', function () {237  it('should return the image element id from json obj in args', function () {238    const imgEl = `${IMAGE_ELEMENT_PREFIX}foo`;239    const args = [1, 'foo', imgEl];240    getImgElFromArgs(args).should.eql(imgEl);241  });242  it('should not return anything if image element id not in args', function () {243    const args = [1, 'foo'];244    _.isUndefined(getImgElFromArgs(args)).should.be.true;245  });246  it('should not find image element id in anything but prefix', function () {247    const notImgEl = `foo${IMAGE_ELEMENT_PREFIX}`;248    const args = [1, 'foo', notImgEl];249    _.isUndefined(getImgElFromArgs(args)).should.be.true;250  });...

Full Screen

Full Screen

image-element.js

Source:image-element.js Github

copy

Full Screen

1import _ from 'lodash';2import { errors } from '../..';3import LRU from 'lru-cache';4import { IMAGE_ELEMENT_PREFIX } from '../constants';5import log from './logger';6import { util } from 'appium-support';7const MAX_CACHE_SIZE = 1024 * 1024 * 40; // 40mb8const TAP_DURATION_MS = 125;9const IMAGE_EL_TAP_STRATEGY_W3C = 'w3cActions';10const IMAGE_EL_TAP_STRATEGY_MJSONWP = 'touchActions';11const IMAGE_TAP_STRATEGIES = [12  IMAGE_EL_TAP_STRATEGY_MJSONWP,13  IMAGE_EL_TAP_STRATEGY_W3C14];15const DEFAULT_TEMPLATE_IMAGE_SCALE = 1.0;16/**17 * @typedef {Object} Rect18 * @property {int} x - x-coordinate of top-left corner19 * @property {int} y - y-coordinate of top-left corner20 * @property {int} width - width of rect21 * @property {int} height - height of rect22 */23/**24 * @typedef {Object} Dimension25 * @property {int} width - width of rect26 * @property {int} height - height of rect27 */28/**29 * @typedef {Object} Position30 * @property {int} x - x coordinate31 * @property {int} y - y coordinate32 */33/**34 * Representation of an "image element", which is simply a set of coordinates35 * and methods that can be used on that set of coordinates via the driver36 */37class ImageElement {38  /**39   * @param {string} b64Template - the base64-encoded image which was used to40   *                               find this ImageElement41   * @param {Rect} rect - bounds of matched image element42   * @param {number} score The similarity score as a float number in range [0.0, 1.0].43   * 1.0 is the highest score (means both images are totally equal).44   * @param {?string} b64Result - the base64-encoded image which has matched marks.45   *                              Defaults to null.46   *47   * @returns {ImageElement}48   */49  constructor (b64Template, rect, score, b64Result = null) {50    this.template = b64Template;51    this.rect = rect;52    this.id = `${IMAGE_ELEMENT_PREFIX}${util.uuidV4()}`;53    this.b64MatchedImage = b64Result;54    this.score = score;55  }56  /**57   * @returns {Dimension} - dimension of element58   */59  get size () {60    return {width: this.rect.width, height: this.rect.height};61  }62  /**63   * @returns {Position} - coordinates of top-left corner of element64   */65  get location () {66    return {x: this.rect.x, y: this.rect.y};67  }68  /**69   * @returns {Position} - coordinates of center of element70   */71  get center () {72    return {73      x: this.rect.x + this.rect.width / 2,74      y: this.rect.y + this.rect.height / 2,75    };76  }77  /**78   * @returns {?string} - the base64-encoded image which has matched marks79   */80  get matchedImage () {81    return this.b64MatchedImage;82  }83  /**84   * @param {string} protocolKey - the protocol-specific JSON key for85   * a WebElement86   *87   * @returns {WebElement} - this image element as a WebElement88   */89  asElement (protocolKey) {90    return {[protocolKey]: this.id};91  }92  /**93   * @param {ImageElement} other - an ImageElement to compare with this one94   *95   * @returns {boolean} - whether the other element and this one have the same96   * properties97   */98  equals (other) {99    return this.rect.x === other.rect.x &&100           this.rect.y === other.rect.y &&101           this.rect.width === other.rect.width &&102           this.rect.height === other.rect.height;103  }104  /**105   * Use a driver to tap the screen at the center of this ImageElement's106   * position107   *108   * @param {BaseDriver} driver - driver for calling actions with109   */110  async click (driver) {111    // before we click we need to make sure the element is actually still there112    // where we expect it to be113    let newImgEl;114    const {115      autoUpdateImageElementPosition: updatePos,116      checkForImageElementStaleness,117      imageElementTapStrategy,118    } = driver.settings.getSettings();119    // validate tap strategy120    if (!IMAGE_TAP_STRATEGIES.includes(imageElementTapStrategy)) {121      throw new Error(`Incorrect imageElementTapStrategy setting ` +122                      `'${imageElementTapStrategy}'. Must be one of ` +123                      JSON.stringify(IMAGE_TAP_STRATEGIES));124    }125    if (checkForImageElementStaleness || updatePos) {126      log.info('Checking image element for staleness before clicking');127      try {128        newImgEl = await driver.findByImage(this.template, {129          shouldCheckStaleness: true,130          // Set ignoreDefaultImageTemplateScale because this.template is device screenshot based image131          // managed inside Appium after finidng image by template which managed by a user132          ignoreDefaultImageTemplateScale: true133        });134      } catch (err) {135        throw new errors.StaleElementReferenceError();136      }137      if (!this.equals(newImgEl)) {138        log.warn(`When trying to click on an image element, the image changed ` +139                 `position from where it was originally found. It is now at ` +140                 `${JSON.stringify(newImgEl.rect)} and was originally at ` +141                 `${JSON.stringify(this.rect)}.`);142        if (updatePos) {143          log.warn('Click will proceed at new coordinates');144          this.rect = _.clone(newImgEl.rect);145        } else {146          log.warn('Click will take place at original coordinates. If you ' +147                   'would like Appium to automatically click the new ' +148                   "coordinates, set the 'autoUpdateImageElementPosition' " +149                   'setting to true');150        }151      }152    }153    const {x, y} = this.center;154    log.info(`Will tap on image element at coordinate [${x}, ${y}]`);155    if (imageElementTapStrategy === IMAGE_EL_TAP_STRATEGY_W3C) {156      // set up a W3C action to click on the image by position157      log.info('Will tap using W3C actions');158      const action = {159        type: 'pointer',160        id: 'mouse',161        parameters: {pointerType: 'touch'},162        actions: [163          {type: 'pointerMove', x, y, duration: 0},164          {type: 'pointerDown', button: 0},165          {type: 'pause', duration: TAP_DURATION_MS},166          {type: 'pointerUp', button: 0},167        ]168      };169      // check if the driver has the appropriate performActions method170      if (driver.performActions) {171        return await driver.performActions([action]);172      }173      // if not, warn and fall back to the other method174      log.warn('Driver does not seem to implement W3C actions, falling back ' +175               'to TouchActions');176    }177    // if the w3c strategy was not requested, do the only other option (mjsonwp178    // touch actions)179    log.info('Will tap using MJSONWP TouchActions');180    const action = {181      action: 'tap',182      options: {x, y}183    };184    if (driver.performTouch) {185      return await driver.performTouch([action]);186    }187    throw new Error("Driver did not implement the 'performTouch' command. " +188                    'For drivers to support finding image elements, they ' +189                    "should support 'performTouch' and 'performActions'");190  }191  /**192   * Handle various Appium commands that involve an image element193   *194   * @param {BaseDriver} driver - the driver to use for commands195   * @param {string} cmd - the name of the driver command196   * @param {string} imgElId - the id of the ImageElement to work with197   * @param {Array} args - Rest of arguments for executeScripts198   *199   * @returns {Object} - the result of running a command200   */201  static async execute (driver, cmd, imgElId, ...args) {202    if (!driver._imgElCache.has(imgElId)) {203      throw new errors.NoSuchElementError();204    }205    const imgEl = driver._imgElCache.get(imgElId);206    switch (cmd) {207      case 'click':208        return await imgEl.click(driver);209      case 'elementDisplayed':210        return true;211      case 'getSize':212        return imgEl.size;213      case 'getLocation':214      case 'getLocationInView':215        return imgEl.location;216      case 'getElementRect':217        return imgEl.rect;218      case 'getAttribute':219        // /session/:sessionId/element/:elementId/attribute/:name220        // /session/:sessionId/element/:elementId/attribute/visual should retun the visual data221        // e.g. ["content-desc","appium-image-element-xxxxx","xxxxx"], ["visual","appium-image-element-xxxxx","xxxxx"]222        switch (args[0]) {223          case 'visual':224            return imgEl.matchedImage;225          case 'score':226            return imgEl.score;227          default:228            throw new errors.NotYetImplementedError();229        }230      default: throw new errors.NotYetImplementedError();231    }232  }233}234function makeImageElementCache (max = MAX_CACHE_SIZE) {235  return new LRU({236    max,237    length: (el) => el.template.length,238  });239}240function getImgElFromArgs (args) {241  for (let arg of args) {242    if (_.isString(arg) && arg.startsWith(IMAGE_ELEMENT_PREFIX)) {243      return arg;244    }245  }246}247export {248  ImageElement, getImgElFromArgs, makeImageElementCache,249  IMAGE_EL_TAP_STRATEGY_MJSONWP, IMAGE_EL_TAP_STRATEGY_W3C,250  DEFAULT_TEMPLATE_IMAGE_SCALE...

Full Screen

Full Screen

plugin.js

Source:plugin.js Github

copy

Full Screen

...51  }52  async handle (next, driver, cmdName, ...args) {53    // if we have a command that involves an image element id, attempt to find the image element54    // and execute the command on it55    const imgElId = getImgElFromArgs(args);56    if (imgElId) {57      if (!this.finder.imgElCache.has(imgElId)) {58        throw new errors.NoSuchElementError();59      }60      const imgEl = this.finder.imgElCache.get(imgElId);61      return await ImageElement.execute(driver, imgEl, cmdName, ...args);62    }63    // otherwise just do the normal thing64    return await next();65  }66}...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const BaseDriver = require('appium-base-driver');2const {getImgElFromArgs} = BaseDriver.prototype;3const args = {4};5const imgEl = getImgElFromArgs(args);6console.log(imgEl);7const XCUITestDriver = require('appium-xcuitest-driver');8const {getImgElFromArgs} = XCUITestDriver.prototype;9const args = {10};11const imgEl = getImgElFromArgs(args);12console.log(imgEl);

Full Screen

Using AI Code Generation

copy

Full Screen

1const BaseDriver = require('appium-base-driver');2const args = {3};4const imgEl = BaseDriver.getImgElFromArgs(args);5console.log(imgEl);6{ ELEMENT: 'someId' }7const BaseDriver = require('appium-base-driver');8const args = {9};10const imgEl = BaseDriver.getImgElFromArgs(args);11console.log(imgEl);12{ ELEMENT: 'someId' }13const BaseDriver = require('appium-base-driver');14const args = {15};16const imgEl = BaseDriver.getImgElFromArgs(args);17console.log(imgEl);18{ ELEMENT: 'someId' }19const BaseDriver = require('appium-base-driver');20const args = {21};22const imgEl = BaseDriver.getImgElFromArgs(args);23console.log(imgEl);24{ ELEMENT: 'someId' }25const BaseDriver = require('appium-base-driver');26const args = {27};28const imgEl = BaseDriver.getImgElFromArgs(args);29console.log(imgEl);30{ ELEMENT: 'someId' }31const BaseDriver = require('appium-base-driver');32const args = {33};34const imgEl = BaseDriver.getImgElFromArgs(args);35console.log(imgEl);36{ ELEMENT: 'someId' }37const BaseDriver = require('

Full Screen

Using AI Code Generation

copy

Full Screen

1var AppiumBaseDriver = require('appium-base-driver');2var appiumBaseDriver = new AppiumBaseDriver();3var args = ["imgEl", "imgEl2", "imgEl3"];4var imgEl = appiumBaseDriver.getImgElFromArgs(args);5console.log(imgEl);6{ IMAGE: 'imgEl' }

Full Screen

Using AI Code Generation

copy

Full Screen

1var BaseDriver = require('appium-base-driver');2var args = {3};4var imageEl = BaseDriver.getImgElFromArgs(args);5console.log(imageEl);6var BaseDriver = require('appium-base-driver');7var args = {8};9var imageEl = BaseDriver.getImgElFromArgs(args);10console.log(imageEl);11var BaseDriver = require('appium-base-driver');12var args = {13};14var imageEl = BaseDriver.getImgElFromArgs(args);15console.log(imageEl);16var BaseDriver = require('appium-base-driver');17var args = {18};19var imageEl = BaseDriver.getImgElFromArgs(args);20console.log(imageEl);21var BaseDriver = require('appium-base-driver');22var args = {23};24var imageEl = BaseDriver.getImgElFromArgs(args);25console.log(imageEl);

Full Screen

Using AI Code Generation

copy

Full Screen

1import { BaseDriver } from 'appium-base-driver';2import { getImgElFromArgs, getImgElFromElementId, getImgElFromElement } from 'appium-base-driver/build/lib/image-element';3const driver = new BaseDriver();4const args = {5}6const imgElFromArgs = getImgElFromArgs(args);7const elementId = 'elementId';8const imgElFromElementId = getImgElFromElementId(driver, elementId);9const element = {10}11const imgElFromElement = getImgElFromElement(driver, element);12const elementId = 'elementId';13const imgElFromElementId = getImgElFromElementId(driver, elementId);14const element = {15}16const imgElFromElement = getImgElFromElement(driver, element);17const elementId = 'elementId';18const imgElFromElementId = getImgElFromElementId(driver, elementId);19const element = {20}21const imgElFromElement = getImgElFromElement(driver, element

Full Screen

Using AI Code Generation

copy

Full Screen

1var AppiumBaseDriver = require('appium-base-driver');2var args = {3};4var imgEl = AppiumBaseDriver.prototype.getImgElFromArgs(args);5console.log(imgEl);6var AppiumBaseDriver = require('appium-base-driver');7var args = {8};9var imgEl = AppiumBaseDriver.prototype.getImgElFromArgs(args);10console.log(imgEl);11var AppiumBaseDriver = require('appium-base-driver');12var args = {13};14var imgEl = AppiumBaseDriver.prototype.getImgElFromArgs(args);15console.log(imgEl);16var AppiumBaseDriver = require('appium-base-driver');17var args = {18};19var imgEl = AppiumBaseDriver.prototype.getImgElFromArgs(args);20console.log(imgEl);21var AppiumBaseDriver = require('appium-base-driver');22var args = {23};24var imgEl = AppiumBaseDriver.prototype.getImgElFromArgs(args);

Full Screen

Using AI Code Generation

copy

Full Screen

1const { getImgElFromArgs } = require('appium-base-driver').imageUtils;2const element = getImgElFromArgs(args);3const image = await element.takeScreenshot();4const { getImgElFromArgs } = require('appium-base-driver').imageUtils;5const element = getImgElFromArgs(args);6const image = await element.takeScreenshot();7const { getImgElFromArgs } = require('appium-base-driver').imageUtils;8const element = getImgElFromArgs(args);9const image = await element.takeScreenshot();10const { getImgElFromArgs } = require('appium-base-driver').imageUtils;11const element = getImgElFromArgs(args);12const image = await element.takeScreenshot();13const { getImgElFromArgs } = require('appium-base-driver').imageUtils;14const element = getImgElFromArgs(args);15const image = await element.takeScreenshot();16const { getImgElFromArgs } = require('appium-base-driver').imageUtils;17const element = getImgElFromArgs(args);18const image = await element.takeScreenshot();19const { getImgElFromArgs } = require('

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