File: test_links.rb

package info (click to toggle)
libwww-mechanize-ruby 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 956 kB
  • ctags: 883
  • sloc: ruby: 6,621; makefile: 4
file content (128 lines) | stat: -rw-r--r-- 4,093 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
require "helper"

class LinksMechTest < Test::Unit::TestCase
  def setup
    @agent = Mechanize.new
  end

  def test_weird_uri
    doc = Nokogiri::HTML::Document.new
    node = Nokogiri::XML::Node.new('foo', doc)
    node['href'] = 'http://foo.bar/ baz'
    link = Mechanize::Page::Link.new(node, nil, nil)
    assert_equal 'http://foo.bar/%20baz', link.uri.to_s
  end

  def test_unsupported_link_types
    page = @agent.get("http://google.com/tc_links.html")
    link = page.link_with(:text => 'javascript link')
    assert_raise(Mechanize::UnsupportedSchemeError) {
      link.click
    }

    @agent.scheme_handlers['javascript'] = lambda { |my_link, my_page|
      URI.parse('http://localhost/tc_links.html')
    }
    assert_nothing_raised {
      link.click
    }
  end

  def test_link_with_no_path
    page = @agent.get("http://localhost/relative/tc_relative_links.html")
    page = page.link_with(:text => 'just the query string').click
    assert_equal('http://localhost/relative/tc_relative_links.html?a=b', page.uri.to_s)
  end

  def test_base
    page = @agent.get("http://google.com/tc_base_link.html")
    page = page.links.first.click
    assert @agent.visited?("http://localhost/index.html")
  end

  def test_find_meta
    page = @agent.get("http://localhost/find_link.html")
    assert_equal(3, page.meta.length)
    assert_equal(%w{
      http://www.drphil.com/
      http://www.upcase.com/
      http://tenderlovemaking.com/ }.sort,
      page.meta.map { |x| x.href.downcase }.sort)
  end

  def test_find_link
    page = @agent.get("http://localhost/find_link.html")
    assert_equal(18, page.links.length)
  end

  def test_alt_text
    page = @agent.get("http://localhost/alt_text.html")
    assert_equal(5, page.links.length)
    assert_equal(1, page.meta.length)

    assert_equal('', page.meta.first.text)
    assert_equal('alt text', page.link_with(:href => 'alt_text.html').text)
    assert_equal('', page.link_with(:href => 'no_alt_text.html').text)
    assert_equal('no image', page.link_with(:href => 'no_image.html').text)
    assert_equal('', page.link_with(:href => 'no_text.html').text)
    assert_equal('', page.link_with(:href => 'nil_alt_text.html').text)
  end

  def test_click_link
    @agent.user_agent_alias = 'Mac Safari'
    page = @agent.get("http://localhost/frame_test.html")
    link = page.link_with(:text => "Form Test")
    assert_not_nil(link)
    assert_equal('Form Test', link.text)
    page = @agent.click(link)
    assert_equal("http://localhost/form_test.html",
      @agent.history.last.uri.to_s)
  end

  def test_click_method
    page = @agent.get("http://localhost/frame_test.html")
    link = page.link_with(:text => "Form Test")
    assert_not_nil(link)
    assert_equal('Form Test', link.text)
    page = link.click
    assert_equal("http://localhost/form_test.html",
      @agent.history.last.uri.to_s)
  end

  def test_find_bold_link
    page = @agent.get("http://localhost/tc_links.html")
    link = page.links_with(:text => /Bold Dude/)
    assert_equal(1, link.length)
    assert_equal('Bold Dude', link.first.text)

    link = page.links_with(:text => 'Aaron James Patterson')
    assert_equal(1, link.length)
    assert_equal('Aaron James Patterson', link.first.text)

    link = page.links_with(:text => 'Aaron Patterson')
    assert_equal(1, link.length)
    assert_equal('Aaron Patterson', link.first.text)

    link = page.links_with(:text => 'Ruby Rocks!')
    assert_equal(1, link.length)
    assert_equal('Ruby Rocks!', link.first.text)
  end

  def test_link_with_encoded_space
    page = @agent.get("http://localhost/tc_links.html")
    link = page.link_with(:text => 'encoded space')
    page = @agent.click link
  end

  def test_link_with_space
    page = @agent.get("http://localhost/tc_links.html")
    link = page.link_with(:text => 'not encoded space')
    page = @agent.click link
  end

  def test_link_with_unusual_characters
    page = @agent.get("http://localhost/tc_links.html")
    link = page.link_with(:text => 'unusual characters')
    assert_nothing_raised { @agent.click link }
  end
end