diff --git a/app/uiauto/appium/app.js b/app/uiauto/appium/app.js index 8d5b4404..5cd980e2 100644 --- a/app/uiauto/appium/app.js +++ b/app/uiauto/appium/app.js @@ -177,14 +177,12 @@ $.extend(au, { return this._returnElems(elems); } - , getElementsByType: function(type, ctx) { - var selector = type; - + , convertSelector: function(selector) { // some legacy: be backwards compatible, mechanic.js - switch (type) { + switch (selector) { case 'tableView': case 'textField': - selector = type.toLowerCase(); + selector = selector.toLowerCase(); break; case 'staticText': selector = 'text'; @@ -196,6 +194,11 @@ $.extend(au, { selector = 'secure'; break; } + return selector; + } + + , getElementsByType: function(type, ctx) { + var selector = this.convertSelector(type); var elems = []; @@ -251,8 +254,9 @@ $.extend(au, { elems = $(_ctx); for (var i = 0; i < xpObj.path.length; i++) { var path = xpObj.path[i]; + path.node = this.convertSelector(path.node); if (path.search === "child") { - elems = elems.children(path.node); + elems = elems.childrenByType(path.node); } else if (path.search === "desc") { elems = elems.find(path.node); } diff --git a/app/uiauto/lib/mechanic.js b/app/uiauto/lib/mechanic.js index c6b5f3f5..7fad53b2 100644 --- a/app/uiauto/lib/mechanic.js +++ b/app/uiauto/lib/mechanic.js @@ -296,6 +296,19 @@ var mechanic = (function() { children: function(selector) { return filtered(this.map(function(){ return slice.call(this.elements()) }), selector); }, + childrenByType: function(type) { + var result = this.map(function() { + var children = this.elements(); + var filtered = []; + for (var i = 0; i < children.length; i++) { + if (children[i].isType(type)) { + filtered.push(children[i]); + } + } + return filtered; + }); + return $(result); + }, siblings: function(selector) { return filtered(this.map(function(i, el) { return slice.call(el.parent().elements()).filter(function(child){ return child!==el }); diff --git a/test/functional/uicatalog/findElement.js b/test/functional/uicatalog/findElement.js index 1af82582..0e1d884d 100644 --- a/test/functional/uicatalog/findElement.js +++ b/test/functional/uicatalog/findElement.js @@ -2,6 +2,7 @@ "use strict"; var describeWd = require("../../helpers/driverblock.js").describeForApp('UICatalog') + , _ = require("underscore") , should = require('should'); describeWd('findElementFromElement', function(h) { @@ -103,20 +104,54 @@ describeWd('findElement(s)ByXpath', function(h) { }); }); }); - it.only('should search an extended path', function(done) { + it('should know how to restrict root-level elements', function(done) { setupXpath(h.driver, function() { - h.driver.source(function(err, s) { - console.log(s); + h.driver.elementByXPath("/button", function(err, el) { + should.exist(err); + err.status.should.equal(7); done(); }); - //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(); - //}); - //}); + }); + }); + it('should search an extended path by child', function(done) { + setupXpath(h.driver, function() { + h.driver.elementsByXPath("navigationBar/text", function(err, els) { + should.not.exist(err); + els[0].text(function(err, text) { + text.should.equal('Buttons'); + done(); + }); + }); + }); + }); + it('should search an extended path by descendant', function(done) { + setupXpath(h.driver, function() { + h.driver.elementsByXPath("cell//button", function(err, els) { + should.not.exist(err); + var texts = []; + _.each(els, function(el) { + el.text(function(err, text) { + texts.push(text); + if (texts.length === els.length) { + var hasNavButton = _.contains(texts, "Button"); + hasNavButton.should.equal(false); + _.contains(texts, "Gray").should.equal(true); + done(); + } + }); + }); + }); + }); + }); + it('should filter by partial text', function(done) { + setupXpath(h.driver, function() { + h.driver.elementByXPath("cell//button[contains(@name, 'Gr')]", function(err, el) { + should.not.exist(err); + el.text(function(err, text) { + text.should.equal("Gray"); + done(); + }); + }); }); }); });