Permalink
Browse files

Added custom Capybara selector to find a specific task field

  • Loading branch information...
1 parent e45466d commit 07b1c8d0eee7cb997f79c0269ee181f40de656f1 @rondy committed Dec 4, 2011
Showing with 31 additions and 17 deletions.
  1. +23 −17 spec/requests/todo_lists_request_spec.rb
  2. +1 −0 spec/spec_helper.rb
  3. +7 −0 spec/support/capybara_selectors.rb
@@ -18,15 +18,15 @@
fill_in "Name", :with => ""
click_button "Create Todo list"
all("div#tasks > div.task_field input[type=text]").should have(TodoList::NUMBER_OF_TASK_FIELDS).items
- fill_in(find("div#tasks > div.task_field:nth-child(1) input")[:id], :with => "Can't find a good name to my todo list")
+ fill_in(find(:task_field, 1)[:id], :with => "Can't find a good name to my todo list")
click_button "Create Todo list"
all("div#tasks > div.task_field input[type=text]").should have(TodoList::NUMBER_OF_TASK_FIELDS).items
end
scenario "can create a todo list with valid attributes" do
visit new_todo_list_path
fill_in "Name", :with => "Christmas wish list"
- fill_in(find("div#tasks > div.task_field:nth-child(1) input")[:id], :with => "Kindle")
+ fill_in(find(:task_field, 1)[:id], :with => "Kindle")
click_button "Create Todo list"
current_path.should eq todo_lists_path
within(".flash_message") { page.should have_content "Todo list was successfully created." }
@@ -38,22 +38,24 @@
visit new_todo_list_path
fill_in "Name", :with => "Things to learn"
all("div#tasks > div.task_field").should have(4).items
- fill_in(find("div#tasks > div.task_field:nth-child(1) input")[:id], :with => "Node.js")
- fill_in(find("div#tasks > div.task_field:nth-child(2) input")[:id], :with => "EventMachine")
- fill_in(find("div#tasks > div.task_field:nth-child(3) input")[:id], :with => "SVG")
- fill_in(find("div#tasks > div.task_field:nth-child(4) input")[:id], :with => "NoSQL")
+ fill_in(find(:task_field, 1)[:id], :with => "Node.js")
+ fill_in(find(:task_field, 2)[:id], :with => "EventMachine")
+ fill_in(find(:task_field, 3)[:id], :with => "SVG")
+ fill_in(find(:task_field, 4)[:id], :with => "NoSQL")
click_link "Add task"
all("div#tasks > div.task_field").should have(5).items
- fill_in(find("div#tasks > div.task_field:nth-child(5) input")[:id], :with => "Titanium Mobile")
+ fill_in(find(:task_field, 5)[:id], :with => "Titanium Mobile")
click_button "Create Todo list"
current_path.should eq todo_lists_path
within(".flash_message") { page.should have_content "Todo list was successfully created." }
within("div#todo_lists") { page.should have_content "Things to learn" }
- within("ul.tasks") { page.should have_content "Node.js" }
- within("ul.tasks") { page.should have_content "EventMachine" }
- within("ul.tasks") { page.should have_content "SVG" }
- within("ul.tasks") { page.should have_content "NoSQL" }
- within("ul.tasks") { page.should have_content "Titanium Mobile" }
+ within("ul.tasks") do
+ page.should have_content "Node.js"
+ page.should have_content "EventMachine"
+ page.should have_content "SVG"
+ page.should have_content "NoSQL"
+ page.should have_content "Titanium Mobile"
+ end
end
scenario "cannot create a todo list without name" do
@@ -69,13 +71,15 @@
visit edit_todo_list_path(todo_list)
all("div#tasks > div.task_field").should have(2).items
fill_in "Name", :with => "Things I MUST to learn"
- fill_in(find("div#tasks > div.task_field:nth-child(1) input")[:id], :with => "Backbone.js")
+ fill_in(find(:task_field, 1)[:id], :with => "Backbone.js")
click_button "Update Todo list"
current_path.should eq todo_lists_path
within(".flash_message") { page.should have_content "Todo list was successfully updated." }
within("div#todo_lists") { page.should have_content "Things I MUST to learn" }
- within("ul.tasks") { page.should have_content "Backbone.js" }
- within("ul.tasks") { page.should have_content "SproutCore" }
+ within("ul.tasks") do
+ page.should have_content "Backbone.js"
+ page.should have_content "SproutCore"
+ end
end
scenario "can remove items from a todo list", :js => true do
@@ -85,8 +89,10 @@
find("div#tasks > div.task_field:nth-child(2) a.remove_child").click
click_button "Update Todo list"
all("ul.tasks").should have(1).item
- within("ul.tasks") { page.should have_content "Cool item" }
- within("ul.tasks") { page.should_not have_content "Not so cool" }
+ within("ul.tasks") do
+ page.should have_content "Cool item"
+ page.should_not have_content "Not so cool"
+ end
end
scenario "can remove a todo list", :js => true do
View
@@ -18,6 +18,7 @@
config.use_transactional_fixtures = false
config.infer_base_class_for_anonymous_controllers = false
config.include AuthMacros
+ config.include CapybaraSelectors
end
end
@@ -0,0 +1,7 @@
+module CapybaraSelectors
+
+ Capybara.add_selector(:task_field) do
+ xpath { |num| XPath.css("div#tasks > div.task_field:nth-child(#{num}) input") }
+ end
+
+end

0 comments on commit 07b1c8d

Please sign in to comment.