initial searching by xpath logic

This commit is contained in:
Jonathan Lipps 2013-02-15 15:44:30 -08:00
Родитель cef7cea3dd
Коммит ed6fb344a5
5 изменённых файлов: 104 добавлений и 11 удалений

Просмотреть файл

@ -143,11 +143,14 @@ exports.stopTimeWarp = function() {
inTimeWarp = false;
};
exports.escapeSpecialChars = function(str) {
exports.escapeSpecialChars = function(str, quoteEscape) {
if (typeof str !== "string") {
return str;
}
return str
if (typeof quoteEscape === "undefined") {
quoteEscape = false;
}
str = str
.replace(/[\\]/g, '\\\\')
.replace(/[\/]/g, '\\/')
.replace(/[\b]/g, '\\b')
@ -157,4 +160,9 @@ exports.escapeSpecialChars = function(str) {
.replace(/[\t]/g, '\\t')
.replace(/[\"]/g, '\\"')
.replace(/\\'/g, "\\'");
if (quoteEscape) {
var re = new RegExp(quoteEscape, "g");
str = str.replace(re, "\\" + quoteEscape);
}
return str;
};

Просмотреть файл

@ -290,7 +290,7 @@ IOS.prototype.push = function(elem) {
};
IOS.prototype.findUIElementOrElements = function(strategy, selector, ctx, many, cb) {
if (_.contains(["name", "tag name"], strategy)) {
if (_.contains(["name", "tag name", "xpath"], strategy)) {
var ext = many ? 's' : '';
if (typeof ctx === "undefined" || !ctx) {
ctx = '';
@ -301,6 +301,9 @@ IOS.prototype.findUIElementOrElements = function(strategy, selector, ctx, many,
var command = "";
if (strategy === "name") {
command = ["au.getElement", ext, "ByName('", selector, "'", ctx,")"].join('');
} else if (strategy === "xpath") {
selector = escapeSpecialChars(selector, "'");
command = ["au.getElement", ext, "ByXpath('", selector, "'", ctx, ")"].join('');
} else {
command = ["au.getElement", ext, "ByType('", selector, "'", ctx,")"].join('');
}

Просмотреть файл

@ -156,7 +156,7 @@ $.extend(au, {
elems.each(function(e, el) {
var elid = me.getId(el);
results.push({ 'ELEMENT': elid });
results.push({ELEMENT: elid});
});
return {
@ -240,16 +240,32 @@ $.extend(au, {
_ctx = ctx;
}
var parsedXpath = this.parseXpath(xpath);
if (parsedXpath === false) {
var xpObj = this.parseXpath(xpath);
if (xpObj === false) {
return {
status: codes.XPathLookupError.code
, value: null
};
} else {
if (parsedXpath.absolute) {
_ctx = this.mainWindow; // overwrite context if we want '//'
this.target.pushTimeout(0);
elems = $(_ctx);
for (var i = 0; i < xpObj.path.length; i++) {
var path = xpObj.path[i];
if (path.search === "child") {
elems = elems.children(path.node);
} else if (path.search === "desc") {
elems = elems.find(path.node);
}
if (i === xpObj.path.length - 1 && xpObj.attr) {
// last run, need to apply attr filters if there are any
if (xpObj.substr) {
elems = elems.valueInKey(xpObj.attr, xpObj.constraint);
} else {
elems = elems.valueForKey(xpObj.attr, xpObj.constraint);
}
}
}
this.target.popTimeout();
return this._returnElems(elems);
}
}

Просмотреть файл

@ -243,11 +243,35 @@ var mechanic = (function() {
return $(result);
},
predicate: function(predicate) {
return this.map(function(el, idx) {
if (typeof predicate == 'string') return el.withPredicate(predicate);
else return null; // TODO: handle map with key/values to match using withValueForKey
this.map(function(idx, el) {
if (typeof predicate == 'string') {
return el.withPredicate(predicate).toArray();
} else {
}
});
},
valueForKey: function(key, value) {
var result = this.map(function(idx, el) {
if (key in el) {
if (el[key]() == value) {
return el;
}
}
return null;
});
return $(result);
},
valueInKey: function(key, val) {
var result = this.map(function(idx, el) {
if (key in el) {
if (el[key]().indexOf(val) !== -1) {
return el;
}
}
return null;
});
return $(result);
},
closest: function(selector, context) {
var el = this[0], candidates = $$(context || app, selector);
if (!candidates.length) el = null;

Просмотреть файл

@ -62,3 +62,45 @@ describeWd('findElementsByTagName', function(h) {
});
});
});
var setupXpath = function(d, cb) {
d.elementsByTagName('tableCell', function(err, els) {
els[0].click(cb);
});
};
describeWd('findElement(s)ByXpath', function(h) {
it('should return a single element', function(done) {
setupXpath(h.driver, function() {
h.driver.elementByXPath("//button", function(err, el) {
should.not.exist(err);
el.text(function(err, text) {
should.not.exist(err);
text.should.equal("Back");
done();
});
});
});
});
it('should return multiple elements', function(done) {
setupXpath(h.driver, function() {
h.driver.elementsByXPath("//button", function(err, els) {
should.not.exist(err);
els.length.should.be.above(5);
done();
});
});
});
it.only('should filter by name', function(done) {
setupXpath(h.driver, function() {
h.driver.elementByXPath("button[@name='Rounded']", function(err, el) {
should.not.exist(err);
el.text(function(err, text) {
should.not.exist(err);
text.should.equal("Rounded");
done();
});
});
});
});
});