How to use this.findElOrEls method in Appium Android Driver

Best JavaScript code snippet using appium-android-driver

Run Appium Android Driver automation tests on LambdaTest cloud grid

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

find.js

Source: find.js Github

copy
1import _ from 'lodash';
2import { errors } from 'appium-base-driver';
3import { FakeElement } from '../fake-element';
4
5let commands = {}, helpers = {}, extensions = {};
6
7helpers.getExistingElementForNode = function getExistingElementForNode (node) {
8  for (let [id, el] of _.toPairs(this.elMap)) {
9    if (el.node === node) {
10      return id;
11    }
12  }
13  return null;
14};
15
16helpers.wrapNewEl = function wrapNewEl (obj) {
17  // first check and see if we already have a ref to this element
18  let existingElId = this.getExistingElementForNode(obj);
19  if (existingElId) {
20    return {ELEMENT: existingElId};
21  }
22
23  // otherwise add the element to the map
24  this.maxElId++;
25  this.elMap[this.maxElId.toString()] = new FakeElement(obj, this.appModel);
26  return {ELEMENT: this.maxElId.toString()};
27};
28
29helpers.findElOrEls = async function findElOrEls (strategy, selector, mult, ctx) {
30  let qMap = {
31    'xpath': 'xpathQuery',
32    'id': 'idQuery',
33    'accessibility id': 'idQuery',
34    'class name': 'classQuery',
35    'tag name': 'classQuery'
36  };
37  // TODO this error checking should probably be part of MJSONWP?
38  if (!_.includes(_.keys(qMap), strategy)) {
39    throw new errors.UnknownCommandError();
40  }
41  if (selector === 'badsel') {
42    throw new errors.InvalidSelectorError();
43  }
44  let els = this.appModel[qMap[strategy]](selector, ctx);
45  if (els.length) {
46    if (mult) {
47      let allEls = [];
48      for (let el of els) {
49        allEls.push(this.wrapNewEl(el));
50      }
51      return allEls;
52    } else {
53      return this.wrapNewEl(els[0]);
54    }
55  } else if (mult) {
56    return [];
57  } else {
58    throw new errors.NoSuchElementError();
59  }
60};
61
62commands.findElement = async function findElement (strategy, selector) {
63  return this.findElOrEls(strategy, selector, false);
64};
65
66commands.findElements = async function findElements (strategy, selector) {
67  return this.findElOrEls(strategy, selector, true);
68};
69
70commands.findElementFromElement = async function findElementFromElement (strategy, selector, elementId) {
71  let el = this.getElement(elementId);
72  return this.findElOrEls(strategy, selector, false, el.xmlFragment);
73};
74
75commands.findElementsFromElement = async function findElementsFromElement (strategy, selector, elementId) {
76  let el = this.getElement(elementId);
77  return this.findElOrEls(strategy, selector, true, el.xmlFragment);
78};
79
80Object.assign(extensions, commands, helpers);
81export { commands, helpers};
82export default extensions;
83
Full Screen

element.js

Source: element.js Github

copy
1
2import { errors } from 'appium-base-driver';
3import strategies from './strategies';
4import log from '../logger';
5
6let commands = {};
7let extensions = {};
8
9commands.getAttribute = async function getAttribute (elementId, attribute) {
10  const element = await this.findElOrEls(strategies.ELEMENT_ID, elementId);
11  return element.attributes[attribute];
12};
13
14commands.getName = async function getName (elementId) {
15  const element = await this.findElOrEls(strategies.ELEMENT_ID, elementId);
16  return element.tag;
17};
18
19commands.elementDisplayed = async function elementDisplayed (elementId) {
20  const element = await this.findElOrEls(strategies.ELEMENT_ID, elementId);
21  return element.isDisplayed;
22};
23
24commands.elementEnabled = async function elementEnabled (elementId) {
25  return await this.elementDisplayed(elementId);
26};
27
28commands.elementSelected = async function elementSelected (elementId) {
29  const element = await this.findElOrEls(strategies.ELEMENT_ID, elementId);
30  return element.isFocused;
31};
32
33commands.setValue = async function setValue (keys, elementId) {
34  const element = await this.findElOrEls(strategies.ELEMENT_ID, elementId);
35  await element.append(keys);
36};
37
38commands.replaceValue = async function replaceValue (keys, elementId) {
39  const element = await this.findElOrEls(strategies.ELEMENT_ID, elementId);
40  await element.type(keys);
41};
42
43commands.setValueImmediate = async function setValueImmediate (keys, elementId) {
44  await this.replaceValue(keys, elementId);
45};
46
47commands.getText = async function getText (elementId) {
48  const element = await this.findElOrEls(strategies.ELEMENT_ID, elementId);
49  return element.text;
50};
51
52commands.getSize = async function getSize (elementId) {
53  const element = await this.findElOrEls(strategies.ELEMENT_ID, elementId);
54
55  if (!element.bounds) {
56    return log.errorAndThrow(new errors.ElementNotVisibleError());
57  }
58  return { width: element.bounds.width, height: element.bounds.height };
59};
60
61commands.getLocation = async function getLocation (elementId) {
62  const node = await this.findElOrEls(strategies.ELEMENT_ID, elementId);
63  if (!node) {
64    return log.errorAndThrow(new errors.ElementNotVisibleError());
65  }
66  return { x: node.bounds.x, y: node.bounds.y };
67};
68
69commands.getElementRect = async function getElementRect (elementId) {
70  const element = await this.findElOrEls(strategies.ELEMENT_ID, elementId);
71
72  if (!element.bounds) {
73    return log.errorAndThrow(new errors.ElementNotVisibleError());
74  }
75  return element.bounds;
76};
77
78commands.click = async function click (elementId) {
79  const element = await this.findElOrEls(strategies.ELEMENT_ID, elementId);
80  await element.select(this.ecp);
81};
82
83commands.clear = async function clear (elementId) {
84  await this.replaceValue('', elementId);
85};
86
87commands.active = async function active () {
88  return await this.findElOrEls(strategies.CSS_SELECTOR, '[focused="true"]:not(:has([focused="true"]))');
89};
90
91Object.assign(extensions, commands);
92export {commands};
93export default extensions;
94
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Appium Android Driver on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)