Support passing an ActiveRecord model to #within when in Rails mode [#68 state:resolved] (Luke Melia)

This commit is contained in:
Bryan Helmkamp 2009-01-17 13:18:42 -05:00
parent 21aef01222
commit 7a7cb3f002
7 changed files with 73 additions and 1 deletions

View File

@ -20,6 +20,8 @@
* Use Hpricot and REXML when not parsing with Nokogiri (on JRuby, for example) * Use Hpricot and REXML when not parsing with Nokogiri (on JRuby, for example)
* Minor enhancements * Minor enhancements
* Support passing an ActiveRecord model to #within when in Rails mode [#68] (Luke Melia)
* Added assert_contain, assert_not_contain [#86] (Mike Gaffney, Amos King) * Added assert_contain, assert_not_contain [#86] (Mike Gaffney, Amos King)
* Add configuration options for the Selenium environment and port (Kieran Pilkington) * Add configuration options for the Selenium environment and port (Kieran Pilkington)
* Maximize the browser window after initializing Selenium (Luke Melia) * Maximize the browser window after initializing Selenium (Luke Melia)

View File

@ -2,9 +2,26 @@ require "webrat"
require "action_controller" require "action_controller"
require "action_controller/integration" require "action_controller/integration"
require "action_controller/record_identifier"
module Webrat module Webrat
class RailsSession < Session #:nodoc: class RailsSession < Session #:nodoc:
include ActionController::RecordIdentifier
# The Rails version of within supports passing in a model and Webrat
# will apply a scope based on Rails' dom_id for that model.
#
# Example:
# within User.last do
# click_link "Delete"
# end
def within(selector_or_object, &block)
if selector_or_object.is_a?(String)
super
else
super('#' + dom_id(selector_or_object), &block)
end
end
def doc_root def doc_root
File.expand_path(File.join(RAILS_ROOT, 'public')) File.expand_path(File.join(RAILS_ROOT, 'public'))

View File

@ -1,3 +1,9 @@
class FakeModel
def id
nil
end
end
class WebratController < ApplicationController class WebratController < ApplicationController
def form def form

View File

@ -13,5 +13,11 @@
<%= select_tag "month", "<option></option><option>January</option>" %> <%= select_tag "month", "<option></option><option>January</option>" %>
</label> </label>
<% div_for FakeModel.new do %>
<label>
Object field <%= text_field_tag "object_field" %>
</label>
<% end %>
<%= submit_tag "Test" %> <%= submit_tag "Test" %>
<% end %> <% end %>

View File

@ -0,0 +1,17 @@
require 'test_helper'
class WebratTest < ActionController::IntegrationTest
test "should scope within an object" do
visit root_path
within FakeModel.new do
fill_in "Object field", :with => "Test"
assert_raise Webrat::NotFoundError do
check "TOS"
end
end
end
end

View File

@ -1,6 +1,7 @@
require 'test_helper' require 'test_helper'
class WebratTest < ActionController::IntegrationTest class WebratTest < ActionController::IntegrationTest
test "should visit pages" do test "should visit pages" do
visit root_path visit root_path
assert_tag "Webrat Form" assert_tag "Webrat Form"
@ -24,4 +25,5 @@ class WebratTest < ActionController::IntegrationTest
visit external_redirect_path visit external_redirect_path
assert response.redirect? assert response.redirect?
end end
end end

View File

@ -85,4 +85,26 @@ describe Webrat::RailsSession do
it "should provide a doc_root" do it "should provide a doc_root" do
Webrat::RailsSession.new(mock("integration session")).should respond_to(:doc_root) Webrat::RailsSession.new(mock("integration session")).should respond_to(:doc_root)
end end
it "should accept an ActiveRecord argument to #within and translate to a selector using dom_id" do
body = <<-HTML
<a href="/page1">Edit</a>
<div id="new_object">
<a href="/page2">Edit</a>
</div>
HTML
response = mock("response", :body => body, :headers => {}, :code => 200)
@integration_session.stub!(:response => response)
@integration_session.should_receive(:get).with("/page2", {}, nil)
rails_session = Webrat::RailsSession.new(@integration_session)
object = Object.new
object.stub!(:id => nil)
rails_session.within(object) do
rails_session.click_link 'Edit'
end
end
end end