Skip to content

Commit

Permalink
Support passing an ActiveRecord model to #within when in Rails mode [#68
Browse files Browse the repository at this point in the history
 state:resolved] (Luke Melia)
  • Loading branch information
brynary committed Jan 17, 2009
1 parent 21aef01 commit 7a7cb3f
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 1 deletion.
2 changes: 2 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* Use Hpricot and REXML when not parsing with Nokogiri (on JRuby, for example)

* 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)
* Add configuration options for the Selenium environment and port (Kieran Pilkington)
* Maximize the browser window after initializing Selenium (Luke Melia)
Expand Down
19 changes: 18 additions & 1 deletion lib/webrat/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@

require "action_controller"
require "action_controller/integration"
require "action_controller/record_identifier"

module Webrat
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
File.expand_path(File.join(RAILS_ROOT, 'public'))
end
Expand Down
6 changes: 6 additions & 0 deletions spec/integration/rails/app/controllers/webrat_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
class FakeModel
def id
nil
end
end

class WebratController < ApplicationController

def form
Expand Down
6 changes: 6 additions & 0 deletions spec/integration/rails/app/views/webrat/form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@
<%= select_tag "month", "<option></option><option>January</option>" %>
</label>

<% div_for FakeModel.new do %>
<label>
Object field <%= text_field_tag "object_field" %>
</label>
<% end %>
<%= submit_tag "Test" %>
<% end %>
17 changes: 17 additions & 0 deletions spec/integration/rails/test/integration/webrat_rails_test.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions spec/integration/rails/test/integration/webrat_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'test_helper'

class WebratTest < ActionController::IntegrationTest

test "should visit pages" do
visit root_path
assert_tag "Webrat Form"
Expand All @@ -24,4 +25,5 @@ class WebratTest < ActionController::IntegrationTest
visit external_redirect_path
assert response.redirect?
end

end
22 changes: 22 additions & 0 deletions spec/private/rails/rails_session_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,26 @@
it "should provide a doc_root" do
Webrat::RailsSession.new(mock("integration session")).should respond_to(:doc_root)
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

1 comment on commit 7a7cb3f

@tmak
Copy link

@tmak tmak commented on 7a7cb3f Jan 18, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And what if you want to run your tests in selenium mode (or another new mode)?
Such features will make it harder to maintain the code base. I thought a main goal is to keep it possible to run the webrat tests in multiple modes?
I think the public webrat api should be web framework agnostic.

BTW: I think for a rails guy it’s not a big deal to write “within(dom_id(user))” ;-)

WDYT?

Please sign in to comment.