Skip to content

Instantly share code, notes, and snippets.

@jferris
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jferris/d21b7e511e0509925d77 to your computer and use it in GitHub Desktop.
Save jferris/d21b7e511e0509925d77 to your computer and use it in GitHub Desktop.
Demonstrate Capybara finders
require "rubygems"
require "active_support"
require "active_support/core_ext"
require "capybara"
require "capybara/webkit"
require "sinatra/base"
class App < Sinatra::Base
get "/" do
<<-HTML
<html>
<body>
<ol id="items">
</ol>
<script type="text/javascript">
setTimeout(function () {
var list = document.getElementById("items");
for (var i = 1; i <= 5; i++) {
var item = document.createElement("li");
var text = document.createTextNode("Element " + i);
item.appendChild(text);
list.appendChild(item);
}
}, 1000);
</script>
</body>
</html>
HTML
end
end
Capybara.app = App
Capybara.default_driver = :webkit
def run_test(title)
puts title
page = Capybara.current_session
page.visit("/")
yield page
page.reset!
puts
end
run_test "Testing #find" do |page|
p page.find("ol li", match: :first).try(:text)
end
run_test "Testing #all" do |page|
p page.all("ol li").map { |li| li.try(:text) }
end
run_test "Testing #all with minimum" do |page|
p page.all("ol li", minimum: 1).map { |li| li.try(:text) }
end
run_test "Testing #first" do |page|
p page.first("ol li").try(:text)
end
% ruby capybara.rb
Testing #find
"Element 1"
Testing #all
[]
Testing #all with minimum
["Element 1", "Element 2", "Element 3", "Element 4", "Element 5"]
Testing #first
nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment