appium-xcuitest-driver
Version:
Appium driver for iOS using XCUITest for backend
125 lines • 4.64 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPageSource = getPageSource;
exports.mobileGetSource = mobileGetSource;
const lodash_1 = __importDefault(require("lodash"));
const js2xmlparser2_1 = __importDefault(require("js2xmlparser2"));
const APPIUM_AUT_TAG = 'AppiumAUT';
/**
* Retrieves the page source of the current application.
*
* @returns The page source as XML or HTML string
*/
async function getPageSource() {
if (this.isWebContext()) {
const script = 'return document.documentElement.outerHTML';
return await this.executeAtom('execute_script', [script, []]);
}
const { pageSourceExcludedAttributes: excludedAttributes, useJSONSource } = await this.settings.getSettings();
const hasExcludedAttributes = lodash_1.default.isString(excludedAttributes) && !lodash_1.default.isEmpty(excludedAttributes);
if (useJSONSource) {
const srcTree = await this.mobileGetSource('json', hasExcludedAttributes ? excludedAttributes : undefined);
return getSourceXml(getTreeForXML(srcTree));
}
return await this.mobileGetSource('xml', hasExcludedAttributes ? excludedAttributes : undefined);
}
/**
* Retrieve the source tree of the current page in XML or JSON format.
*
* @param format - Page tree source representation format.
* @param excludedAttributes - A comma-separated string of attribute names to exclude from the output. Only works if `format` is `xml`.
* @privateRemarks Why isn't `excludedAttributes` an array?
* @returns The source tree of the current page in the given format.
*/
async function mobileGetSource(format = 'xml', excludedAttributes) {
const paramsMap = {
format,
scope: APPIUM_AUT_TAG,
};
if (excludedAttributes) {
paramsMap.excluded_attributes = excludedAttributes;
}
const query = Object.entries(paramsMap)
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join('&');
return await this.proxyCommand(`/source?${query}`, 'GET');
}
/**
* Will get JSON of the form:
*
* ```js
* { isEnabled: '1',
* isVisible: '1',
* isAccessible: '1',
* frame: '{{0, 0}, {375, 667}}',
* children:
* [ { isEnabled: '1',
* isVisible: '1',
* isAccessible: '1',
* frame: '{{0, 0}, {375, 667}}',
* children: [],
* rect: { x: 0, y: 0, width: 375, height: 667 },
* value: null,
* label: null,
* type: 'Other',
* name: null,
* rawIdentifier: null },
* rect: { origin: { x: 0, y: 0 }, size: { width: 375, height: 667 } },
* value: null,
* label: 'UICatalog',
* type: 'Application',
* name: 'UICatalog',
* rawIdentifier: null }
* ```
*/
function getTreeForXML(srcTree) {
function getTree(element, elementIndex, parentPath) {
const curPath = `${parentPath}/${elementIndex}`;
const rect = element.rect || {};
/**
* @privateRemarks I don't even want to try to type this right now
*/
const subtree = {
'@': {
type: `XCUIElementType${element.type}`,
enabled: parseInt(element.isEnabled, 10) === 1,
visible: parseInt(element.isVisible, 10) === 1,
accessible: parseInt(element.isAccessible, 10) === 1,
focused: parseInt(element.isFocused, 10) === 1,
x: rect.x,
y: rect.y,
width: rect.width,
height: rect.height,
},
'>': [],
};
if (element.name !== null) {
subtree['@'].name = element.name;
}
if (element.label !== null) {
subtree['@'].label = element.label;
}
if (element.value !== null) {
subtree['@'].value = element.value;
}
for (let i = 0; i < (element.children || []).length; i++) {
subtree['>'].push(getTree(element.children[i], i, curPath));
}
return {
[`XCUIElementType${element.type}`]: subtree,
};
}
const tree = getTree(srcTree, 0, '');
return tree;
}
function getSourceXml(jsonSource) {
return (0, js2xmlparser2_1.default)('AppiumAUT', jsonSource, {
wrapArray: { enabled: false, elementName: 'element' },
declaration: { include: true },
prettyPrinting: { indentString: ' ' },
});
}
//# sourceMappingURL=source.js.map