Permalink
Browse files

upgrade to capybara 2

  • Loading branch information...
1 parent cee9a89 commit 314b67531075b1ccb952646f2cce87c45cd9172c @taavo taavo committed Dec 18, 2012
View
@@ -34,7 +34,7 @@ end
group :development, :test do
gem 'strong_parameters', '~> 0.1.5'
gem 'cancan', '~> 1.6'
- gem 'capybara', '~> 1.1'
+ gem 'capybara', '~> 2.0'
gem 'carrierwave', '~> 0.6'
gem 'database_cleaner', '~> 0.8'
gem 'devise', '~> 2.1'
@@ -76,9 +76,9 @@ def initialize(user)
it "GET /admin should show Player but not League" do
visit dashboard_path
- expect(body).to have_content("Player")
- expect(body).not_to have_content("League")
- expect(body).not_to have_content("Add new")
+ should have_content("Player")
+ should_not have_content("League")
+ should_not have_content("Add new")
end
it "GET /admin/player should render successfully but not list retired players and not show new, edit, or delete actions" do
@@ -132,7 +132,7 @@ def initialize(user)
fill_in "player[name]", :with => "Jackie Robinson"
fill_in "player[number]", :with => "42"
fill_in "player[position]", :with => "Second baseman"
- click_button "Save"
+ first(:button, "Save").click
should_not have_content("Edit")
@player = RailsAdmin::AbstractModel.new("Player").first
@@ -165,7 +165,7 @@ def initialize(user)
should_not have_content("History")
should_not have_content("Show in app")
fill_in "player[name]", :with => "Jackie Robinson"
- click_button "Save"
+ first(:button, "Save").click
@player.reload
expect(@player.name).to eq("Jackie Robinson")
end
@@ -276,17 +276,17 @@ def initialize(user)
active_player = FactoryGirl.create :player, :retired => false
retired_player = FactoryGirl.create :player, :retired => true
- page.driver.post(bulk_action_path(:bulk_action => 'bulk_delete', :model_name => "player", :bulk_ids => [active_player, retired_player].map(&:id)))
+ post bulk_action_path(:bulk_action => 'bulk_delete', :model_name => "player", :bulk_ids => [active_player, retired_player].map(&:id))
- should have_content(active_player.name)
- should_not have_content(retired_player.name)
+ expect(response.body).to include(active_player.name)
+ expect(response.body).not_to include(retired_player.name)
end
it "POST /admin/player/bulk_destroy should destroy records which are authorized to" do
active_player = FactoryGirl.create :player, :retired => false
retired_player = FactoryGirl.create :player, :retired => true
- page.driver.delete(bulk_delete_path(:model_name => "player", :bulk_ids => [active_player, retired_player].map(&:id)))
+ delete bulk_delete_path(:model_name => "player", :bulk_ids => [active_player, retired_player].map(&:id))
expect(@player_model.get(active_player.id)).to be_nil
expect(@player_model.get(retired_player.id)).not_to be_nil
end
@@ -298,18 +298,18 @@ def initialize(user)
active_player = FactoryGirl.create :player, :retired => false
retired_player = FactoryGirl.create :player, :retired => true
- page.driver.post(bulk_action_path(:bulk_action => 'bulk_delete', :model_name => "player", :bulk_ids => [active_player, retired_player].map(&:id)))
+ post bulk_action_path(:bulk_action => 'bulk_delete', :model_name => "player", :bulk_ids => [active_player, retired_player].map(&:id))
- should have_content(active_player.name)
- should_not have_content(retired_player.name)
+ expect(response.body).to include(active_player.name)
+ expect(response.body).not_to include(retired_player.name)
end
it "POST /admin/player/bulk_destroy should destroy records which are authorized to" do
@user.update_attributes(:roles => [:admin, :test_exception])
active_player = FactoryGirl.create :player, :retired => false
retired_player = FactoryGirl.create :player, :retired => true
- page.driver.delete(bulk_delete_path(:model_name => "player", :bulk_ids => [active_player, retired_player].map(&:id)))
+ delete bulk_delete_path(:model_name => "player", :bulk_ids => [active_player, retired_player].map(&:id))
expect(@player_model.get(active_player.id)).to be_nil
expect(@player_model.get(retired_player.id)).not_to be_nil
end
@@ -10,8 +10,8 @@
describe "bulk_delete" do
it "shows names of to-be-deleted players" do
- page.driver.post(bulk_action_path(:bulk_action => 'bulk_delete', :model_name => "player", :bulk_ids => @players.map(&:id)))
- @players.each { |player| should have_content(player.name) }
+ post(bulk_action_path(:bulk_action => 'bulk_delete', :model_name => "player", :bulk_ids => @players.map(&:id)))
+ @players.each { |player| expect(response.body).to include(player.name) }
end
end
@@ -9,8 +9,13 @@
RailsAdmin.config { |c| c.audit_with :history }
@players = 3.times.map { FactoryGirl.create(:player) }
@delete_ids = @players[0..1].map(&:id)
- page.driver.post(bulk_action_path(:bulk_action => 'bulk_delete', :model_name => "player", :bulk_ids => @delete_ids))
+ # NOTE: This uses an internal, unsupported capybara API which could break at any moment. We
+ # should refactor this test so that it either A) uses capybara's supported API (only GET
+ # requests via visit) or B) just uses Rack::Test (and doesn't use capybara for browser
+ # interaction like click_button).
+ page.driver.browser.reset_host!
+ page.driver.browser.process :post, bulk_action_path(:bulk_action => 'bulk_delete', :model_name => "player", :bulk_ids => @delete_ids, '_method' => 'post')
click_button "Yes, I'm sure"
end
@@ -31,7 +36,13 @@
before do
@players = 3.times.map { FactoryGirl.create(:player) }
@delete_ids = @players[0..1].map(&:id)
- page.driver.post(bulk_action_path(:bulk_action => 'bulk_delete', :model_name => "player", :bulk_ids => @delete_ids))
+
+ # NOTE: This uses an internal, unsupported capybara API which could break at any moment. We
+ # should refactor this test so that it either A) uses capybara's supported API (only GET
+ # requests via visit) or B) just uses Rack::Test (and doesn't use capybara for browser
+ # interaction like click_button).
+ page.driver.browser.reset_host!
+ page.driver.browser.process :post, bulk_action_path(:bulk_action => 'bulk_delete', :model_name => "player", :bulk_ids => @delete_ids, '_method' => 'post')
click_button "Cancel"
end
@@ -9,7 +9,7 @@
fill_in "player[name]", :with => "Jackie Robinson"
fill_in "player[number]", :with => "42"
fill_in "player[position]", :with => "Second baseman"
- click_button "Save"
+ first(:button, "Save").click
@player = RailsAdmin::AbstractModel.new("Player").first
end
@@ -62,7 +62,7 @@
before(:each) do
@draft = FactoryGirl.create :draft
- page.driver.post new_path(:model_name => "player", :player => {:name => "Jackie Robinson", :number => 42, :position => 'Second baseman', :draft_id => @draft.id})
+ post new_path(:model_name => "player", :player => {:name => "Jackie Robinson", :number => 42, :position => 'Second baseman', :draft_id => @draft.id})
@player = RailsAdmin::AbstractModel.new("Player").all.last # first is created by FactoryGirl
end
@@ -76,7 +76,7 @@
describe "create with has-many association" do
before(:each) do
@divisions = 3.times.map { Division.create!(:name => "div #{Time.now.to_f}", :league => League.create!(:name => "league #{Time.now.to_f}")) }
- page.driver.post new_path(:model_name => "league", :league => {:name => "National League", :division_ids =>[@divisions[0].id]})
+ post new_path(:model_name => "league", :league => {:name => "National League", :division_ids =>[@divisions[0].id]})
@league = RailsAdmin::AbstractModel.new("League").all.last
end
@@ -91,7 +91,7 @@
describe "create with has-and-belongs-to-many association" do
before(:each) do
@teams = 3.times.map { FactoryGirl.create :team }
- page.driver.post new_path(:model_name => "fan", :fan => {:name => "John Doe", :team_ids => [@teams[0].id] })
+ post new_path(:model_name => "fan", :fan => {:name => "John Doe", :team_ids => [@teams[0].id] })
@fan = RailsAdmin::AbstractModel.new("Fan").first
end
@@ -108,22 +108,21 @@
@team = FactoryGirl.create :team
@player = FactoryGirl.create :player, :team => @team
- page.driver.post new_path(:model_name => "player", :player => {:name => @player.name, :number => @player.number.to_s, :position => @player.position, :team_id => @team.id})
+ post new_path(:model_name => "player", :player => {:name => @player.name, :number => @player.number.to_s, :position => @player.position, :team_id => @team.id})
end
it "shows an error message" do
- should have_content("There is already a player with that number on this team")
+ expect(response.body).to include("There is already a player with that number on this team")
end
end
describe "create with invalid object" do
before(:each) do
- page.driver.post(new_path(:model_name => "player", :id => 1), :params => {:player => {}})
+ post new_path(:model_name => "player", :player => {:id => 1})
end
it "shows an error message" do
- should have_content("Player failed to be created")
- should have_selector "form", :action => "/admin/players"
+ expect(response.body).to include("Player failed to be created")
end
end
@@ -10,7 +10,7 @@
fill_in "cms_basic_page[title]", :with => "Hello"
fill_in "cms_basic_page[content]", :with => "World"
expect {
- click_button "Save"
+ first(:button, "Save").click
}.to change(Cms::BasicPage, :count).by(1)
end
end
@@ -53,19 +53,19 @@
describe "destroy with missing object" do
before(:each) do
- page.driver.delete(delete_path(:model_name => "player", :id => 1))
+ delete delete_path(:model_name => "player", :id => 1)
end
it "raises NotFound" do
- expect(page.driver.status_code).to eq(404)
+ expect(response.code).to eq("404")
end
end
describe "destroy from show page" do
it "redirects to the index instead of trying to show the deleted object" do
@player = FactoryGirl.create :player
visit show_path(:model_name => 'player', :id => @player.id)
- visit delete_path(:model_name => "player", :id => @player.id)
+ click_link "Delete"
click_button "Yes, I'm sure"
expect(URI.parse(page.current_url).path).to eq(index_path(:model_name => 'player'))
@@ -75,7 +75,7 @@
Player.any_instance.stub(:destroy_hook).and_return false
@player = FactoryGirl.create :player
visit show_path(:model_name => 'player', :id => @player.id)
- visit delete_path(:model_name => "player", :id => @player.id)
+ click_link "Delete"
click_button "Yes, I'm sure"
expect(URI.parse(page.current_url).path).to eq(show_path(:model_name => 'player', :id => @player.id))
@@ -30,13 +30,13 @@
it "adds a related id to the belongs_to create team link" do
@player = FactoryGirl.create :player
visit edit_path(:model_name => "player", :id => @player.id)
- should have_selector("a", :href => 'admin/teams/new?associations[players]=' + @player.id.to_s)
+ should have_selector("a[data-link='/admin/team/new?associations%5Bplayers%5D=#{@player.id.to_s}&modal=true']")
end
it "adds a related id to the has_many create team link" do
@team = FactoryGirl.create :team
visit edit_path(:model_name => "team", :id => @team.id)
- should have_selector("a", :href => 'admin/players/new?associations[team]=' + @team.id.to_s)
+ should have_selector("a[data-link='/admin/player/new?associations%5Bteam%5D=#{@team.id.to_s}&modal=true']")
end
end
@@ -46,7 +46,7 @@
should have_content 'Select fields to export'
select "<comma> ','", :from => "csv_options_generator_col_sep"
click_button 'Export to csv'
- csv = CSV.parse find('body').text
+ csv = CSV.parse page.driver.response.body.force_encoding("utf-8") # comes through as us-ascii on some platforms
expect(csv[0]).to match_array ["Id", "Created at", "Updated at", "Deleted at", "Name", "Position",
"Number", "Retired", "Injured", "Born on", "Notes", "Suspended", "Id [Team]", "Created at [Team]",
"Updated at [Team]", "Name [Team]", "Logo url [Team]", "Team Manager [Team]", "Ballpark [Team]",
@@ -87,7 +87,7 @@
visit export_path(:model_name => 'comment')
select "<comma> ','", :from => "csv_options_generator_col_sep"
click_button 'Export to csv'
- csv = CSV.parse find('body').text
+ csv = CSV.parse page.driver.response.body
expect(csv[0]).to match_array ["Id", "Commentable", "Commentable type", "Content", "Created at", "Updated at"]
csv[1..-1].each do |line|
expect(line[csv[0].index('Commentable')]).to eq(@player.id.to_s)
@@ -37,9 +37,9 @@
should have_content("Updated at")
# it "shows the show, edit and delete links" do
- should have_selector("td a", :text => 'Show')
- should have_selector("td a", :text => 'Edit')
- should have_selector("td a", :text => 'Delete')
+ should have_selector("li[title='Show'] a")
+ should have_selector("li[title='Edit'] a")
+ should have_selector("li[title='Delete'] a")
# it "has the search box with some prompt text" do
should have_selector("input[placeholder='Filter']")
@@ -278,9 +278,9 @@
end
end
- visit index_path(:model_name => "player")
- should have_content(%{$.filters.append("Name", "name", "string", "", null, "", 1);})
- should have_content(%{$.filters.append("Team", "team", "belongs_to_association", "", null, "", 2);})
+ get index_path(:model_name => "player")
+ expect(response.body).to include(%{$.filters.append("Name", "name", "string", "", null, "", 1);})
+ expect(response.body).to include(%{$.filters.append("Team", "team", "belongs_to_association", "", null, "", 2);})
end
end
@@ -361,7 +361,7 @@
describe "list as compact json" do
it "has_content an array with 2 elements and contain an array of elements with keys id and label" do
2.times.map { FactoryGirl.create :player }
- response = page.driver.get(index_path(:model_name => "player", :compact => true, :format => :json))
+ get index_path(:model_name => "player", :compact => true, :format => :json)
expect(ActiveSupport::JSON.decode(response.body).length).to eq(2)
ActiveSupport::JSON.decode(response.body).each do |object|
expect(object).to have_key("id")
Oops, something went wrong.

0 comments on commit 314b675

Please sign in to comment.