2011-02-26 03:32:22 +00:00
|
|
|
Capybara = {
|
|
|
|
nextIndex: 0,
|
|
|
|
nodes: {},
|
|
|
|
|
2011-02-26 03:57:55 +00:00
|
|
|
invoke: function () {
|
|
|
|
return this[CapybaraInvocation.functionName].apply(this, CapybaraInvocation.arguments);
|
|
|
|
},
|
|
|
|
|
2011-02-26 03:32:22 +00:00
|
|
|
find: function (xpath) {
|
2011-02-26 21:46:59 +00:00
|
|
|
return this.findRelativeTo(document, xpath);
|
|
|
|
},
|
|
|
|
|
|
|
|
findWithin: function (index, xpath) {
|
|
|
|
return this.findRelativeTo(this.nodes[index], xpath);
|
|
|
|
},
|
|
|
|
|
|
|
|
findRelativeTo: function (reference, xpath) {
|
|
|
|
var iterator = document.evaluate(xpath, reference, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
|
2011-02-26 03:32:22 +00:00
|
|
|
var node;
|
|
|
|
var results = [];
|
|
|
|
while (node = iterator.iterateNext()) {
|
|
|
|
this.nextIndex++;
|
|
|
|
this.nodes[this.nextIndex] = node;
|
|
|
|
results.push(this.nextIndex);
|
|
|
|
}
|
2011-02-26 03:33:47 +00:00
|
|
|
return results.join(",");
|
2011-02-26 03:32:22 +00:00
|
|
|
},
|
|
|
|
|
2011-02-26 04:39:29 +00:00
|
|
|
text: function (index) {
|
|
|
|
return this.nodes[index].innerText;
|
|
|
|
},
|
|
|
|
|
2011-02-26 03:32:22 +00:00
|
|
|
attribute: function (index, name) {
|
|
|
|
return this.nodes[index].getAttribute(name);
|
2011-02-26 19:55:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
tagName: function(index) {
|
2011-02-26 20:04:34 +00:00
|
|
|
return this.nodes[index].tagName.toLowerCase();
|
2011-02-26 20:18:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
click: function (index) {
|
|
|
|
var clickEvent = document.createEvent('MouseEvents');
|
|
|
|
clickEvent.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
|
|
|
|
this.nodes[index].dispatchEvent(clickEvent);
|
2011-02-26 20:50:45 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
trigger: function (index, eventName) {
|
|
|
|
var eventObject = document.createEvent("HTMLEvents");
|
|
|
|
eventObject.initEvent(eventName, true, true);
|
|
|
|
this.nodes[index].dispatchEvent(eventObject);
|
2011-02-26 21:20:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
visible: function (index) {
|
|
|
|
var element = this.nodes[index];
|
|
|
|
while (element) {
|
2011-03-10 04:50:21 +00:00
|
|
|
if (element.ownerDocument.defaultView.getComputedStyle(element, null).getPropertyValue("display") == 'none')
|
2011-02-26 21:20:05 +00:00
|
|
|
return false;
|
|
|
|
element = element.parentElement;
|
|
|
|
}
|
|
|
|
return true;
|
2011-02-26 20:48:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
value: function(index) {
|
|
|
|
return this.nodes[index].value;
|
2011-02-26 21:26:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
set: function(index, value) {
|
2011-03-10 00:04:41 +00:00
|
|
|
this.trigger(index, "focus");
|
2011-02-26 21:26:22 +00:00
|
|
|
this.nodes[index].value = value;
|
2011-03-10 00:04:41 +00:00
|
|
|
this.trigger(index, "keydown");
|
|
|
|
this.trigger(index, "keyup");
|
|
|
|
this.trigger(index, "change");
|
|
|
|
this.trigger(index, "blur");
|
2011-02-26 22:51:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
selectOption: function(index) {
|
|
|
|
this.nodes[index].setAttribute("selected", "selected");
|
|
|
|
},
|
|
|
|
|
|
|
|
unselectOption: function(index) {
|
|
|
|
this.nodes[index].removeAttribute("selected");
|
2011-02-26 03:32:22 +00:00
|
|
|
}
|
2011-02-26 22:51:47 +00:00
|
|
|
|
2011-02-26 03:32:22 +00:00
|
|
|
};
|
|
|
|
|