UNPKG

rubick

Version:

tools for mtc

51 lines (46 loc) 1.44 kB
/** * Created by hongzhiyuan on 2017/4/17. */ "use strict" let nameAlias = { 'class':'className', 'resource-id':'resourceId', 'content-desc':'description', 'long-clickable':'longClickable' }; let logger = require('../log').init('rubick'); class DomUtil { static parseNode(node, id) { let result = {}; // logger.info('parse node, index is :' + id); if (node.attributes != null) { for (let index in node.attributes) { let val = node.attributes[index]; let key = val.name; if (key in nameAlias) { key = nameAlias[key]; } if (key === 'bounds') { result[key] = DomUtil.parseBounds(val.value); } else { result[key] = val.value; } } } result.id = id; result.xpath = (result['className'] || '*'); if (result['text']) { result.xpath += "[@text='" + result['text'] + "']"; } return result; } static parseBounds(text) { let bounds = []; let match = text.toString('utf-8').match(/\[(\d+),(\d+)\]\[(\d+),(\d+)\]/); if (match && match.length > 4) { return bounds.concat(match.slice(1)); } return bounds; } } module.exports = DomUtil;