Class: Grope::Env
- Inherits:
-
Object
- Object
- Grope::Env
- Defined in:
- lib/grope/env.rb
Instance Method Summary collapse
- #all(xpath, node = nil) ⇒ Object
- #capture(elem = nil, filename = "capture.png") ⇒ Object
- #document ⇒ Object
- #eval(js) ⇒ Object
- #find(xpath, node = nil) ⇒ Object
-
#initialize(options = {}) ⇒ Env
constructor
A new instance of Env.
- #load(url) ⇒ Object
- #wait(sec = 0) ⇒ Object
- #window ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Env
Returns a new instance of Env.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/grope/env.rb', line 5 def initialize( = {}) @options = { :timeout => 60, :use_shared_cookie => false, :init_width => 1024, :init_height => 600 }.merge() NSApplication.sharedApplication @webview = WebView.alloc @webview.initWithFrame(rect) @webview.setPreferencesIdentifier('Grope') @webview.preferences.setShouldPrintBackgrounds(true) @webview.preferences.setAllowsAnimatedImages(false) @webview.mainFrame.frameView.setAllowsScrolling(false) @webview.setMediaStyle('screen') create_window @frame_load_delegate = FrameLoadDelegate.alloc.init @webview.setFrameLoadDelegate(@frame_load_delegate) unless @options[:use_shared_cookie] @resource_load_delegate = WebResourceLoadDelegate.alloc.init @resource_load_delegate. = Mechanize::CookieJar.new @webview.setResourceLoadDelegate(@resource_load_delegate) end end |
Instance Method Details
#all(xpath, node = nil) ⇒ Object
132 133 134 135 |
# File 'lib/grope/env.rb', line 132 def all(xpath, node = nil) node ||= document js.xpath(xpath, node) end |
#capture(elem = nil, filename = "capture.png") ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/grope/env.rb', line 141 def capture(elem = nil, filename = "capture.png") view = @webview.mainFrame.frameView.documentView bounds = view.bounds if elem position = js.getElementPosition(elem) raise "element's width is 0" if position.width.zero? raise "element's height is 0" if position.height.zero? bounds.origin.x = position.left bounds.origin.y = position.top bounds.size.width = position.width bounds.size.height = position.height end wait view.display view.window.setContentSize(NSUnionRect(view.bounds, bounds).size) view.setFrame(NSUnionRect(view.bounds, bounds)) view.lockFocus bitmapdata = NSBitmapImageRep.alloc bitmapdata.initWithFocusedViewRect(bounds) view.unlockFocus bitmapdata.representationUsingType_properties(NSPNGFileType, nil). writeToFile_atomically(filename.to_s, 1) end |
#document ⇒ Object
124 125 126 |
# File 'lib/grope/env.rb', line 124 def document eval('return document;') end |
#eval(js) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/grope/env.rb', line 43 def eval(js) value = nil run do wso = @webview.windowScriptObject value = WSOWrapper.wrap(wso.evaluateWebScript(<<JS % js)) (function() { var Grope = { click: function(e) { this._dispatchMouseEvent(e, 'click') }, mouseover: function(e) { this._dispatchMouseEvent(e, 'mouseover') }, mouseout: function(e) { this._dispatchMouseEvent(e, 'mouseout') }, mousedown: function(e) { this._dispatchMouseEvent(e, 'mousedown') }, mouseup: function(e) { this._dispatchMouseEvent(e, 'mouseup') }, xpath: function(exp, context, type /* want type */) { if (typeof context == "function") { type = context; context = null; } if (!context) context = document; exp = (context.ownerDocument || context).createExpression(exp, function (prefix) { var o = document.createNSResolver(context)(prefix); if (o) return o; return (document.contentType == "application/xhtml+xml") ? "http://www.w3.org/1999/xhtml" : ""; }); switch (type) { case String: return exp.evaluate(context, XPathResult.STRING_TYPE, null).stringValue; case Number: return exp.evaluate(context, XPathResult.NUMBER_TYPE, null).numberValue; case Boolean: return exp.evaluate(context, XPathResult.BOOLEAN_TYPE, null).booleanValue; case Array: var result = exp.evaluate(context, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); for (var ret = [], i = 0, len = result.snapshotLength; i < len; i++) { ret.push(result.snapshotItem(i)); } return ret; case undefined: var result = exp.evaluate(context, XPathResult.ANY_TYPE, null); switch (result.resultType) { case XPathResult.STRING_TYPE : return result.stringValue; case XPathResult.NUMBER_TYPE : return result.numberValue; case XPathResult.BOOLEAN_TYPE: return result.booleanValue; case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: // not ensure the order. var ret = [], i = null; while ((i = result.iterateNext())) ret.push(i); return ret; } return null; default: throw(TypeError("$X: specified type is not valid type.")); } }, getElementPosition: function(elem) { var position = elem.getBoundingClientRect(); return { left: Math.round(window.scrollX+position.left), top: Math.round(window.scrollY+position.top), width: elem.clientWidth, height: elem.clientHeight } }, _dispatchMouseEvent: function(e, type, dst) { var evt = document.createEvent('MouseEvents'); dst = dst || e; var pos = dst.getBoundingClientRect(); evt.initMouseEvent(type, true, true, window, 0, 0, 0, Math.round(pos.left), Math.round(pos.top), false, false, false, false, 0, null); e.dispatchEvent(evt); } }; %s })() JS end wait value end |
#find(xpath, node = nil) ⇒ Object
137 138 139 |
# File 'lib/grope/env.rb', line 137 def find(xpath, node = nil) all(xpath, node)[0] end |
#load(url) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/grope/env.rb', line 34 def load(url) run do @webview.setMainFrameURL(url) if !@webview.mainFrame.provisionalDataSource raise " ... not a proper url?" end end end |
#wait(sec = 0) ⇒ Object
120 121 122 |
# File 'lib/grope/env.rb', line 120 def wait(sec = 0) run(sec) do; end end |
#window ⇒ Object
128 129 130 |
# File 'lib/grope/env.rb', line 128 def window eval('return window;') end |