Created
April 1, 2010 19:56
-
-
Save jferris/352282 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module CucumberJavascript | |
| MOCK_XHR = %{ | |
| <script type="text/javascript"> | |
| var originalXMLHttpRequest = new XMLHttpRequest(); | |
| XMLHttpRequest = function() { | |
| this.xml = originalXMLHttpRequest; | |
| return this; | |
| }; | |
| XMLHttpRequest.prototype.getResponseHeader = function(key) { | |
| if(key == "content-type") { return "text/html; charset=utf-8"; } | |
| }; | |
| XMLHttpRequest.prototype.open = function(method, url, async, username, password) { | |
| this.info = { method: method, url: url }; | |
| }; | |
| XMLHttpRequest.prototype.send = function(data) { | |
| this.responseText = Ruby.HolyGrail.XhrProxy.request(this.info, data); | |
| this.status = 200; | |
| this.readyState = 4; | |
| this.onreadystatechange(); | |
| } | |
| </script> | |
| } | |
| MOCK_FB = %{ | |
| <script type="text/javascript"> | |
| function FB_RequireFeatures() { } | |
| </script> | |
| } | |
| MOCK_LOG = %{ | |
| <script type="text/javascript"> | |
| console = { | |
| log: function (text) { | |
| Ruby.Rails.logger().debug('*** Javascript: ' + text + ' ***'); | |
| } | |
| }; | |
| </script> | |
| } | |
| MOCK_SET_TIMEOUT = %{ | |
| <script type="text/javascript"> | |
| setTimeout = function() { | |
| arguments[0].call(); | |
| }; | |
| </script> | |
| } | |
| MOCK_JAVASCRIPT = (MOCK_XHR + MOCK_FB + MOCK_SET_TIMEOUT + MOCK_LOG).freeze | |
| def cucumber_js(code) | |
| HolyGrail::XhrProxy.context = self | |
| current_page_body = rewrite_script_paths(current_dom.to_html) | |
| current_page_body.sub!("<head>", "<head>\n#{MOCK_JAVASCRIPT}") | |
| current_page_body.gsub!(%r{<script src="http://[^"]+" type="text/javascript"></script>}, '') | |
| @__page ||= Harmony::Page.new(current_page_body) | |
| @__page.execute_js(code) | |
| page_without_xhr_mock_script = @__page.to_html.sub(MOCK_JAVASCRIPT, '') | |
| @response.body = page_without_xhr_mock_script | |
| class << @response | |
| attr_accessor :dom | |
| end | |
| @response.dom = Nokogiri::HTML.parse(@response.body) | |
| webrat_session.current_scope.instance_variable_set(:@dom, @response.dom) | |
| current_dom | |
| end | |
| def rewrite_script_paths(body) | |
| body.gsub(%r%src=("|')/?javascripts/([^'"]*)("|')%) { %|src=#{$1}%s#{$1}"| % Rails.root.join("public/javascripts/#{$2}") } | |
| end | |
| end | |
| World(CucumberJavascript) | |
| module HolyGrail | |
| module XhrProxy | |
| def request(info, data="") | |
| context.instance_eval do | |
| xhr(info["method"].downcase, info["url"], data) | |
| @response.body.to_s | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment