More specs for within. Move save_and_open functionality to session.
This commit is contained in:
parent
88a9a4cd61
commit
c64556f489
|
@ -0,0 +1,7 @@
|
|||
module Flunk
|
||||
|
||||
def flunk(message)
|
||||
raise message
|
||||
end
|
||||
|
||||
end
|
|
@ -7,7 +7,8 @@ module Webrat
|
|||
class Page
|
||||
extend Forwardable
|
||||
include Logging
|
||||
|
||||
include Flunk
|
||||
|
||||
attr_reader :session
|
||||
attr_reader :url
|
||||
|
||||
|
@ -27,23 +28,6 @@ module Webrat
|
|||
yield Scope.new(self, session.response_body, selector)
|
||||
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
|
||||
# and their data.
|
||||
#
|
||||
|
@ -82,14 +66,10 @@ module Webrat
|
|||
|
||||
protected
|
||||
|
||||
def open_in_browser(path) # :nodoc
|
||||
`open #{path}`
|
||||
end
|
||||
|
||||
def load_page
|
||||
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?
|
||||
reset_scope
|
||||
|
@ -103,14 +83,5 @@ module Webrat
|
|||
@scope ||= Scope.new(self, session.response_body)
|
||||
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
|
|
@ -1,5 +1,6 @@
|
|||
module Webrat
|
||||
class Scope
|
||||
include Flunk
|
||||
|
||||
def initialize(page, html, selector = nil)
|
||||
@page = page
|
||||
|
|
|
@ -1,5 +1,22 @@
|
|||
module Webrat
|
||||
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
|
||||
nil
|
||||
|
@ -40,8 +57,13 @@ module Webrat
|
|||
super || current_page.respond_to?(name)
|
||||
end
|
||||
|
||||
def save_and_open_page
|
||||
current_page.save_and_open
|
||||
def open_in_browser(path) # :nodoc
|
||||
`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
|
||||
|
||||
def method_missing(name, *args, &block)
|
||||
|
|
|
@ -18,7 +18,7 @@ describe "save_and_open_page" do
|
|||
|
||||
File.stub!(:exist?).and_return(true)
|
||||
Time.stub!(:now).and_return(1234)
|
||||
@session.current_page.stub!(:open_in_browser)
|
||||
@session.stub!(:open_in_browser)
|
||||
|
||||
@file_handle = mock("file handle")
|
||||
File.stub!(:open).with(filename, 'w').and_yield(@file_handle)
|
||||
|
@ -42,7 +42,7 @@ describe "save_and_open_page" do
|
|||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
||||
|
|
|
@ -7,10 +7,10 @@ describe "within" do
|
|||
|
||||
it "should click links within a scope" do
|
||||
@session.response_body = <<-EOS
|
||||
<a href="/page1">Link</a>
|
||||
<div id="container">
|
||||
<a href="/page2">Link</a>
|
||||
</div>
|
||||
<a href="/page1">Link</a>
|
||||
<div id="container">
|
||||
<a href="/page2">Link</a>
|
||||
</div>
|
||||
EOS
|
||||
|
||||
@session.should_receive(:get).with("/page2", {})
|
||||
|
@ -18,4 +18,39 @@ describe "within" do
|
|||
scope.clicks_link "Link"
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue