findElement + tests, yay!

This commit is contained in:
Santiago Suarez Ordoñez
2013-01-16 15:14:47 -08:00
parent 2acad1a476
commit ae50ba3817
5 changed files with 89 additions and 37 deletions
+15 -2
View File
@@ -61,9 +61,22 @@ exports.deleteSession = function(req, res) {
exports.findElements = function(req, res) {
var strategy = req.body.using
, value = req.body.value;
, selector = req.body.value;
req.device.findElements(value, function(err, result) {
req.device.findElements(strategy, selector, function(err, result) {
res.send({
sessionId: req.appium.sessionId
, status: 0
, value: result
});
});
};
exports.findElement = function(req, res) {
var strategy = req.body.using
, selector = req.body.value;
req.device.findElement(strategy, selector, function(err, result) {
res.send({
sessionId: req.appium.sessionId
, status: 0
+14 -13
View File
@@ -117,24 +117,25 @@ IOS.prototype.push = function(elem) {
next();
};
IOS.prototype.findElements = function(selector, cb) {
var me = this;
var findElement = function(value, ctx, many, cb) {
var ext = many ? 's' : '';
IOS.prototype.findElementOrElements = function(selector, many, cb) {
var ext = many ? 's' : ''
, ctx = 'wd_frame'; // Does this value ever need to change?
var command = [ctx, ".findElement", ext, "AndSetKey", ext, "('", value, "')"].join("");
var command = [ctx, ".findElement", ext, "AndSetKey", ext, "('", selector, "')"].join("");
me.proxy(command, function(json) {
json = many ? json : json[0];
cb(null, json);
});
};
findElement(selector, 'wd_frame', true, function(err, res) {
cb(null, res);
this.proxy(command, function(json) {
cb(null, json);
});
};
IOS.prototype.findElement = function(strategy, selector, cb) {
this.findElementOrElements(selector, false, cb);
};
IOS.prototype.findElements = function(strategy, selector, cb) {
this.findElementOrElements(selector, true, cb);
};
IOS.prototype.setValue = function(elementId, value, cb) {
var command = ["elements['", elementId, "'].setValue('", value, "')"].join('');
+1 -1
View File
@@ -17,6 +17,7 @@ module.exports = function(appium) {
rest.get('/wd/hub/session/:sessionId?', controller.getSession);
rest.delete('/wd/hub/session/:sessionId?', controller.deleteSession);
rest.get('/wd/hub/sessions', controller.getSessions);
rest.post('/wd/hub/session/:sessionId?/element', controller.findElement);
rest.post('/wd/hub/session/:sessionId?/elements', controller.findElements);
rest.post('/wd/hub/session/:sessionId?/element/:elementId?/value', controller.setValue);
rest.post('/wd/hub/session/:sessionId?/element/:elementId?/click', controller.doClick);
@@ -63,7 +64,6 @@ module.exports = function(appium) {
// rest.delete('/wd/hub/session/:sessionId/cookie/:name --> Delete the cookie with the given name.
// rest.get('/wd/hub/session/:sessionId/source --> Get the current page source.
// rest.get('/wd/hub/session/:sessionId/title --> Get the current page title.
// rest.post('/wd/hub/session/:sessionId/element --> Search for an element on the page, starting from the document root.
// rest.post('/wd/hub/session/:sessionId/element/active --> Get the element on the page that currently has focus.
// rest.get('/wd/hub/session/:sessionId/element/:id --> Describe the identified element.
// rest.post('/wd/hub/session/:sessionId/element/:id/element --> Search for an element on the page, starting from the identified element.
+20 -21
View File
@@ -106,16 +106,16 @@ UIAElement.prototype.findElements = function(by) {
if (child.hasChildren()) // big optimization
findElements(child, tagName, text);
}
}
findElements(this, tagName, text)
};
findElements(this, tagName, text);
return foundElements;
}
};
// @param by "type[/text]"
UIAElement.prototype.findElement = function(by) {
var tagName;
var text;
var sep = by.indexOf('/');
var tagName
, text
, sep = by.indexOf('/');
if (sep != -1) {
tagName = by.substring(0, sep);
text = by.substring(sep + 1);
@@ -128,22 +128,21 @@ UIAElement.prototype.findElement = function(by) {
var findElement = function(element, tagName, text) {
var children = element.elements();
var numChildren = children.length;
for ( var i = 0; i < numChildren; i++) {
var found;
for (var i = 0; i < numChildren; i++) {
var child = children[i];
if (child.matchesBy(tagName, text)) {
foundElement = child;
return;
return child;
}
if (child.hasChildren()) { // big optimization
findElement(child, tagName, text);
if (foundElement)
return;
else if (child.hasChildren()) { // big optimization
found = findElement(child, tagName, text);
if (found)
return found;
}
}
}
findElement(this, tagName, text)
return foundElement;
}
};
return findElement(this, tagName, text);
};
var elements = new Array();
var globalElementCounter = 0;
@@ -157,11 +156,11 @@ UIAElement.prototype.findElementsAndSetKeys = function(by) {
elements[varName] = foundElements[i];
if (i > 0)
json += ',';
json += '{"ELEMENT":' + '"' + varName + '"' + '}';
json += ['{"ELEMENT":"',varName,'"}'].join('');
}
json += ']';
return json;
}
};
// @return var_namne
UIAElement.prototype.findElementAndSetKey = function(by) {
@@ -169,10 +168,10 @@ UIAElement.prototype.findElementAndSetKey = function(by) {
if (foundElement) {
var varName = 'wde' + globalElementCounter++;
elements[varName] = foundElement;
return varName;
return ['{"ELEMENT":"',varName,'"}'].join('');
}
return '';
}
};
// getPageSource
+39
View File
@@ -0,0 +1,39 @@
/*global describe:true, it:true */
"use strict";
var wd = require('wd')
, assert = require('assert')
, caps = {
browserName: 'iOS'
, platform: 'Mac'
, version: '6.0'
};
describe('check findElement', function() {
var driver = wd.remote('127.0.0.1', 4723);
return it('should find a single element on the app', function(done) {
driver.init(caps, function(err, sessionId) {
driver.elementByTagName('button', function(err, element) {
assert.ok(element.value);
driver.quit(function() {
done();
});
});
});
});
});
describe('check findElements', function() {
var driver = wd.remote('127.0.0.1', 4723);
return it('should find both elements on the app', function(done) {
driver.init(caps, function(err, sessionId) {
driver.elementsByTagName('button', function(err, elements) {
assert.equal(elements.length, 2);
assert.ok(elements[0].value);
driver.quit(function() {
done();
});
});
});
});
});