Permalink
Browse files

Add Page#load shortcut

  • Loading branch information...
1 parent 7ff06ab commit f2be0841d505a73bd8188ffde6525bb602408b03 @mynyml committed Feb 9, 2010
Showing with 23 additions and 2 deletions.
  1. +2 −2 README.md
  2. +13 −0 lib/harmony/page.rb
  3. +8 −0 test/page_test.rb
View
@@ -67,13 +67,13 @@ based, DOM-javascript tests.
class JavascriptTest < Test::Unit::TestCase
def setup
@page = Harmony::Page.new
- @page.execute_js(File.read('public/javascripts/foo.js'))
+ @page.load('public/javascripts/foo.js')
end
def test_foo
assert_equal "world", @page.execute_js(<<-JS)
foo = new Foo;
- foo.hello;
+ foo.hello();
JS
end
end
View
@@ -1,3 +1,4 @@
+require 'pathname'
require 'tempfile'
require 'johnson/tracemonkey'
@@ -64,6 +65,18 @@ def initialize(document=nil)
@window = Window.from_document(document) if document
end
+ # Load a javascript file in page's context
+ #
+ # @param [String] path
+ # path to js file
+ #
+ # @return [Page] self
+ #
+ def load(path)
+ window.load(path.to_s)
+ self
+ end
+
# Evaluate Javascript code within this page's context.
#
# @param [String] code
View
@@ -57,6 +57,14 @@ class PageTest < MiniTest::Unit::TestCase
assert_equal "<html><head><title></title></head><body></body></html>", Page.new.to_s
end
+ test "loads javascript file" do
+ path = tempfile(<<-HTML)
+ function foo() { return 'bar' };
+ HTML
+ page = Page.new.load(path)
+ assert_equal 'bar', page.x('foo()')
+ end
+
private
def tempfile(content)
Tempfile.open('abc') {|f| f << content; @__path = f.path }

0 comments on commit f2be084

Please sign in to comment.