update android xml source to have class names as node names rather than just 'node'

This commit is contained in:
Jonathan Lipps
2014-03-27 22:02:12 -07:00
parent 78f3a43982
commit 36fbeca8c1
2 changed files with 49 additions and 1 deletions

View File

@@ -15,6 +15,7 @@ var errors = require('../../server/errors.js')
, async = require('async')
, mkdirp = require('mkdirp')
, path = require('path')
, XMLDom = require("xmldom")
, helpers = require('../../helpers.js')
, warnDeprecated = helpers.logDeprecationWarning;
@@ -286,6 +287,48 @@ androidController.getCssProperty = function (elementId, propertyName, cb) {
cb(new NotYetImplementedError(), null);
};
var _updateSourceXMLNodeNames = function (source) {
var newSource;
var origDom = new XMLDom.DOMParser().parseFromString(source);
var newDom = new XMLDom.DOMImplementation().createDocument(null);
_buildClassNodeFromPlainNode(newDom, newDom, origDom);
newSource = new XMLDom.XMLSerializer().serializeToString(newDom);
return newSource;
};
var _getNodeClass = function (node) {
var nodeClass = null;
_.each(node.attributes, function (attr) {
if (attr.name === "class") {
nodeClass = attr.value;
}
});
return nodeClass;
};
var _copyNodeAttributes = function (oldNode, newNode) {
_.each(oldNode.attributes, function (attr) {
newNode.setAttribute(attr.name, attr.value);
});
};
var _buildClassNodeFromPlainNode = function (newDom, newParent, oldNode) {
var newNode;
var nodeClass = _getNodeClass(oldNode);
if (nodeClass) {
newNode = newDom.createElement(nodeClass);
_copyNodeAttributes(oldNode, newNode);
} else {
newNode = oldNode.cloneNode(false);
}
newParent.appendChild(newNode);
if (oldNode.hasChildNodes()) {
_.each(oldNode.childNodes, function (childNode) {
_buildClassNodeFromPlainNode(newDom, newNode, childNode);
});
}
};
androidController.getPageSource = function (cb) {
var xmlFile = temp.path({suffix: '.xml'});
var jsonFile = xmlFile + '.json';
@@ -367,6 +410,11 @@ androidController.getPageSourceXML = function (cb) {
function () {
var xml = fs.readFileSync(xmlFile, 'utf8');
fs.unlinkSync(xmlFile);
try {
xml = _updateSourceXMLNodeNames(xml);
} catch (e) {
return cb(e);
}
cb(null, {
status: status.codes.Success.code
, value: xml

View File

@@ -29,7 +29,7 @@ describe("apidemos - source -", function () {
.execute("mobile: source", [{type: 'xml'}]).then(function (source) {
source.should.exist;
var dom = new XMLDom().parseFromString(source);
var nodes = xpath.select('//node[@content-desc="App"]', dom);
var nodes = xpath.select('//android.widget.TextView[@content-desc="App"]', dom);
nodes.length.should.equal(1);
}).nodeify(done);
});