webrat 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,193 @@
1
+ require File.dirname(__FILE__) + "/helper"
2
+
3
+ class ClicksLinkTest < Test::Unit::TestCase
4
+ def setup
5
+ @session = ActionController::Integration::Session.new
6
+ @session.stubs(:assert_response)
7
+ @session.stubs(:get_via_redirect)
8
+ @session.stubs(:response).returns(@response=mock)
9
+ end
10
+
11
+ def test_should_use_get_by_default
12
+ @response.stubs(:body).returns(<<-EOS)
13
+ <a href="/page">Link text</a>
14
+ EOS
15
+ @session.expects(:get_via_redirect).with("/page", {})
16
+ @session.clicks_link "Link text"
17
+ end
18
+
19
+ def test_should_click_get_links
20
+ @response.stubs(:body).returns(<<-EOS)
21
+ <a href="/page">Link text</a>
22
+ EOS
23
+ @session.expects(:get_via_redirect).with("/page", {})
24
+ @session.clicks_get_link "Link text"
25
+ end
26
+
27
+ def test_should_click_delete_links
28
+ @response.stubs(:body).returns(<<-EOS)
29
+ <a href="/page">Link text</a>
30
+ EOS
31
+ @session.expects(:delete_via_redirect).with("/page", {})
32
+ @session.clicks_delete_link "Link text"
33
+ end
34
+
35
+ def test_should_click_post_links
36
+ @response.stubs(:body).returns(<<-EOS)
37
+ <a href="/page">Link text</a>
38
+ EOS
39
+ @session.expects(:post_via_redirect).with("/page", {})
40
+ @session.clicks_post_link "Link text"
41
+ end
42
+
43
+ def test_should_click_put_links
44
+ @response.stubs(:body).returns(<<-EOS)
45
+ <a href="/page">Link text</a>
46
+ EOS
47
+ @session.expects(:put_via_redirect).with("/page", {})
48
+ @session.clicks_put_link "Link text"
49
+ end
50
+
51
+ def test_should_click_rails_javascript_links_with_authenticity_tokens
52
+ @response.stubs(:body).returns(<<-EOS)
53
+ <a href="/posts" onclick="var f = document.createElement('form');
54
+ f.style.display = 'none';
55
+ this.parentNode.appendChild(f);
56
+ f.method = 'POST';
57
+ f.action = this.href;
58
+ var s = document.createElement('input');
59
+ s.setAttribute('type', 'hidden');
60
+ s.setAttribute('name', 'authenticity_token');
61
+ s.setAttribute('value', 'aa79cb354597a60a3786e7e291ed4f74d77d3a62');
62
+ f.appendChild(s);
63
+ f.submit();
64
+ return false;">Posts</a>
65
+ EOS
66
+ @session.expects(:post_via_redirect).with("/posts", "authenticity_token" => "aa79cb354597a60a3786e7e291ed4f74d77d3a62")
67
+ @session.clicks_link "Posts"
68
+ end
69
+
70
+ def test_should_click_rails_javascript_delete_links
71
+ @response.stubs(:body).returns(<<-EOS)
72
+ <a href="/posts/1" onclick="var f = document.createElement('form');
73
+ f.style.display = 'none';
74
+ this.parentNode.appendChild(f);
75
+ f.method = 'POST';
76
+ f.action = this.href;
77
+ var m = document.createElement('input');
78
+ m.setAttribute('type', 'hidden');
79
+ m.setAttribute('name', '_method');
80
+ m.setAttribute('value', 'delete');
81
+ f.appendChild(m);
82
+ f.submit();
83
+ return false;">Delete</a>
84
+ EOS
85
+ @session.expects(:delete_via_redirect).with("/posts/1", {})
86
+ @session.clicks_link "Delete"
87
+ end
88
+
89
+ def test_should_click_rails_javascript_post_links
90
+ @response.stubs(:body).returns(<<-EOS)
91
+ <a href="/posts" onclick="var f = document.createElement('form');
92
+ f.style.display = 'none';
93
+ this.parentNode.appendChild(f);
94
+ f.method = 'POST';
95
+ f.action = this.href;
96
+ f.submit();
97
+ return false;">Posts</a>
98
+ EOS
99
+ @session.expects(:post_via_redirect).with("/posts", {})
100
+ @session.clicks_link "Posts"
101
+ end
102
+
103
+ def test_should_click_rails_javascript_put_links
104
+ @response.stubs(:body).returns(<<-EOS)
105
+ <a href="/posts" onclick="var f = document.createElement('form');
106
+ f.style.display = 'none';
107
+ this.parentNode.appendChild(f);
108
+ f.method = 'POST';
109
+ f.action = this.href;
110
+ var m = document.createElement('input');
111
+ m.setAttribute('type', 'hidden');
112
+ m.setAttribute('name', '_method');
113
+ m.setAttribute('value', 'put');
114
+ f.appendChild(m);
115
+ f.submit();
116
+ return false;">Put</a></h2>
117
+ EOS
118
+ @session.expects(:put_via_redirect).with("/posts", {})
119
+ @session.clicks_link "Put"
120
+ end
121
+
122
+ def test_should_assert_valid_response
123
+ @response.stubs(:body).returns(<<-EOS)
124
+ <a href="/page">Link text</a>
125
+ EOS
126
+ @session.expects(:assert_response).with(:success)
127
+ @session.clicks_link "Link text"
128
+ end
129
+
130
+ def test_should_not_be_case_sensitive
131
+ @response.stubs(:body).returns(<<-EOS)
132
+ <a href="/page">Link text</a>
133
+ EOS
134
+ @session.expects(:get_via_redirect).with("/page", {})
135
+ @session.clicks_link "LINK TEXT"
136
+ end
137
+
138
+ def test_should_match_link_substrings
139
+ @response.stubs(:body).returns(<<-EOS)
140
+ <a href="/page">This is some cool link text, isn't it?</a>
141
+ EOS
142
+ @session.expects(:get_via_redirect).with("/page", {})
143
+ @session.clicks_link "Link text"
144
+ end
145
+
146
+ def test_should_work_with_elements_in_the_link
147
+ @response.stubs(:body).returns(<<-EOS)
148
+ <a href="/page"><span>Link text</span></a>
149
+ EOS
150
+ @session.expects(:get_via_redirect).with("/page", {})
151
+ @session.clicks_link "Link text"
152
+ end
153
+
154
+ def test_should_match_the_first_matching_link
155
+ @response.stubs(:body).returns(<<-EOS)
156
+ <a href="/page1">Link text</a>
157
+ <a href="/page2">Link text</a>
158
+ EOS
159
+ @session.expects(:get_via_redirect).with("/page1", {})
160
+ @session.clicks_link "Link text"
161
+ end
162
+
163
+ def test_should_choose_the_shortest_link_text_match
164
+ @response.stubs(:body).returns(<<-EOS)
165
+ <a href="/page1">Linkerama</a>
166
+ <a href="/page2">Link</a>
167
+ EOS
168
+
169
+ @session.expects(:get_via_redirect).with("/page2", {})
170
+ @session.clicks_link "Link"
171
+ end
172
+
173
+ def test_should_click_link_within_a_selector
174
+ @response.stubs(:body).returns(<<-EOS)
175
+ <a href="/page1">Link</a>
176
+ <div id="container">
177
+ <a href="/page2">Link</a>
178
+ </div>
179
+ EOS
180
+
181
+ @session.expects(:get_via_redirect).with("/page2", {})
182
+ @session.clicks_link_within "#container", "Link"
183
+ end
184
+
185
+ def test_should_not_make_request_when_link_is_local_anchor
186
+ @response.stubs(:body).returns(<<-EOS)
187
+ <a href="#section-1">Jump to Section 1</a>
188
+ EOS
189
+ # Don't know why @session.expects(:get_via_redirect).never doesn't work here
190
+ @session.expects(:send).with('get_via_redirect', '#section-1', {}).never
191
+ @session.clicks_link "Jump to Section 1"
192
+ end
193
+ end
@@ -0,0 +1,139 @@
1
+ require File.dirname(__FILE__) + "/helper"
2
+
3
+ class FillsInTest < Test::Unit::TestCase
4
+ def setup
5
+ @session = ActionController::Integration::Session.new
6
+ @session.stubs(:assert_response)
7
+ @session.stubs(:get_via_redirect)
8
+ @session.stubs(:response).returns(@response=mock)
9
+ end
10
+
11
+ def test_should_work_with_textareas
12
+ @response.stubs(:body).returns(<<-EOS)
13
+ <form method="post" action="/login">
14
+ <label for="user_text">User Text</label>
15
+ <textarea id="user_text" name="user[text]"></textarea>
16
+ <input type="submit" />
17
+ </form>
18
+ EOS
19
+ @session.expects(:post_via_redirect).with("/login", "user" => {"text" => "filling text area"})
20
+ @session.fills_in "User Text", :with => "filling text area"
21
+ @session.clicks_button
22
+ end
23
+
24
+ def test_should_fail_if_input_not_found
25
+ @response.stubs(:body).returns(<<-EOS)
26
+ <form method="get" action="/login">
27
+ </form>
28
+ EOS
29
+ @session.expects(:flunk)
30
+ @session.fills_in "Email", :with => "foo@example.com"
31
+ end
32
+
33
+ def test_should_allow_overriding_default_form_values
34
+ @response.stubs(:body).returns(<<-EOS)
35
+ <form method="post" action="/login">
36
+ <label for="user_email">Email</label>
37
+ <input id="user_email" name="user[email]" value="test@example.com" type="text" />
38
+ <input type="submit" />
39
+ </form>
40
+ EOS
41
+ @session.expects(:post_via_redirect).with("/login", "user" => {"email" => "foo@example.com"})
42
+ @session.fills_in "user[email]", :with => "foo@example.com"
43
+ @session.clicks_button
44
+ end
45
+
46
+ def test_should_choose_the_closest_label_match
47
+ @response.stubs(:body).returns(<<-EOS)
48
+ <form method="post" action="/login">
49
+ <label for="user_mail1">Some other mail</label>
50
+ <input id="user_mail1" name="user[mail1]" type="text" />
51
+ <label for="user_mail2">Some mail</label>
52
+ <input id="user_mail2" name="user[mail2]" type="text" />
53
+ <input type="submit" />
54
+ </form>
55
+ EOS
56
+
57
+ @session.expects(:post_via_redirect).with("/login", "user" => {"mail1" => "", "mail2" => "value"})
58
+ @session.fills_in "Some", :with => "value"
59
+ @session.clicks_button
60
+ end
61
+
62
+ def test_should_choose_the_first_label_match_if_closest_is_a_tie
63
+ @response.stubs(:body).returns(<<-EOS)
64
+ <form method="post" action="/login">
65
+ <label for="user_mail1">Some mail one</label>
66
+ <input id="user_mail1" name="user[mail1]" type="text" />
67
+ <label for="user_mail2">Some mail two</label>
68
+ <input id="user_mail2" name="user[mail2]" type="text" />
69
+ <input type="submit" />
70
+ </form>
71
+ EOS
72
+
73
+ @session.expects(:post_via_redirect).with("/login", "user" => {"mail1" => "value", "mail2" => ""})
74
+ @session.fills_in "Some mail", :with => "value"
75
+ @session.clicks_button
76
+ end
77
+
78
+ def test_should_anchor_label_matches_to_start_of_label
79
+ @response.stubs(:body).returns(<<-EOS)
80
+ <form method="post" action="/login">
81
+ <label for="user_email">Some mail</label>
82
+ <input id="user_email" name="user[email]" value="test@example.com" type="text" />
83
+ </form>
84
+ EOS
85
+
86
+ assert_raises(RuntimeError) { @session.fills_in "mail", :with => "value" }
87
+ end
88
+
89
+ def test_should_anchor_label_matches_to_word_boundaries
90
+ @response.stubs(:body).returns(<<-EOS)
91
+ <form method="post" action="/login">
92
+ <label for="user_email">Emailtastic</label>
93
+ <input id="user_email" name="user[email]" value="test@example.com" type="text" />
94
+ </form>
95
+ EOS
96
+
97
+ assert_raises(RuntimeError) { @session.fills_in "Email", :with => "value" }
98
+ end
99
+
100
+ def test_should_work_with_inputs_nested_in_labels
101
+ @response.stubs(:body).returns(<<-EOS)
102
+ <form method="post" action="/login">
103
+ <label>
104
+ Email
105
+ <input id="user_email" name="user[email]" value="test@example.com" type="text" />
106
+ </label>
107
+ <input type="submit" />
108
+ </form>
109
+ EOS
110
+ @session.expects(:post_via_redirect).with("/login", "user" => {"email" => "foo@example.com"})
111
+ @session.fills_in "Email", :with => "foo@example.com"
112
+ @session.clicks_button
113
+ end
114
+
115
+ def test_should_work_with_full_input_names
116
+ @response.stubs(:body).returns(<<-EOS)
117
+ <form method="post" action="/login">
118
+ <input id="user_email" name="user[email]" type="text" />
119
+ <input type="submit" />
120
+ </form>
121
+ EOS
122
+ @session.expects(:post_via_redirect).with("/login", "user" => {"email" => "foo@example.com"})
123
+ @session.fills_in "user[email]", :with => "foo@example.com"
124
+ @session.clicks_button
125
+ end
126
+
127
+ def test_should_work_with_symbols
128
+ @response.stubs(:body).returns(<<-EOS)
129
+ <form method="post" action="/login">
130
+ <label for="user_email">Email</label>
131
+ <input id="user_email" name="user[email]" type="text" />
132
+ <input type="submit" />
133
+ </form>
134
+ EOS
135
+ @session.expects(:post_via_redirect).with("/login", "user" => {"email" => "foo@example.com"})
136
+ @session.fills_in :email, :with => "foo@example.com"
137
+ @session.clicks_button
138
+ end
139
+ end
@@ -0,0 +1,21 @@
1
+ require "rubygems"
2
+ require "test/unit"
3
+ # gem install redgreen for colored test output
4
+ begin require "redgreen" unless ENV['TM_CURRENT_LINE']; rescue LoadError; end
5
+ require "mocha"
6
+
7
+
8
+ require "active_support"
9
+
10
+ silence_warnings do
11
+ require "action_controller"
12
+ require "action_controller/integration"
13
+ end
14
+
15
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/webrat")
16
+
17
+ class ActionController::Integration::Session
18
+ def flunk(message)
19
+ raise message
20
+ end
21
+ end
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + "/helper"
2
+
3
+ RAILS_ROOT = "." unless defined?(RAILS_ROOT)
4
+
5
+ class ReloadsTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @session = ActionController::Integration::Session.new
9
+ @session.stubs(:assert_response)
10
+ @session.stubs(:get_via_redirect)
11
+ @response = mock
12
+ @session.stubs(:response).returns(@response)
13
+ @response.stubs(:body).returns("")
14
+ end
15
+
16
+ def test_should_reload_the_page
17
+ @session.expects(:get_via_redirect).with("/", {}).times(2)
18
+ @session.visits("/")
19
+ @session.reloads
20
+ end
21
+
22
+ def test_should_not_request_page_if_not_visited_any_page
23
+ @session.expects(:get_via_redirect).never
24
+ @session.reloads
25
+ end
26
+ end
@@ -0,0 +1,93 @@
1
+ require File.dirname(__FILE__) + "/helper"
2
+
3
+ class SelectsTest < Test::Unit::TestCase
4
+ def setup
5
+ @session = ActionController::Integration::Session.new
6
+ @session.stubs(:assert_response)
7
+ @session.stubs(:get_via_redirect)
8
+ @session.stubs(:response).returns(@response=mock)
9
+ end
10
+
11
+ def test_should_fail_if_option_not_found
12
+ @response.stubs(:body).returns(<<-EOS)
13
+ <form method="get" action="/login">
14
+ <select name="month"><option value="1">January</option></select>
15
+ </form>
16
+ EOS
17
+ @session.expects(:flunk)
18
+ @session.selects "February"
19
+ end
20
+
21
+ def test_should_fail_if_option_not_found_in_list_specified_by_element_name
22
+ @response.stubs(:body).returns(<<-EOS)
23
+ <form method="get" action="/login">
24
+ <select name="month"><option value="1">January</option></select>
25
+ <select name="year"><option value="2008">2008</option></select>
26
+ </form>
27
+ EOS
28
+ @session.expects(:flunk)
29
+ @session.selects "February", :from => "year"
30
+ end
31
+
32
+ def test_should_fail_if_specified_list_not_found
33
+ @response.stubs(:body).returns(<<-EOS)
34
+ <form method="get" action="/login">
35
+ <select name="month"><option value="1">January</option></select>
36
+ </form>
37
+ EOS
38
+ @session.expects(:flunk)
39
+ @session.selects "February", :from => "year"
40
+ end
41
+
42
+ def test_should_send_value_from_option
43
+ @response.stubs(:body).returns(<<-EOS)
44
+ <form method="post" action="/login">
45
+ <select name="month"><option value="1">January</option></select>
46
+ <input type="submit" />
47
+ </form>
48
+ EOS
49
+ @session.expects(:post_via_redirect).with("/login", "month" => "1")
50
+ @session.selects "January"
51
+ @session.clicks_button
52
+ end
53
+
54
+ def test_should_send_value_from_option_in_list_specified_by_name
55
+ @response.stubs(:body).returns(<<-EOS)
56
+ <form method="post" action="/login">
57
+ <select name="start_month"><option value="s1">January</option></select>
58
+ <select name="end_month"><option value="e1">January</option></select>
59
+ <input type="submit" />
60
+ </form>
61
+ EOS
62
+ @session.expects(:post_via_redirect).with("/login", "start_month" => "s1", "end_month" => "e1")
63
+ @session.selects "January", :from => "end_month"
64
+ @session.clicks_button
65
+ end
66
+
67
+ def test_should_send_value_from_option_in_list_specified_by_label
68
+ @response.stubs(:body).returns(<<-EOS)
69
+ <form method="post" action="/login">
70
+ <label for="start_month">Start Month</label>
71
+ <select id="start_month" name="start_month"><option value="s1">January</option></select>
72
+ <label for="end_month">End Month</label>
73
+ <select id="end_month" name="end_month"><option value="e1">January</option></select>
74
+ <input type="submit" />
75
+ </form>
76
+ EOS
77
+ @session.expects(:post_via_redirect).with("/login", "start_month" => "s1", "end_month" => "e1")
78
+ @session.selects "January", :from => "End Month"
79
+ @session.clicks_button
80
+ end
81
+
82
+ def test_should_use_option_text_if_no_value
83
+ @response.stubs(:body).returns(<<-EOS)
84
+ <form method="post" action="/login">
85
+ <select name="month"><option>January</option></select>
86
+ <input type="submit" />
87
+ </form>
88
+ EOS
89
+ @session.expects(:post_via_redirect).with("/login", "month" => "January")
90
+ @session.selects "January"
91
+ @session.clicks_button
92
+ end
93
+ end