Created
October 5, 2009 05:28
-
-
Save AlexJWayne/201905 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rubygems' | |
| require 'open-uri' | |
| require 'hpricot' | |
| require 'test/unit' | |
| class HpricotTest < Test::Unit::TestCase | |
| def assert_selector_is_found(page, selector, message = nil) | |
| message ||= "Expected to find elements to match selector: #{selector}" | |
| elements = (page / selector) | |
| assert(elements.any?, message) | |
| end | |
| def assert_selector_not_found(page, selector, message = nil) | |
| message ||= "Expected no elements to match selector: #{selector}" | |
| elements = (page / selector) | |
| assert(elements.empty?, message) | |
| end | |
| def test_url_based_page_should_find_element | |
| page = Hpricot(open("http://twitter.com/Squeegy")) | |
| assert_selector_is_found(page, 'a[@class="tweet-url web"]') | |
| assert_selector_is_found(page, 'a[@class~="tweet-url"]') | |
| assert_selector_is_found(page, 'a[@class~="web"]') | |
| assert_selector_is_found(page, 'a.tweet-url') | |
| assert_selector_is_found(page, 'a.web') | |
| assert_selector_is_found(page, 'a.tweet-url.web') | |
| assert_selector_not_found(page, 'a[@class="web tweet-url"]') | |
| end | |
| def test_string_based_page_should_find_element | |
| page = Hpricot(<<-HTML) | |
| <div> | |
| <div> | |
| <a href="http://foobar.com" class="tweet-url web">Foobar</a> | |
| </div> | |
| <div> | |
| <a href="http://barbaz.com" class="tweet-url web">Barbaz</a> | |
| </div> | |
| <div> | |
| <a href="http://bazfoo.com" class="tweet-url web">Bazfoo</a> | |
| </div> | |
| </div> | |
| HTML | |
| assert_selector_is_found(page, 'a[@class="tweet-url web"]') | |
| assert_selector_is_found(page, 'a[@class~="tweet-url"]') | |
| assert_selector_is_found(page, 'a[@class~="web"]') | |
| assert_selector_is_found(page, 'a.tweet-url') | |
| assert_selector_is_found(page, 'a.web') | |
| assert_selector_is_found(page, 'a.tweet-url.web') | |
| assert_selector_not_found(page, 'a[@class="web tweet-url"]') | |
| end | |
| def test_should_list_all_attributes | |
| page = Hpricot(open("http://twitter.com/Squeegy")) | |
| link = page.at('a.web.tweet-url') | |
| attributes = link.attributes | |
| assert_kind_of(Hash, attributes) | |
| assert_equal(attributes['class'], 'tweet-url web') | |
| assert(attributes.has_key?('href')) | |
| assert(attributes.has_key?('rel')) | |
| assert(attributes.has_key?('target')) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment