Skip to content

Instantly share code, notes, and snippets.

@kylewelsby
Created June 14, 2013 14:52
Show Gist options
  • Save kylewelsby/5782473 to your computer and use it in GitHub Desktop.
Save kylewelsby/5782473 to your computer and use it in GitHub Desktop.
Selecting content from a page and rendering it into an array of hashes `ruby server.rb` to start the server before running test.
require_relative 'test_helper'
require 'capybara'
require 'capybara/poltergeist'
class ArraySotringTest < MiniTest::Test
def test_drill_down
session = Capybara::Session.new(:poltergeist)
session.visit "http://0.0.0.0:9494/"
links = session.all('a.page').map{ |el| el[:href] }.compact
return_url = session.current_url
result = []
links.each do |path|
session.visit(session.current_url + path)
# name
node = session.all('h1').first.base
node1= NodeCapture.new(node).node_tree
# price
node = session.all('.price').first.base
node2= NodeCapture.new(node).node_tree
# price
node = session.all("ul li").first.base
node3= NodeCapture.new(node).node_tree
common = CommonArray.new([node1,node2,node3]).common
session.all(common.join(' ')).each do |parent|
name = parent.all('h1').collect { |n| n.text.nil? ? n['src'] : n.text }
price= parent.all('.price').collect { |n| n.text.nil? ? n['src'] : n.text }
feat = parent.all('ul li').collect { |n| n.text.nil? ? n['src'] : n.text }
name = name.size == 1 ? name.first : name
price = price.size == 1 ? price.first : price
feat = feat.size == 1 ? feat.first : feat
hash = {}
hash.store('Name',name)
hash.store("Price", price)
feat.each_with_index do |feat,index|
hash.store("Feat#{index+1}", feat)
end
result << hash
end
session.visit return_url
end
assert_equal([
{"Name"=>"Product 1", "Price"=> "9.99", "Feat1"=>"Feature 1", "Feat2"=>"Feature 2", "Feat3"=>"Feature 3"},
{"Name"=>"Product 2", "Price"=>"19.98", "Feat1"=>"Feature 1", "Feat2"=>"Feature 2", "Feat3"=>"Feature 3"},
{"Name"=>"Product 3", "Price"=>"29.97", "Feat1"=>"Feature 1", "Feat2"=>"Feature 2", "Feat3"=>"Feature 3"},
{"Name"=>"Product 4", "Price"=>"39.96", "Feat1"=>"Feature 1", "Feat2"=>"Feature 2", "Feat3"=>"Feature 3"},
{"Name"=>"Product 5", "Price"=>"49.95", "Feat1"=>"Feature 1", "Feat2"=>"Feature 2", "Feat3"=>"Feature 3"}
], result)
end
def test_single_page
session = Capybara::Session.new(:poltergeist)
session.visit "http://0.0.0.0:9494/"
nodes = session.all('.page')
node = nodes.first.base
node1= NodeCapture.new(node).node_tree
nodes = session.all('.desc')
node = nodes.first.base
node2 = NodeCapture.new(node).node_tree
result = []
common = CommonArray.new([node1,node2]).common
session.all(common.join(' ')).each do |parent|
name = parent.all('.page').collect { |n| n.text.nil? ? n['src'] : n.text }
desc = parent.all('.desc').collect { |n| n.text.nil? ? n['src'] : n.text }
name = name.size == 1 ? name.first : name
desc = desc.size == 1 ? desc.first : desc
result << {'Name' => name, "Desc" => desc}
end
assert_equal([
{"Name" => "Page 1", "Desc" => "This is the description for page 1."},
{"Name" => "Page 2", "Desc" => "This is the description for page 2."},
{"Name" => "Page 3", "Desc" => "This is the description for page 3."},
{"Name" => "Page 4", "Desc" => "This is the description for page 4."},
{"Name" => "Page 5", "Desc" => "This is the description for page 5."},
], result)
end
end
class CommonArray
attr_accessor :common
def initialize(arrays)
@common = arrays[0].zip(*arrays[1..-1]).map{|n| n.first if n.all?{|a| a == n.first}}.compact
end
end
class NodeCapture
attr_accessor :node_tree
# given a Capybara::Base::Node
# then it should calulate the node_tree
def initialize(node)
@node_tree = []
@node = node
get_parent(node)
@node_tree = @node_tree.reverse
end
def node
@node
end
def get_parent(node)
begin
@node_tree << node.tag_name
node = node.find_xpath('..').first
get_parent(node)
rescue Capybara::Poltergeist::ObsoleteNode
end
end
end
require 'sinatra'
require "sinatra/reloader"
PAGES = 5
set :port, 9494
get "/" do
html = ""
html << "<p>welcome to index page</p>"
html << "<ul>"
PAGES.times do |i|
html << "<li>"
html << "<a class='page' href='/pages/#{i+1}'>Page #{i+1}</a>"
html << "<p class='desc'>This is the description for page #{i+1}.</p>"
html << "</li>"
end
html << "</ul>"
html << "<p>footer</p>"
end
get "/failing_drilldown" do
html = ""
html << "<p>welcome to index page</p>"
html << "<ul>"
(PAGES+1).times do |i|
html << "<li><a class='page' href='/pages/#{i+1}'>Page #{i+1}</a></li>"
end
html << "</ul>"
html << "<p>footer</p>"
end
get "/pages/:id" do |id|
html = ""
if id.to_i <= PAGES
html << "<h1>Product #{id}</h1>"
html << %Q{<div class="price">#{9.99 * id.to_i}</div>}
html << "<ul class='features'>"
3.times do |i|
html << "<li>Feature #{i+1}</li>"
end
html << "</ul>"
end
html << "<nav id='pagination'>"
html << _welcome(id)
html << _home(id)
html << _prev(id) if id.to_i > 1
html << _next(id) if id.to_i < PAGES
html << "</nav>"
html
end
get "/scrollpage/:id" do |id|
html = ""
html << "<nav id='pagination'>"
html << _welcome(id)
html << _scroll_link(id)
html << "</nav>"
html
end
get "/imagepage/:id" do |id|
<<-EOT
#{_welcome(id)}
#{_image_link(id)}
EOT
end
def _welcome(id)
"<p>welcome to page #{id}</p>"
end
def _home(id)
"<a class='home' href='/'>Home</a>"
end
def _next(id)
"<a class='next' href='/pages/#{id.to_i+1}'>Next</a>"
end
def _prev(id)
"<a class='prev' href='/pages/#{id.to_i-1}'>Prev</a>"
end
def _image_link(id)
(1..id.to_i).map { |i| "<img class='image' src='pretend#{i}.gif'>"}.join("\n")
end
def _scroll_link(id)
(1..id.to_i).map { |i| "<a class='scroll-link' href='/scrollpage/#{i}'>Scroll Item #{i}</a>"}.join("\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment