Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implement the find command and start implementing Node methods.

Also includes webkit-server changes.
  • Loading branch information...
commit 6d66d5e0b92ce41ca0659aa554137fd30eb232bb 1 parent ac93240
Tristan Dunn authored
13 lib/browser.js
... ... @@ -1,4 +1,5 @@
1 1 var ChildProcess = require("child_process"),
  2 + Node = require("./node"),
2 3 Parser = require("./parser"),
3 4 Path = require("path"),
4 5 Socket = require("net").Socket;
@@ -34,6 +35,18 @@ Browser.prototype.execute = function(code, callback) {
34 35 this.command("Execute", code, callback);
35 36 };
36 37
  38 +Browser.prototype.find = function(selector, callback) {
  39 + this.command("Find", selector, function(ids) {
  40 + if (ids.length > 0) {
  41 + var nodes = ids.split(',').map(function(id) {
  42 + return new Node(id, this);
  43 + }.bind(this));
  44 + }
  45 +
  46 + callback(nodes || []);
  47 + }.bind(this));
  48 +};
  49 +
37 50 Browser.prototype.headers = function(callback) {
38 51 this.command("Headers", callback);
39 52 };
25 lib/node.js
... ... @@ -0,0 +1,25 @@
  1 +var Node = module.exports = function(id, browser) {
  2 + this.id = id;
  3 + this.browser = browser;
  4 +};
  5 +
  6 +Node.prototype.get = function(name, callback) {
  7 + this.invoke("attribute", name, function(value) {
  8 + if (name == "checked" || name == "disabled") {
  9 + value = (value == "true");
  10 + }
  11 +
  12 + callback(value);
  13 + });
  14 +};
  15 +
  16 +Node.prototype.invoke = function() {
  17 + var options = Array.prototype.slice.call(arguments),
  18 + name = options.shift();
  19 +
  20 + options.unshift(this.id);
  21 + options.unshift(name);
  22 + options.unshift("Node");
  23 +
  24 + this.browser.command.apply(this.browser, options);
  25 +};
150 vendor/webkit-server/src/webkit_server.js
@@ -20,7 +20,7 @@ window.WebKitServer = {
20 20 elements = parent.querySelectorAll(selector);
21 21
22 22 for (var index = 0, length = elements.length; index < length; index++) {
23   - this.nodes[results.push(this.nextIndex++)] = elements[index];
  23 + this.nodes[results.push(++this.nextIndex)] = elements[index];
24 24 }
25 25
26 26 return results.join(",");
@@ -31,8 +31,9 @@ window.WebKitServer = {
31 31 },
32 32
33 33 text: function(index) {
34   - var node = this.nodes[index];
35   - var type = (node.type || node.tagName).toLowerCase();
  34 + var node = this.nodes[index],
  35 + type = (node.type || node.tagName).toLowerCase();
  36 +
36 37 if (type == "textarea") {
37 38 return node.innerHTML;
38 39 } else {
@@ -42,16 +43,9 @@ window.WebKitServer = {
42 43
43 44 attribute: function(index, name) {
44 45 switch(name) {
45   - case 'checked':
46   - return this.nodes[index].checked;
47   - break;
48   -
49   - case 'disabled':
50   - return this.nodes[index].disabled;
51   - break;
52   -
53   - default:
54   - return this.nodes[index].getAttribute(name);
  46 + case "checked": return this.nodes[index].checked;
  47 + case "disabled": return this.nodes[index].disabled;
  48 + default: return this.nodes[index].getAttribute(name);
55 49 }
56 50 },
57 51
@@ -60,8 +54,8 @@ window.WebKitServer = {
60 54 },
61 55
62 56 click: function(index) {
63   - var clickEvent = document.createEvent('MouseEvents');
64   - clickEvent.initMouseEvent('click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  57 + var clickEvent = document.createEvent("MouseEvents");
  58 + clickEvent.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
65 59 this.nodes[index].dispatchEvent(clickEvent);
66 60 },
67 61
@@ -73,7 +67,7 @@ window.WebKitServer = {
73 67
74 68 keypress: function(index, altKey, ctrlKey, shiftKey, metaKey, keyCode, charCode) {
75 69 var eventObject = document.createEvent("Events");
76   - eventObject.initEvent('keypress', true, true);
  70 + eventObject.initEvent("keypress", true, true);
77 71 eventObject.window = window;
78 72 eventObject.altKey = altKey;
79 73 eventObject.ctrlKey = ctrlKey;
@@ -86,11 +80,15 @@ window.WebKitServer = {
86 80
87 81 visible: function(index) {
88 82 var element = this.nodes[index];
  83 +
89 84 while (element) {
90   - if (element.ownerDocument.defaultView.getComputedStyle(element, null).getPropertyValue("display") == 'none')
  85 + if (element.ownerDocument.defaultView.getComputedStyle(element, null).getPropertyValue("display") == "none") {
91 86 return false;
  87 + }
  88 +
92 89 element = element.parentElement;
93 90 }
  91 +
94 92 return true;
95 93 },
96 94
@@ -103,29 +101,34 @@ window.WebKitServer = {
103 101 },
104 102
105 103 set: function(index, value) {
106   - var node = this.nodes[index];
107   - var type = (node.type || node.tagName).toLowerCase();
  104 + var node = this.nodes[index],
  105 + type = (node.type || node.tagName).toLowerCase();
  106 +
108 107 if (type == "text" || type == "textarea" || type == "password") {
109 108 this.trigger(index, "focus");
  109 +
110 110 node.value = "";
111   - var maxLength = this.attribute(index, "maxlength"),
112   - length;
  111 +
  112 + var length = value.length,
  113 + maxLength = this.attribute(index, "maxlength");
  114 +
113 115 if (maxLength && value.length > maxLength) {
114 116 length = maxLength;
115   - } else {
116   - length = value.length;
117 117 }
118 118
119   - for(var strindex = 0; strindex < length; strindex++) {
120   - node.value += value[strindex];
  119 + for (var offset = 0; offset < length; offset++) {
  120 + node.value += value[offset];
  121 +
121 122 this.trigger(index, "keydown");
122   - this.keypress(index, false, false, false, false, 0, value[strindex]);
  123 + this.keypress(index, false, false, false, false, 0, value[offset]);
123 124 this.trigger(index, "keyup");
124 125 }
  126 +
125 127 this.trigger(index, "change");
126 128 this.trigger(index, "blur");
127 129 } else if(type == "checkbox" || type == "radio") {
128 130 node.checked = (value == "true");
  131 +
129 132 this.trigger(index, "click");
130 133 this.trigger(index, "change");
131 134 } else if(type == "file") {
@@ -137,28 +140,35 @@ window.WebKitServer = {
137 140 },
138 141
139 142 selectOption: function(index) {
140   - this.nodes[index].selected = true;
141   - this.nodes[index].setAttribute("selected", "selected");
  143 + var node = this.nodes[index];
  144 +
  145 + node.selected = true;
  146 + node.setAttribute("selected", "selected");
142 147 this.trigger(index, "change");
143 148 },
144 149
145 150 unselectOption: function(index) {
146   - this.nodes[index].selected = false;
147   - this.nodes[index].removeAttribute("selected");
  151 + var node = this.nodes[index];
  152 +
  153 + node.selected = false;
  154 + node.removeAttribute("selected");
148 155 this.trigger(index, "change");
149 156 },
150 157
151 158 centerPostion: function(element) {
152 159 this.reflow(element);
153   - var rect = element.getBoundingClientRect();
154   - var position = {
155   - x: rect.width / 2,
156   - y: rect.height / 2
157   - };
  160 +
  161 + var rect = element.getBoundingClientRect(),
  162 + position = {
  163 + x: rect.width / 2,
  164 + y: rect.height / 2
  165 + };
  166 +
158 167 do {
159   - position.x += element.offsetLeft;
160   - position.y += element.offsetTop;
  168 + position.x += element.offsetLeft;
  169 + position.y += element.offsetTop;
161 170 } while ((element = element.offsetParent));
  171 +
162 172 position.x = Math.floor(position.x), position.y = Math.floor(position.y);
163 173
164 174 return position;
@@ -166,38 +176,56 @@ window.WebKitServer = {
166 176
167 177 reflow: function(element, force) {
168 178 if (force || element.offsetWidth === 0) {
169   - var prop, oldStyle = {}, newStyle = {position: "absolute", visibility : "hidden", display: "block" };
170   - for (prop in newStyle) {
171   - oldStyle[prop] = element.style[prop];
172   - element.style[prop] = newStyle[prop];
  179 + var property,
  180 + oldStyle = {},
  181 + newStyle = {
  182 + display : "block",
  183 + position : "absolute",
  184 + visibility : "hidden",
  185 + };
  186 +
  187 + for (property in newStyle) {
  188 + oldStyle[property] = element.style[property];
  189 + element.style[property] = newStyle[property];
  190 + }
  191 +
  192 + element.offsetWidth;
  193 + element.offsetHeight;
  194 +
  195 + for (property in oldStyle) {
  196 + element.style[property] = oldStyle[property];
173 197 }
174   - element.offsetWidth, element.offsetHeight; // force reflow
175   - for (prop in oldStyle)
176   - element.style[prop] = oldStyle[prop];
177 198 }
178 199 },
179 200
180 201 dragTo: function(index, targetIndex) {
181   - var element = this.nodes[index], target = this.nodes[targetIndex];
182   - var position = this.centerPostion(element);
183   - var options = {
  202 + var element = this.nodes[index],
  203 + target = this.nodes[targetIndex],
  204 + position = this.centerPostion(element),
  205 + options = {
  206 + clientX: position.x,
  207 + clientY: position.y
  208 + },
  209 + mouseTrigger = function(eventName, options) {
  210 + var eventObject = document.createEvent("MouseEvents");
  211 + eventObject.initMouseEvent(eventName, true, true, window, 0, 0, 0, options.clientX || 0, options.clientY || 0, false, false, false, false, 0, null);
  212 + element.dispatchEvent(eventObject);
  213 + };
  214 +
  215 + mouseTrigger("mousedown", options);
  216 +
  217 + options.clientX += 1;
  218 + options.clientY += 1;
  219 +
  220 + mouseTrigger("mousemove", options);
  221 +
  222 + position = this.centerPostion(target),
  223 + options = {
184 224 clientX: position.x,
185 225 clientY: position.y
186 226 };
187   - var mouseTrigger = function(eventName, options) {
188   - var eventObject = document.createEvent("MouseEvents");
189   - eventObject.initMouseEvent(eventName, true, true, window, 0, 0, 0, options.clientX || 0, options.clientY || 0, false, false, false, false, 0, null);
190   - element.dispatchEvent(eventObject);
191   - }
192   - mouseTrigger('mousedown', options);
193   - options.clientX += 1, options.clientY += 1;
194   - mouseTrigger('mousemove', options);
195 227
196   - position = this.centerPostion(target), options = {
197   - clientX: position.x,
198   - clientY: position.y
199   - };
200   - mouseTrigger('mousemove', options);
201   - mouseTrigger('mouseup', options);
  228 + mouseTrigger("mousemove", options);
  229 + mouseTrigger("mouseup", options);
202 230 }
203 231 };

0 comments on commit 6d66d5e

Please sign in to comment.
Something went wrong with that request. Please try again.