@appium/universal-xml-plugin 1.0.0-beta.8 → 1.0.0-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/plugin.js CHANGED
@@ -1,49 +1,59 @@
1
1
  /* eslint-disable no-case-declarations */
2
2
 
3
- import BasePlugin from '@appium/base-plugin';
4
- import { errors } from '@appium/base-driver';
5
- import { transformSourceXml } from './source';
6
- import { transformQuery } from './xpath';
3
+ import BasePlugin from 'appium/plugin';
4
+ import {errors} from 'appium/driver';
5
+ import {transformSourceXml} from './source';
6
+ import {transformQuery} from './xpath';
7
7
  import log from './logger';
8
8
 
9
9
  export default class UniversalXMLPlugin extends BasePlugin {
10
+ commands = [
11
+ 'getPageSource',
12
+ 'findElement',
13
+ 'findElements',
14
+ 'findElementFromElement',
15
+ 'findElementsFromElement',
16
+ ];
10
17
 
11
- commands = ['getPageSource', 'findElement', 'findElements', 'findElementFromElement',
12
- 'findElementsFromElement'];
13
-
14
- async getPageSource (next, driver, sessId, addIndexPath = false) {
18
+ async getPageSource(next, driver, sessId, addIndexPath = false) {
15
19
  const source = next ? await next() : await driver.getPageSource();
16
20
  const metadata = {};
17
21
  const {platformName} = driver.caps;
18
22
  if (platformName.toLowerCase() === 'android') {
19
23
  metadata.appPackage = driver.opts.appPackage;
20
24
  }
21
- const {xml, unknowns} = transformSourceXml(source,
22
- platformName.toLowerCase(), {metadata, addIndexPath});
25
+ const {xml, unknowns} = transformSourceXml(source, platformName.toLowerCase(), {
26
+ metadata,
27
+ addIndexPath,
28
+ });
23
29
  if (unknowns.nodes.length) {
24
- log.warn(`The XML mapper found ${unknowns.nodes.length} node(s) / ` +
25
- `tag name(s) that it didn't know about. These should be ` +
26
- `reported to improve the quality of the plugin: ` +
27
- unknowns.nodes.join(', '));
30
+ log.warn(
31
+ `The XML mapper found ${unknowns.nodes.length} node(s) / ` +
32
+ `tag name(s) that it didn't know about. These should be ` +
33
+ `reported to improve the quality of the plugin: ` +
34
+ unknowns.nodes.join(', ')
35
+ );
28
36
  }
29
37
  if (unknowns.attrs.length) {
30
- log.warn(`The XML mapper found ${unknowns.attrs.length} attributes ` +
31
- `that it didn't know about. These should be reported to ` +
32
- `improve the quality of the plugin: ` +
33
- unknowns.attrs.join(', '));
38
+ log.warn(
39
+ `The XML mapper found ${unknowns.attrs.length} attributes ` +
40
+ `that it didn't know about. These should be reported to ` +
41
+ `improve the quality of the plugin: ` +
42
+ unknowns.attrs.join(', ')
43
+ );
34
44
  }
35
45
  return xml;
36
46
  }
37
47
 
38
- async findElement (...args) {
48
+ async findElement(...args) {
39
49
  return await this._find(false, ...args);
40
50
  }
41
51
 
42
- async findElements (...args) {
52
+ async findElements(...args) {
43
53
  return await this._find(true, ...args);
44
54
  }
45
55
 
46
- async _find (multiple, next, driver, strategy, selector) {
56
+ async _find(multiple, next, driver, strategy, selector) {
47
57
  const {platformName} = driver.caps;
48
58
  if (strategy.toLowerCase() !== 'xpath') {
49
59
  return await next();
@@ -54,8 +64,10 @@ export default class UniversalXMLPlugin extends BasePlugin {
54
64
  // if the selector was not able to be transformed, that means no elements were found that
55
65
  // matched, so do the appropriate thing based on element vs elements
56
66
  if (newSelector === null) {
57
- log.warn(`Selector was not able to be translated to underlying XML. Either the requested ` +
58
- `element does not exist or there was an error in translation`);
67
+ log.warn(
68
+ `Selector was not able to be translated to underlying XML. Either the requested ` +
69
+ `element does not exist or there was an error in translation`
70
+ );
59
71
  if (multiple) {
60
72
  return [];
61
73
  }
@@ -74,7 +86,6 @@ export default class UniversalXMLPlugin extends BasePlugin {
74
86
  const finder = multiple ? 'findElements' : 'findElement';
75
87
  return await driver[finder](strategy, newSelector);
76
88
  }
77
-
78
89
  }
79
90
 
80
- export { UniversalXMLPlugin };
91
+ export {UniversalXMLPlugin};
package/lib/source.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import _ from 'lodash';
2
- import parser, { j2xParser } from 'fast-xml-parser';
2
+ import parser, {j2xParser} from 'fast-xml-parser';
3
3
  import NODE_MAP from './node-map';
4
- import { ATTR_MAP, REMOVE_ATTRS } from './attr-map';
4
+ import {ATTR_MAP, REMOVE_ATTRS} from './attr-map';
5
5
  import TRANSFORMS from './transformers';
6
6
 
7
7
  const PARSE_OPTS = {
@@ -22,19 +22,23 @@ export const IDX_PREFIX = `${ATTR_PREFIX}index`;
22
22
  const isAttr = (k) => k.substring(0, 2) === ATTR_PREFIX;
23
23
  const isNode = (k) => !isAttr(k);
24
24
 
25
- export function transformSourceXml (xmlStr, platform, {metadata = {}, addIndexPath = false} = {}) {
25
+ export function transformSourceXml(xmlStr, platform, {metadata = {}, addIndexPath = false} = {}) {
26
26
  // first thing we want to do is modify the ios source root node, because it doesn't include the
27
27
  // necessary index attribute, so we add it if it's not there
28
28
  xmlStr = xmlStr.replace('<AppiumAUT>', '<AppiumAUT index="0">');
29
29
  const xmlObj = parser.parse(xmlStr, PARSE_OPTS);
30
- const unknowns = transformNode(xmlObj, platform, {metadata, addIndexPath, parentPath: ''});
30
+ const unknowns = transformNode(xmlObj, platform, {
31
+ metadata,
32
+ addIndexPath,
33
+ parentPath: '',
34
+ });
31
35
  const jParser = new j2xParser(GEN_OPTS);
32
36
  let transformedXml = jParser.parse(xmlObj).trim();
33
37
  transformedXml = `<?xml version="1.0" encoding="UTF-8"?>\n${transformedXml}`;
34
38
  return {xml: transformedXml, unknowns};
35
39
  }
36
40
 
37
- function getUniversalName (nameMap, name, platform) {
41
+ function getUniversalName(nameMap, name, platform) {
38
42
  for (const translatedName of Object.keys(nameMap)) {
39
43
  const sourceNodes = nameMap[translatedName]?.[platform];
40
44
  if (_.isArray(sourceNodes) && sourceNodes.includes(name)) {
@@ -47,15 +51,15 @@ function getUniversalName (nameMap, name, platform) {
47
51
  return null;
48
52
  }
49
53
 
50
- export function getUniversalNodeName (nodeName, platform) {
54
+ export function getUniversalNodeName(nodeName, platform) {
51
55
  return getUniversalName(NODE_MAP, nodeName, platform);
52
56
  }
53
57
 
54
- export function getUniversalAttrName (attrName, platform) {
58
+ export function getUniversalAttrName(attrName, platform) {
55
59
  return getUniversalName(ATTR_MAP, attrName, platform);
56
60
  }
57
61
 
58
- export function transformNode (nodeObj, platform, {metadata, addIndexPath, parentPath}) {
62
+ export function transformNode(nodeObj, platform, {metadata, addIndexPath, parentPath}) {
59
63
  const unknownNodes = [];
60
64
  const unknownAttrs = [];
61
65
  if (_.isPlainObject(nodeObj)) {
@@ -76,14 +80,18 @@ export function transformNode (nodeObj, platform, {metadata, addIndexPath, paren
76
80
  TRANSFORMS[platform]?.(nodeObj, metadata);
77
81
  unknownAttrs.push(...transformAttrs(nodeObj, attrs, platform));
78
82
  const unknowns = transformChildNodes(nodeObj, childNodeNames, platform, {
79
- metadata, addIndexPath, parentPath: thisIndexPath
83
+ metadata,
84
+ addIndexPath,
85
+ parentPath: thisIndexPath,
80
86
  });
81
87
  unknownAttrs.push(...unknowns.attrs);
82
88
  unknownNodes.push(...unknowns.nodes);
83
89
  } else if (_.isArray(nodeObj)) {
84
90
  for (const childObj of nodeObj) {
85
91
  const {nodes, attrs} = transformNode(childObj, platform, {
86
- metadata, addIndexPath, parentPath
92
+ metadata,
93
+ addIndexPath,
94
+ parentPath,
87
95
  });
88
96
  unknownNodes.push(...nodes);
89
97
  unknownAttrs.push(...attrs);
@@ -95,12 +103,21 @@ export function transformNode (nodeObj, platform, {metadata, addIndexPath, paren
95
103
  };
96
104
  }
97
105
 
98
- export function transformChildNodes (nodeObj, childNodeNames, platform, {metadata, addIndexPath, parentPath}) {
106
+ export function transformChildNodes(
107
+ nodeObj,
108
+ childNodeNames,
109
+ platform,
110
+ {metadata, addIndexPath, parentPath}
111
+ ) {
99
112
  const unknownNodes = [];
100
113
  const unknownAttrs = [];
101
114
  for (const nodeName of childNodeNames) {
102
115
  // before modifying the name of this child node, recurse down and modify the subtree
103
- const {nodes, attrs} = transformNode(nodeObj[nodeName], platform, {metadata, addIndexPath, parentPath});
116
+ const {nodes, attrs} = transformNode(nodeObj[nodeName], platform, {
117
+ metadata,
118
+ addIndexPath,
119
+ parentPath,
120
+ });
104
121
  unknownNodes.push(...nodes);
105
122
  unknownAttrs.push(...attrs);
106
123
 
@@ -127,7 +144,7 @@ export function transformChildNodes (nodeObj, childNodeNames, platform, {metadat
127
144
  return {nodes: unknownNodes, attrs: unknownAttrs};
128
145
  }
129
146
 
130
- export function transformAttrs (nodeObj, attrs, platform) {
147
+ export function transformAttrs(nodeObj, attrs, platform) {
131
148
  const unknownAttrs = [];
132
149
  for (const attr of attrs) {
133
150
  const cleanAttr = attr.substring(2);
@@ -1,10 +1,10 @@
1
- import { ATTR_PREFIX } from './source';
1
+ import {ATTR_PREFIX} from './source';
2
2
 
3
- function ios (nodeObj/*, metadata*/) {
3
+ function ios(nodeObj /*, metadata*/) {
4
4
  return nodeObj;
5
5
  }
6
6
 
7
- function android (nodeObj, metadata) {
7
+ function android(nodeObj, metadata) {
8
8
  // strip android:id from front of id
9
9
  const resId = nodeObj[`${ATTR_PREFIX}resource-id`];
10
10
  if (resId && metadata.appPackage) {
package/lib/xpath.js CHANGED
@@ -1,13 +1,13 @@
1
- import { select as xpathQuery } from 'xpath';
2
- import { DOMParser } from 'xmldom';
1
+ import {select as xpathQuery} from 'xpath';
2
+ import {DOMParser} from 'xmldom';
3
3
 
4
- export function runQuery (query, xmlStr) {
4
+ export function runQuery(query, xmlStr) {
5
5
  const dom = new DOMParser().parseFromString(xmlStr);
6
6
  const nodes = xpathQuery(query, dom);
7
7
  return nodes;
8
8
  }
9
9
 
10
- export function transformQuery (query, xmlStr, multiple) {
10
+ export function transformQuery(query, xmlStr, multiple) {
11
11
  const nodes = runQuery(query, xmlStr);
12
12
 
13
13
  const newQueries = nodes.map((node) => {
@@ -16,7 +16,8 @@ export function transformQuery (query, xmlStr, multiple) {
16
16
  let newQuery = indexPath
17
17
  .substring(1) // remove leading / so we can split
18
18
  .split('/') // split into idnexes
19
- .map((indexStr) => { // map to xpath node indexes (1-based)
19
+ .map((indexStr) => {
20
+ // map to xpath node indexes (1-based)
20
21
  const xpathIndex = parseInt(indexStr, 10) + 1;
21
22
  return `*[${xpathIndex}]`;
22
23
  })
@@ -37,7 +38,7 @@ export function transformQuery (query, xmlStr, multiple) {
37
38
  return newSelector;
38
39
  }
39
40
 
40
- export function getNodeAttrVal (node, attr) {
41
+ export function getNodeAttrVal(node, attr) {
41
42
  const attrObjs = Object.values(node.attributes).filter((obj) => obj.name === attr);
42
43
  if (!attrObjs.length) {
43
44
  throw new Error(`Tried to retrieve a node attribute '${attr}' but the node didn't have it`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@appium/universal-xml-plugin",
3
- "version": "1.0.0-beta.8",
3
+ "version": "1.0.0-beta.9",
4
4
  "description": "Appium plugin for making XML source and XPath queries the same across iOS and Android",
5
5
  "keywords": [
6
6
  "appium",
@@ -8,16 +8,17 @@
8
8
  "xml",
9
9
  "webdriver"
10
10
  ],
11
- "homepage": "https://github.com/appium/appium-plugins#readme",
11
+ "homepage": "https://appium.io",
12
12
  "bugs": {
13
- "url": "https://github.com/appium/appium-plugins/issues"
13
+ "url": "https://github.com/appium/appium/issues"
14
14
  },
15
15
  "repository": {
16
16
  "type": "git",
17
17
  "url": "git+https://github.com/appium/appium-plugins.git"
18
18
  },
19
19
  "license": "Apache-2.0",
20
- "author": "Appium <maintainers@appium.io>",
20
+ "author": "https://github.com/appium",
21
+ "types": "./build/lib/plugin.d.ts",
21
22
  "files": [
22
23
  "index.js",
23
24
  "build",
@@ -28,20 +29,25 @@
28
29
  "dev": "npm run build -- --watch",
29
30
  "fix": "npm run lint -- --fix",
30
31
  "lint": "eslint -c ../../.eslintrc --ignore-path ../../.eslintignore .",
32
+ "prepare": "npm run build",
31
33
  "test": "npm run test:unit",
32
34
  "test:unit": "mocha \"./test/unit/**/*.spec.js\""
33
35
  },
34
36
  "dependencies": {
35
- "@appium/base-driver": "^8.5.3",
36
- "@appium/base-plugin": "^1.8.5",
37
37
  "fast-xml-parser": "3.21.1",
38
38
  "xmldom": "0.6.0",
39
39
  "xpath": "0.0.32"
40
40
  },
41
+ "peerDependencies": {
42
+ "appium": "^2.0.0-beta.35"
43
+ },
41
44
  "appium": {
42
45
  "pluginName": "universal-xml",
43
46
  "mainClass": "UniversalXMLPlugin"
44
47
  },
45
- "types": "./build/lib/plugin.d.ts",
46
- "gitHead": "5482c921bc187ed8807922c00bd2355585f4971a"
48
+ "gitHead": "5c7af8ee73078018e4ec52fccf19fe3f77249d72",
49
+ "engines": {
50
+ "node": ">=14",
51
+ "npm": ">=6"
52
+ }
47
53
  }