mirror of
https://github.com/appium/appium.git
synced 2026-02-06 09:38:53 -06:00
update android xml source to have class names as node names rather than just 'node'
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user