More specs for within. Move save_and_open functionality to session.
This commit is contained in:
parent
88a9a4cd61
commit
c64556f489
7
lib/webrat/core/flunk.rb
Normal file
7
lib/webrat/core/flunk.rb
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
module Flunk
|
||||||
|
|
||||||
|
def flunk(message)
|
||||||
|
raise message
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
@ -7,7 +7,8 @@ module Webrat
|
|||||||
class Page
|
class Page
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
include Logging
|
include Logging
|
||||||
|
include Flunk
|
||||||
|
|
||||||
attr_reader :session
|
attr_reader :session
|
||||||
attr_reader :url
|
attr_reader :url
|
||||||
|
|
||||||
@ -27,23 +28,6 @@ module Webrat
|
|||||||
yield Scope.new(self, session.response_body, selector)
|
yield Scope.new(self, session.response_body, selector)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Saves the page out to RAILS_ROOT/tmp/ and opens it in the default
|
|
||||||
# web browser if on OS X. Useful for debugging.
|
|
||||||
#
|
|
||||||
# Example:
|
|
||||||
# save_and_open
|
|
||||||
def save_and_open
|
|
||||||
return unless File.exist?(session.saved_page_dir)
|
|
||||||
|
|
||||||
filename = "#{session.saved_page_dir}/webrat-#{Time.now.to_i}.html"
|
|
||||||
|
|
||||||
File.open(filename, "w") do |f|
|
|
||||||
f.write rewrite_css_and_image_references(session.response_body)
|
|
||||||
end
|
|
||||||
|
|
||||||
open_in_browser(filename)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Reloads the last page requested. Note that this will resubmit forms
|
# Reloads the last page requested. Note that this will resubmit forms
|
||||||
# and their data.
|
# and their data.
|
||||||
#
|
#
|
||||||
@ -82,14 +66,10 @@ module Webrat
|
|||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def open_in_browser(path) # :nodoc
|
|
||||||
`open #{path}`
|
|
||||||
end
|
|
||||||
|
|
||||||
def load_page
|
def load_page
|
||||||
session.request_page(@url, @method, @data)
|
session.request_page(@url, @method, @data)
|
||||||
|
|
||||||
save_and_open if session.exception_caught?
|
save_and_open_page if session.exception_caught?
|
||||||
|
|
||||||
flunk("Page load was not successful (Code: #{session.response_code.inspect})") unless session.success_code?
|
flunk("Page load was not successful (Code: #{session.response_code.inspect})") unless session.success_code?
|
||||||
reset_scope
|
reset_scope
|
||||||
@ -103,14 +83,5 @@ module Webrat
|
|||||||
@scope ||= Scope.new(self, session.response_body)
|
@scope ||= Scope.new(self, session.response_body)
|
||||||
end
|
end
|
||||||
|
|
||||||
def flunk(message)
|
|
||||||
raise message
|
|
||||||
end
|
|
||||||
|
|
||||||
def rewrite_css_and_image_references(response_html) # :nodoc
|
|
||||||
return response_html unless session.doc_root
|
|
||||||
response_html.gsub(/"\/(stylesheets|images)/, session.doc_root + '/\1')
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
@ -1,5 +1,6 @@
|
|||||||
module Webrat
|
module Webrat
|
||||||
class Scope
|
class Scope
|
||||||
|
include Flunk
|
||||||
|
|
||||||
def initialize(page, html, selector = nil)
|
def initialize(page, html, selector = nil)
|
||||||
@page = page
|
@page = page
|
||||||
|
@ -1,5 +1,22 @@
|
|||||||
module Webrat
|
module Webrat
|
||||||
class Session
|
class Session
|
||||||
|
|
||||||
|
# Saves the page out to RAILS_ROOT/tmp/ and opens it in the default
|
||||||
|
# web browser if on OS X. Useful for debugging.
|
||||||
|
#
|
||||||
|
# Example:
|
||||||
|
# save_and_open_page
|
||||||
|
def save_and_open_page
|
||||||
|
return unless File.exist?(saved_page_dir)
|
||||||
|
|
||||||
|
filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.html"
|
||||||
|
|
||||||
|
File.open(filename, "w") do |f|
|
||||||
|
f.write rewrite_css_and_image_references(response_body)
|
||||||
|
end
|
||||||
|
|
||||||
|
open_in_browser(filename)
|
||||||
|
end
|
||||||
|
|
||||||
def doc_root
|
def doc_root
|
||||||
nil
|
nil
|
||||||
@ -40,8 +57,13 @@ module Webrat
|
|||||||
super || current_page.respond_to?(name)
|
super || current_page.respond_to?(name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_and_open_page
|
def open_in_browser(path) # :nodoc
|
||||||
current_page.save_and_open
|
`open #{path}`
|
||||||
|
end
|
||||||
|
|
||||||
|
def rewrite_css_and_image_references(response_html) # :nodoc
|
||||||
|
return response_html unless doc_root
|
||||||
|
response_html.gsub(/"\/(stylesheets|images)/, doc_root + '/\1')
|
||||||
end
|
end
|
||||||
|
|
||||||
def method_missing(name, *args, &block)
|
def method_missing(name, *args, &block)
|
||||||
|
@ -18,7 +18,7 @@ describe "save_and_open_page" do
|
|||||||
|
|
||||||
File.stub!(:exist?).and_return(true)
|
File.stub!(:exist?).and_return(true)
|
||||||
Time.stub!(:now).and_return(1234)
|
Time.stub!(:now).and_return(1234)
|
||||||
@session.current_page.stub!(:open_in_browser)
|
@session.stub!(:open_in_browser)
|
||||||
|
|
||||||
@file_handle = mock("file handle")
|
@file_handle = mock("file handle")
|
||||||
File.stub!(:open).with(filename, 'w').and_yield(@file_handle)
|
File.stub!(:open).with(filename, 'w').and_yield(@file_handle)
|
||||||
@ -42,7 +42,7 @@ describe "save_and_open_page" do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "should open the temp file in a browser" do
|
it "should open the temp file in a browser" do
|
||||||
@session.current_page.should_receive(:open_in_browser).with(filename)
|
@session.should_receive(:open_in_browser).with(filename)
|
||||||
@session.save_and_open_page
|
@session.save_and_open_page
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -7,10 +7,10 @@ describe "within" do
|
|||||||
|
|
||||||
it "should click links within a scope" do
|
it "should click links within a scope" do
|
||||||
@session.response_body = <<-EOS
|
@session.response_body = <<-EOS
|
||||||
<a href="/page1">Link</a>
|
<a href="/page1">Link</a>
|
||||||
<div id="container">
|
<div id="container">
|
||||||
<a href="/page2">Link</a>
|
<a href="/page2">Link</a>
|
||||||
</div>
|
</div>
|
||||||
EOS
|
EOS
|
||||||
|
|
||||||
@session.should_receive(:get).with("/page2", {})
|
@session.should_receive(:get).with("/page2", {})
|
||||||
@ -18,4 +18,39 @@ describe "within" do
|
|||||||
scope.clicks_link "Link"
|
scope.clicks_link "Link"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should submit forms within a scope" do
|
||||||
|
@session.response_body = <<-EOS
|
||||||
|
<form id="form1" action="/form1">
|
||||||
|
<label>Email: <input type="text" name="email" />
|
||||||
|
<input type="submit" value="Add" />
|
||||||
|
</form>
|
||||||
|
<form id="form2" action="/form2">
|
||||||
|
<label>Email: <input type="text" name="email" />
|
||||||
|
<input type="submit" value="Add" />
|
||||||
|
</form>
|
||||||
|
EOS
|
||||||
|
|
||||||
|
@session.should_receive(:get).with("/form2", "email" => "test@example.com")
|
||||||
|
@session.within "#form2" do |scope|
|
||||||
|
scope.fill_in "Email", :with => "test@example.com"
|
||||||
|
scope.clicks_button
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not find buttons outside of the scope" do
|
||||||
|
@session.response_body = <<-EOS
|
||||||
|
<form action="/form1">
|
||||||
|
<input type="submit" value="Add" />
|
||||||
|
</form>
|
||||||
|
<form id="form2" action="/form2">
|
||||||
|
</form>
|
||||||
|
EOS
|
||||||
|
|
||||||
|
@session.within "#form2" do |scope|
|
||||||
|
lambda {
|
||||||
|
scope.clicks_button
|
||||||
|
}.should raise_error
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user