/
test_mechanize_link.rb
171 lines (130 loc) · 5.3 KB
/
test_mechanize_link.rb
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# frozen_string_literal: true
require 'mechanize/test_case'
class TestMechanizeLink < Mechanize::TestCase
def test_search
page = @mech.get("http://localhost/find_link.html")
link = page.link_with(text: "Form Test")
assert_equal('Form Test', link.text)
link_with_search = page.link_with(search: "//*[text()='Form Test']")
assert_equal(link, link_with_search)
link_with_xpath = page.link_with(xpath: "//*[text()='Form Test']")
assert_equal(link, link_with_xpath)
link_with_css = page.link_with(css: ".formtest")
assert_equal(link, link_with_css)
link_with_class = page.link_with(class: "formtest")
assert_equal(link, link_with_class)
end
def test_click
page = @mech.get("http://localhost/frame_test.html")
link = page.link_with(:text => "Form Test")
assert_equal('Form Test', link.text)
page = link.click
assert_equal("http://localhost/form_test.html",
@mech.history.last.uri.to_s)
end unless RUBY_ENGINE == 'jruby' # NekoHTML does not parse body of NOFRAMES
def test_click_bang
page = @mech.get("http://localhost/frame_test.html")
link = page.link_with!(:text => "Form Test")
assert_equal('Form Test', link.text)
page = link.click
assert_equal("http://localhost/form_test.html",
@mech.history.last.uri.to_s)
end unless RUBY_ENGINE == 'jruby' # NekoHTML does not parse body of NOFRAMES
def test_click_base
page = @mech.get("http://google.com/tc_base_link.html")
page = page.links.first.click
assert @mech.visited?("http://localhost/index.html")
end
def test_click_unsupported_scheme
page = @mech.get("http://google.com/tc_links.html")
link = page.link_with(:text => 'javascript link')
assert_raises Mechanize::UnsupportedSchemeError do
begin
link.click
rescue Mechanize::UnsupportedSchemeError => error
assert_equal 'javascript', error.scheme
assert_equal "javascript:new_page('1')", error.uri.to_s
raise
end
end
@mech.scheme_handlers['javascript'] = lambda { |my_link, my_page|
URI.parse('http://localhost/tc_links.html')
}
link.click
# HACK no assertion
end
def test_click_unexiting_link
page = @mech.get("http://google.com/tc_links.html")
assert_raises NoMethodError do
page.link_with(:text => 'no link').click
end
begin
page.link_with!(:text => 'no link').click
rescue => e
assert_instance_of Mechanize::ElementNotFoundError, e
assert_kind_of Mechanize::Page, e.source
assert_equal :link, e.element
assert_kind_of Hash, e.conditions
assert_equal 'no link', e.conditions[:text]
end
end
def test_click_empty_href
page = @mech.get("http://google.com/tc_links.html?q=test#anchor")
link = page.link_with(:text => 'empty href')
new_page = link.click
assert_equal "http://google.com/tc_links.html?q=test", new_page.uri.to_s
end
def test_text_alt_text
page = @mech.get("http://localhost/alt_text.html")
assert_equal(5, page.links.length)
assert_equal(1, page.meta_refresh.length)
assert_equal '', page.meta_refresh.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_uri_escaped
doc = Nokogiri::HTML::Document.new
node = Nokogiri::XML::Node.new('foo', doc)
node['href'] = 'http://foo.bar/%20baz'
link = Mechanize::Page::Link.new(node, nil, nil)
assert_equal 'http://foo.bar/%20baz', link.uri.to_s
end
def test_uri_no_path
page = @mech.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 unless RUBY_ENGINE == 'jruby' # NekoHTML does not parse IFRAME
def test_uri_weird
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_uri_weird_with_fragment
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 '%D1%83%D0%B2%D0%B0%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5', link.uri.fragment
end
def test_bad_uri_raise_compatible_exception
doc = Nokogiri::HTML::Document.new
node = Nokogiri::XML::Node.new('foo', doc)
node['href'] = 'http://http:foo.bar/ baz'
link = Mechanize::Page::Link.new(node, nil, nil)
assert_raises URI::InvalidURIError do
link.uri
end
end
def test_resolving_full_uri
page = @mech.get("http://localhost/frame_test.html")
link = page.link_with(:text => "Form Test")
assert_equal "/form_test.html", link.uri.to_s
assert_equal "http://localhost/form_test.html", link.resolved_uri.to_s
end unless RUBY_ENGINE == 'jruby' # NekoHTML does not parse body of NOFRAMES
end