polonium 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/CHANGES +10 -0
  2. data/README +38 -0
  3. data/Rakefile +68 -0
  4. data/lib/polonium.rb +28 -0
  5. data/lib/polonium/configuration.rb +271 -0
  6. data/lib/polonium/driver.rb +155 -0
  7. data/lib/polonium/dsl/selenium_dsl.rb +33 -0
  8. data/lib/polonium/dsl/test_unit_dsl.rb +58 -0
  9. data/lib/polonium/element.rb +207 -0
  10. data/lib/polonium/extensions/module.rb +8 -0
  11. data/lib/polonium/extensions/testrunnermediator.rb +22 -0
  12. data/lib/polonium/mongrel_selenium_server_runner.rb +37 -0
  13. data/lib/polonium/page.rb +84 -0
  14. data/lib/polonium/selenium_helper.rb +5 -0
  15. data/lib/polonium/server_runner.rb +33 -0
  16. data/lib/polonium/tasks/selenium_test_task.rb +21 -0
  17. data/lib/polonium/test_case.rb +93 -0
  18. data/lib/polonium/wait_for.rb +40 -0
  19. data/lib/polonium/webrick_selenium_server_runner.rb +33 -0
  20. data/spec/polonium/extensions/module_spec.rb +29 -0
  21. data/spec/polonium/extensions/testrunnermediator_spec.rb +19 -0
  22. data/spec/polonium/mongrel_selenium_server_runner_spec.rb +35 -0
  23. data/spec/polonium/selenium_configuration_spec.rb +359 -0
  24. data/spec/polonium/selenium_driver_spec.rb +104 -0
  25. data/spec/polonium/selenium_element_spec.rb +551 -0
  26. data/spec/polonium/selenium_page_spec.rb +249 -0
  27. data/spec/polonium/selenium_server_runner_spec.rb +42 -0
  28. data/spec/polonium/selenium_test_case_class_method_spec.rb +41 -0
  29. data/spec/polonium/selenium_test_case_spec.rb +870 -0
  30. data/spec/polonium/selenium_test_case_spec_helper.rb +16 -0
  31. data/spec/polonium/webrick_selenium_server_runner_spec.rb +117 -0
  32. data/spec/spec_helper.rb +63 -0
  33. data/spec/spec_suite.rb +6 -0
  34. metadata +82 -0
@@ -0,0 +1,249 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ module Polonium
4
+ describe Page, :shared => true do
5
+ it_should_behave_like "Selenium"
6
+ include SeleniumTestCaseSpec
7
+ attr_reader :driver
8
+
9
+ before do
10
+ @driver = ::Polonium::Driver.new('http://test.host', 4000, "*firefox", 'http://test.host')
11
+ @page = Page.new(driver)
12
+ page_loaded
13
+ end
14
+
15
+ def page_loaded
16
+ stub(driver).get_eval(Page::PAGE_LOADED_COMMAND) {"true"}
17
+ end
18
+
19
+ def page_not_loaded
20
+ stub(driver).get_eval(Page::PAGE_LOADED_COMMAND) {"false"}
21
+ end
22
+ end
23
+
24
+ describe Page, "#initialize" do
25
+ it_should_behave_like "Polonium::Page"
26
+
27
+ it "sets the selenium object" do
28
+ @page.driver.should == driver
29
+ end
30
+ end
31
+
32
+ describe Page, "#open_and_wait" do
33
+ it_should_behave_like "Polonium::Page"
34
+
35
+ it "opens the url and waits for the page to load" do
36
+ mock(driver) do |m|
37
+ m.do_command("open", ["/users/list"]) {result}
38
+ m.do_command("waitForPageToLoad", [@page.default_timeout]) do
39
+ result
40
+ end
41
+ m.do_command("getTitle", []) do
42
+ result("Users in the project")
43
+ end
44
+ end
45
+ @page.open_and_wait("/users/list")
46
+ end
47
+
48
+ it "fails when title contains 'Exception caught'" do
49
+ mock(driver) do |m|
50
+ m.do_command("open", ["/users/list"]) {result}
51
+ m.do_command("waitForPageToLoad", [@page.default_timeout]) do
52
+ result
53
+ end
54
+ m.do_command("getTitle", []) do
55
+ result("Exception caught")
56
+ end
57
+ m.do_command("getHtmlSource", []) do
58
+ result("The page's html")
59
+ end
60
+ end
61
+ proc do
62
+ @page.open_and_wait("/users/list")
63
+ end.should raise_error(Test::Unit::AssertionFailedError)
64
+ end
65
+ end
66
+
67
+ describe Page, "#has_title" do
68
+ it_should_behave_like "Polonium::Page"
69
+
70
+ it "passes when title is expected" do
71
+ ticks = ["no page", "no page", "no page", "my page"]
72
+ mock(driver).get_title do
73
+ ticks.shift
74
+ end.times(4)
75
+ @page.assert_title("my page")
76
+ end
77
+
78
+ it "fails when element is not present" do
79
+ stub(driver).get_title {"no page"}
80
+ proc do
81
+ @page.assert_title("my page")
82
+ end.should raise_error("Expected title 'my page' but was 'no page' (after 5 sec)")
83
+ end
84
+ end
85
+
86
+ describe Page, "#title" do
87
+ it_should_behave_like "Polonium::Page"
88
+
89
+ it "returns true when passed in title is the page title" do
90
+ mock(driver).get_title {"my page"}
91
+ @page.title.should == "my page"
92
+ end
93
+
94
+ it "returns false when passed in title is not the page title" do
95
+ mock(driver).get_title {"no page"}
96
+ @page.title.should_not == "my page"
97
+ end
98
+ end
99
+
100
+ describe Page, "#assert_text_present" do
101
+ it_should_behave_like "Polonium::Page"
102
+
103
+ it "passes when title is expected" do
104
+ ticks = [false, false, false, true]
105
+ mock(driver).is_text_present("my page") do
106
+ ticks.shift
107
+ end.times(4)
108
+ @page.assert_text_present("my page")
109
+ end
110
+
111
+ it "fails when page is not loaded" do
112
+ page_not_loaded
113
+ proc do
114
+ @page.assert_text_present("my page")
115
+ end.should raise_error("Expected 'my page' to be present, but it wasn't (after 5 sec)")
116
+ end
117
+
118
+ it "fails when element is not present" do
119
+ stub(driver).is_text_present("my page") {false}
120
+ proc do
121
+ @page.assert_text_present("my page")
122
+ end.should raise_error("Expected 'my page' to be present, but it wasn't (after 5 sec)")
123
+ end
124
+ end
125
+
126
+ describe Page, "#is_text_present?" do
127
+ it_should_behave_like "Polonium::Page"
128
+
129
+ it "returns true when title is expected" do
130
+ mock(driver).is_text_present("my page") {true}
131
+ @page.is_text_present?("my page").should be_true
132
+ end
133
+
134
+ it "fails when element is not present" do
135
+ mock(driver).is_text_present("my page") {false}
136
+ @page.is_text_present?("my page").should be_false
137
+ end
138
+ end
139
+
140
+ describe Page, "#is_text_not_present" do
141
+ it_should_behave_like "Polonium::Page"
142
+
143
+ it "passes when text is not present" do
144
+ ticks = [true, true, true, false]
145
+ mock(driver).is_text_present("my page") do
146
+ ticks.shift
147
+ end.times(4)
148
+ @page.assert_text_not_present("my page")
149
+ end
150
+
151
+ it "fails when page not loaded" do
152
+ page_not_loaded
153
+ proc do
154
+ @page.assert_text_not_present("my page")
155
+ end.should raise_error("Expected 'my page' to be absent, but it wasn't (after 5 sec)")
156
+ end
157
+
158
+ it "fails when text is present" do
159
+ stub(driver).is_text_present("my page") {true}
160
+ proc do
161
+ @page.assert_text_not_present("my page")
162
+ end.should raise_error("Expected 'my page' to be absent, but it wasn't (after 5 sec)")
163
+ end
164
+ end
165
+
166
+ describe Page, "#is_text_not_present?" do
167
+ it_should_behave_like "Polonium::Page"
168
+
169
+ it "returns true when text is not present" do
170
+ mock(driver).is_text_present("my page") {false}
171
+ @page.is_text_not_present?("my page").should be_true
172
+ end
173
+
174
+ it "returns false when text is present" do
175
+ mock(driver).is_text_present("my page") {true}
176
+ @page.is_text_not_present?("my page").should be_false
177
+ end
178
+ end
179
+
180
+ describe Page, "#assert_location_ends_with" do
181
+ it_should_behave_like "Polonium::Page"
182
+
183
+ before do
184
+ @ends_with = "foobar.com?arg1=2"
185
+ end
186
+
187
+ it "passes when title is expected" do
188
+ ticks = [
189
+ "http://no.com",
190
+ "http://no.com",
191
+ "http://no.com",
192
+ "http://foobar.com?arg1=2"
193
+ ]
194
+ mock(driver).get_location do
195
+ ticks.shift
196
+ end.times(4)
197
+ @page.assert_location_ends_with(@ends_with)
198
+ end
199
+
200
+ it "fails when element is not present" do
201
+ stub(driver).get_location {"http://no.com"}
202
+ proc do
203
+ @page.assert_location_ends_with(@ends_with)
204
+ end.should raise_error("Expected 'http://no.com' to end with '#{@ends_with}' (after 5 sec)")
205
+ end
206
+ end
207
+
208
+ describe Page, "#location_ends_with?" do
209
+ it_should_behave_like "Polonium::Page"
210
+
211
+ before do
212
+ @ends_with = "foobar.com?arg1=2"
213
+ end
214
+
215
+ it "passes when title is expected" do
216
+ mock(driver).get_location {"http://foobar.com?arg1=2"}
217
+ @page.location_ends_with?(@ends_with).should be_true
218
+ end
219
+
220
+ it "fails when element is not present" do
221
+ mock(driver).get_location {"http://no.com"}
222
+ @page.location_ends_with?(@ends_with).should be_false
223
+ end
224
+ end
225
+
226
+ describe Page, "#page_loaded?" do
227
+ it_should_behave_like "Polonium::Page"
228
+
229
+ it "when page loaded command returns 'true', returns true" do
230
+ page_loaded
231
+ @page.should be_page_loaded
232
+ end
233
+
234
+ it "when page loaded command returns 'false', returns false" do
235
+ page_not_loaded
236
+ @page.should_not be_page_loaded
237
+ end
238
+ end
239
+
240
+ describe Page, "#method_missing" do
241
+ it_should_behave_like "Polonium::Page"
242
+
243
+ it "delegates command to the driver" do
244
+ @page.methods.should_not include('get_location')
245
+ mock(driver).get_location
246
+ @page.get_location
247
+ end
248
+ end
249
+ end
@@ -0,0 +1,42 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ module Polonium
4
+ describe ServerRunner do
5
+ before(:each) do
6
+ @runner = Polonium::ServerRunner.new
7
+ class << @runner
8
+ public :start_server, :stop_server
9
+ end
10
+ end
11
+
12
+ it "should initialize started? to be false" do
13
+ @runner.started?.should == false
14
+ end
15
+
16
+ it "start method should start new thread and set started" do
17
+ start_server_called = false
18
+ (class << @runner; self; end).class_eval do
19
+ define_method :start_server do; start_server_called = true; end
20
+ end
21
+ def @runner.stop_server; end
22
+ mock_thread_class = "mock_thread_class"
23
+ mock(mock_thread_class).start {|block| block.call}
24
+ @runner.thread_class = mock_thread_class
25
+
26
+ @runner.start
27
+ start_server_called.should equal(true)
28
+ @runner.started?.should equal(true)
29
+ end
30
+
31
+ it "stop method should set started? to false" do
32
+ def @runner.stop_server; end
33
+ @runner.instance_eval {@started = true}
34
+ @runner.stop
35
+ @runner.started?.should == false
36
+ end
37
+
38
+ it "start_server method should raise a NotImplementedError by default" do
39
+ proc {@runner.start_server}.should raise_error(NotImplementedError)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,41 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ module Polonium
4
+ describe TestCase, "Class methods" do
5
+ include SeleniumTestCaseSpec
6
+ it "should maintain a subclass array" do
7
+ test_class = Class.new
8
+ test_class.extend Polonium::TestCase::ClassMethods
9
+
10
+ subclass1 = Class.new(test_class)
11
+ subclass2 = Class.new(test_class)
12
+
13
+ test_class.subclasses.should == [subclass1, subclass2]
14
+ end
15
+
16
+ it "should not use transactional fixtures by default" do
17
+ Polonium::TestCase.use_transactional_fixtures.should == false
18
+ end
19
+
20
+ it "should use instantiated fixtures by default" do
21
+ Polonium::TestCase.use_instantiated_fixtures.should == true
22
+ end
23
+
24
+ class Parent < Polonium::TestCase
25
+ end
26
+ class Child1 < Parent
27
+ end
28
+ class Child2 < Parent
29
+ end
30
+ class Grandchild1 < Child1
31
+ end
32
+ class Grandchild2 < Child2
33
+ end
34
+ class Grandchild3 < Child2
35
+ end
36
+
37
+ it "should recursively gather all subclasses" do
38
+ Parent.all_descendant_classes.should == ([Child1, Grandchild1, Child2, Grandchild2, Grandchild3])
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,870 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ module Polonium
4
+ describe TestCase, :shared => true do
5
+ it_should_behave_like "Selenium"
6
+ include SeleniumTestCaseSpec
7
+ attr_reader :driver, :test_case, :configuration
8
+
9
+ before(:each) do
10
+ stub_selenium_configuration
11
+ @test_case = SeleniumTestCaseSpec::MySeleniumTestCase.new
12
+ @driver = ::Polonium::Driver.new('http://test.host', 4000, "*firefox", 'http://test.host')
13
+ test_case.selenium_driver = driver
14
+ stub(driver).do_command("getEval", [Page::PAGE_LOADED_COMMAND]) do
15
+ result(true)
16
+ end
17
+ end
18
+
19
+ def sample_locator
20
+ "sample_locator"
21
+ end
22
+
23
+ def sample_text
24
+ "test text"
25
+ end
26
+
27
+ def stub_selenium_configuration
28
+ @configuration = Configuration.new
29
+ configuration.external_app_server_host = "test.com"
30
+ configuration.external_app_server_port = 80
31
+
32
+ stub(Configuration.instance) {configuration}
33
+ end
34
+ end
35
+
36
+ describe TestCase, "#setup" do
37
+ it_should_behave_like "Polonium::TestCase"
38
+
39
+ it "should not allow transactional fixtures" do
40
+ test_case.class.use_transactional_fixtures = true
41
+
42
+ expected_message = "Cannot use transactional fixtures if ActiveRecord concurrency is turned on (which is required for Selenium tests to work)."
43
+ proc do
44
+ test_case.setup
45
+ end.should raise_error(RuntimeError, expected_message)
46
+ end
47
+ end
48
+
49
+ describe TestCase, "#wait_for" do
50
+ it_should_behave_like "Polonium::TestCase"
51
+
52
+ it "should pass when the block returns true within time limit" do
53
+ test_case.wait_for(:timeout => 2) do
54
+ true
55
+ end
56
+ end
57
+
58
+ it "should raise a AssertionFailedError when block times out" do
59
+ proc do
60
+ test_case.wait_for(:timeout => 2) {false}
61
+ end.should raise_error(Test::Unit::AssertionFailedError, "Timeout exceeded (after 2 sec)")
62
+ end
63
+ end
64
+
65
+ describe TestCase, "#default_timeout" do
66
+ it_should_behave_like "Polonium::TestCase"
67
+
68
+ it "default_timeout should be 20 seconds" do
69
+ test_case.default_timeout.should == 20000
70
+ end
71
+ end
72
+
73
+ describe TestCase, "#open_home_page" do
74
+ it_should_behave_like "Polonium::TestCase"
75
+
76
+ it "opens home page" do
77
+ mock(driver).open("http://localhost:4000")
78
+ test_case.open_home_page
79
+ end
80
+ end
81
+
82
+ describe TestCase, "#assert_title" do
83
+ it_should_behave_like "Polonium::TestCase"
84
+
85
+ it "when title is expected, passes" do
86
+ mock(driver).do_command("getTitle", []) {result("my page")}
87
+ test_case.assert_title("my page")
88
+ end
89
+
90
+ it "when title is not expected, fails" do
91
+ stub(driver).do_command("getTitle", []) {result("no page")}
92
+ proc do
93
+ test_case.assert_title("my page")
94
+ end.should raise_error(Test::Unit::AssertionFailedError)
95
+ end
96
+ end
97
+
98
+ describe TestCase, "#assert_text_present" do
99
+ it_should_behave_like "Polonium::TestCase"
100
+
101
+ before do
102
+ mock.proxy(Page).new(driver) do |page|
103
+ mock.proxy(page).assert_text_present("my page", {})
104
+ page
105
+ end
106
+ end
107
+
108
+ it "passes when text is in page" do
109
+ ticks = [false, false, false, true]
110
+ mock(driver).is_text_present("my page") do
111
+ ticks.shift
112
+ end.times(4)
113
+ test_case.assert_text_present("my page")
114
+ end
115
+
116
+ it "fails when text is not in page" do
117
+ stub(driver).is_text_present("my page") {false}
118
+ proc do
119
+ test_case.assert_text_present("my page")
120
+ end.should raise_error(Test::Unit::AssertionFailedError)
121
+ end
122
+ end
123
+
124
+ describe TestCase, "#assert_text_not_present" do
125
+ it_should_behave_like "Polonium::TestCase"
126
+
127
+ before do
128
+ mock.proxy(Page).new(driver) do |page|
129
+ mock.proxy(page).assert_text_not_present("my page", {})
130
+ page
131
+ end
132
+ end
133
+
134
+ it "passes when text is not in page" do
135
+ ticks = [true, true, true, false]
136
+ mock(driver).is_text_present("my page") do
137
+ ticks.shift
138
+ end.times(4)
139
+ test_case.assert_text_not_present("my page")
140
+ end
141
+
142
+ it "fails when text is in page" do
143
+ stub(driver).is_text_present("my page") {true}
144
+ proc do
145
+ test_case.assert_text_not_present("my page")
146
+ end.should raise_error(Test::Unit::AssertionFailedError)
147
+ end
148
+ end
149
+
150
+ describe TestCase, "#assert_location_ends_with" do
151
+ it_should_behave_like "Polonium::TestCase"
152
+
153
+ before do
154
+ @ends_with = "foobar.com?arg1=2"
155
+ mock.proxy(Page).new(driver) do |page|
156
+ mock.proxy(page).assert_location_ends_with(@ends_with, {})
157
+ page
158
+ end
159
+ end
160
+
161
+ it "passes when the url ends with the passed in value" do
162
+ ticks = [
163
+ "http://no.com",
164
+ "http://no.com",
165
+ "http://no.com",
166
+ "http://foobar.com?arg1=2"
167
+ ]
168
+ mock(driver).get_location do
169
+ ticks.shift
170
+ end.times(4)
171
+ test_case.assert_location_ends_with(@ends_with)
172
+ end
173
+
174
+ it "fails when the url does not end with the passed in value" do
175
+ stub(driver).get_location {"http://no.com"}
176
+ proc do
177
+ test_case.assert_location_ends_with(@ends_with)
178
+ end.should raise_error(Test::Unit::AssertionFailedError)
179
+ end
180
+ end
181
+
182
+ describe TestCase, "#assert_element_present" do
183
+ it_should_behave_like "Polonium::TestCase"
184
+
185
+ before do
186
+ mock.proxy(Element).new(driver, sample_locator) do |element|
187
+ mock.proxy(element).assert_element_present
188
+ element
189
+ end
190
+ end
191
+
192
+ it "passes when element is present" do
193
+ ticks = [false, false, false, true]
194
+ mock(driver).do_command("isElementPresent", [sample_locator]).times(4) do
195
+ result(ticks.shift)
196
+ end
197
+ test_case.assert_element_present(sample_locator)
198
+ end
199
+
200
+ it "fails when element is not present" do
201
+ stub(driver).do_command("isElementPresent", [sample_locator]) do
202
+ result(false)
203
+ end
204
+ proc do
205
+ test_case.assert_element_present(sample_locator)
206
+ end.should raise_error(Test::Unit::AssertionFailedError)
207
+ end
208
+ end
209
+
210
+ describe TestCase, "#assert_element_not_present" do
211
+ it_should_behave_like "Polonium::TestCase"
212
+
213
+ before do
214
+ mock.proxy(Element).new(driver, sample_locator) do |element|
215
+ mock.proxy(element).assert_element_not_present
216
+ element
217
+ end
218
+ end
219
+
220
+ it "passes when element is not present" do
221
+ ticks = [true, true, true, false]
222
+ mock(driver) do |o|
223
+ o.is_element_present(sample_locator) do
224
+ ticks.shift
225
+ end.times(4)
226
+ end
227
+ test_case.assert_element_not_present(sample_locator)
228
+ end
229
+
230
+ it "fails when element is present" do
231
+ stub(driver) do |o|
232
+ o.is_element_present(sample_locator) {true}
233
+ end
234
+ proc do
235
+ test_case.assert_element_not_present(sample_locator)
236
+ end.should raise_error(Test::Unit::AssertionFailedError)
237
+ end
238
+ end
239
+
240
+ describe TestCase, "#assert_value" do
241
+ it_should_behave_like "Polonium::TestCase"
242
+
243
+ before do
244
+ mock.proxy(Element).new(driver, sample_locator) do |element|
245
+ mock.proxy(element).assert_value("passed in value")
246
+ element
247
+ end
248
+ end
249
+
250
+ it "passes when value is expected" do
251
+ mock(driver) do |o|
252
+ o.is_element_present(sample_locator) {true}
253
+ o.get_value(sample_locator) {"passed in value"}
254
+ end
255
+ test_case.assert_value(sample_locator, "passed in value")
256
+ end
257
+
258
+ it "fails when value is not expected" do
259
+ stub(driver) do |o|
260
+ o.is_element_present(sample_locator) {true}
261
+ o.get_value(sample_locator) {"another value"}
262
+ end
263
+ proc do
264
+ test_case.assert_value(sample_locator, "passed in value")
265
+ end.should raise_error(Test::Unit::AssertionFailedError)
266
+ end
267
+ end
268
+
269
+ describe TestCase, "#assert_element_contains" do
270
+ it_should_behave_like "Polonium::TestCase"
271
+
272
+ before do
273
+ mock.proxy(Element).new(driver, sample_locator) do |element|
274
+ mock.proxy(element).assert_contains("passed in value", {})
275
+ element
276
+ end
277
+ @evaled_js = "this.page().findElement(\"#{sample_locator}\").innerHTML"
278
+ end
279
+
280
+ it "passes when text is in the element's inner_html" do
281
+ mock(driver) do |o|
282
+ o.is_element_present(sample_locator) {true}
283
+ o.get_eval(@evaled_js) do
284
+ "html passed in value html"
285
+ end
286
+ end
287
+ test_case.assert_element_contains(sample_locator, "passed in value")
288
+ end
289
+
290
+ it "fails when text is not in the element's inner_html" do
291
+ stub(driver) do |o|
292
+ o.is_element_present(sample_locator) {true}
293
+ o.get_eval(@evaled_js) {"another value"}
294
+ end
295
+ proc do
296
+ test_case.assert_element_contains(sample_locator, "passed in value")
297
+ end.should raise_error(Test::Unit::AssertionFailedError)
298
+ end
299
+ end
300
+
301
+ describe TestCase, "#element_does_not_contain_text" do
302
+ it_should_behave_like "Polonium::TestCase"
303
+
304
+ it "when element is not on the page, returns true" do
305
+ locator = "id=element_id"
306
+ expected_text = "foobar"
307
+ mock(driver).is_element_present(locator) {false}
308
+
309
+ test_case.element_does_not_contain_text(locator, expected_text).should == true
310
+ end
311
+
312
+ it "when element is on page and inner_html does not contain text, returns true" do
313
+ locator = "id=element_id"
314
+ inner_html = "Some text that does not contain the expected_text"
315
+ expected_text = "foobar"
316
+ mock(driver).do_command("isElementPresent", [locator]) do
317
+ result(true)
318
+ end
319
+ mock(driver).do_command("getEval", [driver.inner_html_js(locator)]) do
320
+ inner_html
321
+ end
322
+
323
+ test_case.element_does_not_contain_text(locator, expected_text).should == true
324
+ end
325
+
326
+ it "when element is on page and inner_html does contain text, returns false" do
327
+ locator = "id=element_id"
328
+ inner_html = "foobar foobar foobar"
329
+ expected_text = "foobar"
330
+ mock(driver).do_command("isElementPresent", [locator]) do
331
+ result(true)
332
+ end
333
+ mock(driver).do_command("getEval", [driver.inner_html_js(locator)]) do
334
+ inner_html
335
+ end
336
+
337
+ test_case.element_does_not_contain_text(locator, expected_text).should == false
338
+ end
339
+ end
340
+
341
+ describe TestCase, "#assert_element_does_not_contain" do
342
+ it_should_behave_like "Polonium::TestCase"
343
+
344
+ before do
345
+ mock.proxy(Element).new(driver, sample_locator) do |element|
346
+ mock.proxy(element).assert_does_not_contain("passed in value", {})
347
+ element
348
+ end
349
+ @evaled_js = "this.page().findElement(\"#{sample_locator}\").innerHTML"
350
+ end
351
+
352
+ it "passes when text is not in the element's inner_html" do
353
+ mock(driver) do |o|
354
+ o.is_element_present(sample_locator) {true}
355
+ o.get_eval(@evaled_js) do
356
+ "another value"
357
+ end
358
+ end
359
+ test_case.assert_element_does_not_contain(sample_locator, "passed in value")
360
+ end
361
+
362
+ it "fails when text is in the element's inner_html" do
363
+ stub(driver) do |o|
364
+ o.is_element_present(sample_locator) {true}
365
+ o.get_eval(@evaled_js) {"html passed in value html"}
366
+ end
367
+ proc do
368
+ test_case.assert_element_does_not_contain(sample_locator, "passed in value")
369
+ end.should raise_error(Test::Unit::AssertionFailedError)
370
+ end
371
+ end
372
+
373
+ describe TestCase, "#assert_contains_in_order" do
374
+ it_should_behave_like "Polonium::TestCase"
375
+
376
+ it "when text is in order, it succeeds" do
377
+ mock.proxy(Element).new(driver, sample_locator) do |element|
378
+ mock.proxy(element).assert_contains_in_order("one", "two", "three")
379
+ element
380
+ end
381
+ stub(@driver).get_text(sample_locator).returns("one\ntwo\nthree\n")
382
+ stub(@driver).is_element_present(sample_locator) {true}
383
+
384
+ test_case.assert_contains_in_order sample_locator, "one", "two", "three"
385
+ end
386
+ end
387
+
388
+ describe TestCase, "#assert_attribute" do
389
+ it_should_behave_like "Polonium::TestCase"
390
+
391
+ before do
392
+ mock.proxy(Element).new(driver, sample_locator) do |element|
393
+ mock.proxy(element).assert_attribute("passed in value")
394
+ element
395
+ end
396
+ end
397
+
398
+ it "passes when attribute is expected" do
399
+ mock(driver) do |o|
400
+ o.is_element_present(sample_locator) {true}
401
+ o.get_attribute(sample_locator) {"passed in value"}
402
+ end
403
+ test_case.assert_attribute(sample_locator, "passed in value")
404
+ end
405
+
406
+ it "fails when attribute is not expected" do
407
+ stub(driver) do |o|
408
+ o.is_element_present(sample_locator) {true}
409
+ o.get_attribute(sample_locator) {"another value"}
410
+ end
411
+ proc do
412
+ test_case.assert_attribute(sample_locator, "passed in value")
413
+ end.should raise_error(Test::Unit::AssertionFailedError)
414
+ end
415
+ end
416
+
417
+ describe TestCase, "#assert_selected" do
418
+ it_should_behave_like "Polonium::TestCase"
419
+
420
+ before do
421
+ mock.proxy(Element).new(driver, sample_locator) do |element|
422
+ mock.proxy(element).assert_selected("passed_in_element")
423
+ element
424
+ end
425
+ end
426
+
427
+ it "passes when selected is expected" do
428
+ mock(driver) do |o|
429
+ o.is_element_present(sample_locator) {true}
430
+ o.get_selected_label(sample_locator) {"passed_in_element"}
431
+ end
432
+ test_case.assert_selected(sample_locator, "passed_in_element")
433
+ end
434
+
435
+ it "fails when selected is not expected" do
436
+ stub(driver) do |o|
437
+ o.is_element_present(sample_locator) {true}
438
+ o.get_selected_label(sample_locator) {"another_element"}
439
+ end
440
+ proc do
441
+ test_case.assert_selected(sample_locator, "passed_in_element")
442
+ end.should raise_error(Test::Unit::AssertionFailedError)
443
+ end
444
+ end
445
+
446
+ describe TestCase, "#assert_checked" do
447
+ it_should_behave_like "Polonium::TestCase"
448
+
449
+ before do
450
+ mock.proxy(Element).new(driver, sample_locator) do |element|
451
+ mock.proxy(element).assert_checked
452
+ element
453
+ end
454
+ end
455
+
456
+ it "passes when checked" do
457
+ mock(driver) do |o|
458
+ o.is_element_present(sample_locator) {true}
459
+ o.is_checked(sample_locator) {true}
460
+ end
461
+ test_case.assert_checked(sample_locator)
462
+ end
463
+
464
+ it "fails when not checked" do
465
+ stub(driver) do |driver|
466
+ driver.is_element_present(sample_locator) {true}
467
+ driver.is_checked(sample_locator) {false}
468
+ end
469
+ proc do
470
+ test_case.assert_checked(sample_locator)
471
+ end.should raise_error(Test::Unit::AssertionFailedError)
472
+ end
473
+ end
474
+
475
+ describe TestCase, "#assert_not_checked" do
476
+ it_should_behave_like "Polonium::TestCase"
477
+
478
+ before do
479
+ mock.proxy(Element).new(driver, sample_locator) do |element|
480
+ mock.proxy(element).assert_not_checked
481
+ element
482
+ end
483
+ end
484
+
485
+ it "passes when not checked" do
486
+ mock(driver) do |driver|
487
+ driver.is_element_present(sample_locator) {true}
488
+ driver.is_checked(sample_locator) {false}
489
+ end
490
+ test_case.assert_not_checked(sample_locator)
491
+ end
492
+
493
+ it "fails when checked" do
494
+ stub(driver) do |driver|
495
+ driver.is_element_present(sample_locator) {true}
496
+ driver.is_checked(sample_locator) {true}
497
+ end
498
+ proc do
499
+ test_case.assert_not_checked(sample_locator)
500
+ end.should raise_error(Test::Unit::AssertionFailedError)
501
+ end
502
+ end
503
+
504
+ describe TestCase, "#assert_text" do
505
+ it_should_behave_like "Polonium::TestCase"
506
+
507
+ before do
508
+ mock.proxy(Element).new(driver, sample_locator) do |element|
509
+ mock.proxy(element).assert_text("expected text")
510
+ element
511
+ end
512
+ end
513
+
514
+ it "passes when text is expected" do
515
+ mock(driver) do |o|
516
+ o.is_element_present(sample_locator) {true}
517
+ o.get_text(sample_locator) {"expected text"}
518
+ end
519
+ test_case.assert_text(sample_locator, "expected text")
520
+ end
521
+
522
+ it "fails when text is not expected" do
523
+ stub(driver) do |o|
524
+ o.is_element_present(sample_locator) {true}
525
+ o.get_text(sample_locator) {"unexpected text"}
526
+ end
527
+ proc do
528
+ test_case.assert_text(sample_locator, "expected text")
529
+ end.should raise_error(Test::Unit::AssertionFailedError)
530
+ end
531
+ end
532
+
533
+ describe TestCase, "#assert_visible" do
534
+ it_should_behave_like "Polonium::TestCase"
535
+
536
+ before do
537
+ mock.proxy(Element).new(driver, sample_locator) do |element|
538
+ mock.proxy(element).assert_visible
539
+ element
540
+ end
541
+ end
542
+
543
+ it "passes when element is visible" do
544
+ mock(driver).is_element_present(sample_locator) {true}
545
+ mock(driver).is_visible(sample_locator) {true}
546
+
547
+ test_case.assert_visible(sample_locator)
548
+ end
549
+
550
+ it "fails when element is not visible" do
551
+ mock(driver).is_element_present(sample_locator) {true}
552
+ stub(driver).is_visible.returns {false}
553
+
554
+ proc {
555
+ test_case.assert_visible(sample_locator)
556
+ }.should raise_error(Test::Unit::AssertionFailedError)
557
+ end
558
+ end
559
+
560
+ describe TestCase, "#assert_not_visible" do
561
+ it_should_behave_like "Polonium::TestCase"
562
+
563
+ before do
564
+ mock.proxy(Element).new(driver, sample_locator) do |element|
565
+ mock.proxy(element).assert_not_visible
566
+ element
567
+ end
568
+ end
569
+
570
+ it "passes when element is present and is not visible" do
571
+ mock(driver).is_element_present(sample_locator) {true}
572
+ mock(driver).is_visible(sample_locator) {false}
573
+
574
+ test_case.assert_not_visible(sample_locator)
575
+ end
576
+
577
+ it "fails when element is visible" do
578
+ mock(driver).is_element_present(sample_locator) {true}
579
+ stub(driver).is_visible(sample_locator) {true}
580
+
581
+ proc {
582
+ test_case.assert_not_visible(sample_locator)
583
+ }.should raise_error(Test::Unit::AssertionFailedError)
584
+ end
585
+ end
586
+
587
+ describe TestCase, "#assert_next_sibling" do
588
+ it_should_behave_like "Polonium::TestCase"
589
+
590
+ before do
591
+ mock.proxy(Element).new(driver, sample_locator) do |element|
592
+ mock.proxy(element).assert_next_sibling("next_sibling")
593
+ element
594
+ end
595
+ @evaled_js = "this.page().findElement('#{sample_locator}').nextSibling.id"
596
+ end
597
+
598
+ it "passes when passed next sibling id" do
599
+ mock(driver) do |o|
600
+ o.is_element_present(sample_locator) {true}
601
+ o.get_eval(@evaled_js) {"next_sibling"}
602
+ end
603
+ test_case.assert_next_sibling(sample_locator, "next_sibling")
604
+ end
605
+
606
+ it "fails when not passed next sibling id" do
607
+ stub(driver) do |o|
608
+ o.is_element_present(sample_locator) {true}
609
+ o.get_eval(@evaled_js) {"wrong_sibling"}
610
+ end
611
+ proc do
612
+ test_case.assert_next_sibling(sample_locator, "next_sibling")
613
+ end.should raise_error(Test::Unit::AssertionFailedError)
614
+ end
615
+ end
616
+
617
+ describe TestCase, "#assert_contains_in_order" do
618
+ it_should_behave_like "Polonium::TestCase"
619
+
620
+ before do
621
+ mock.proxy(Element).new(driver, sample_locator)
622
+ @evaled_js = "this.page().findElement('#{sample_locator}').nextSibling.id"
623
+ end
624
+
625
+ it "passes when text is in order" do
626
+ mock(driver) do |o|
627
+ o.is_element_present(sample_locator) {true}
628
+ o.get_text(sample_locator) {"one\ntwo\nthree\n"}
629
+ end
630
+ test_case.assert_contains_in_order sample_locator, "one", "two", "three"
631
+ end
632
+
633
+ it "fails when element is present and text is not in order" do
634
+ stub(driver) do |o|
635
+ o.is_element_present(sample_locator) {true}
636
+ o.get_text(sample_locator) {"<html>one\ntext not in order\n</html>"}
637
+ end
638
+ proc do
639
+ test_case.assert_contains_in_order sample_locator, "one", "two", "three"
640
+ end.should raise_error(Test::Unit::AssertionFailedError)
641
+ end
642
+ end
643
+
644
+ describe TestCase, "#type" do
645
+ it_should_behave_like "Polonium::TestCase"
646
+
647
+ it "types when element is present and types" do
648
+ is_element_present_results = [false, true]
649
+ mock(driver).do_command("isElementPresent", ["id=foobar"]).twice do
650
+ result(is_element_present_results.shift)
651
+ end
652
+ mock(driver).do_command("type", ["id=foobar", "The Text"]) do
653
+ result()
654
+ end
655
+
656
+ test_case.type "id=foobar", "The Text"
657
+ end
658
+
659
+ it "fails when element is not present" do
660
+ mock(driver).do_command("isElementPresent", ["id=foobar"]).times(4) do
661
+ result(false)
662
+ end
663
+ dont_allow(driver).do_command("type", ["id=foobar", "The Text"])
664
+
665
+ proc {
666
+ test_case.type "id=foobar", "The Text"
667
+ }.should raise_error(Test::Unit::AssertionFailedError)
668
+ end
669
+ end
670
+
671
+ describe TestCase, "#click" do
672
+ it_should_behave_like "Polonium::TestCase"
673
+
674
+ it "click when element is present and types" do
675
+ is_element_present_results = [false, true]
676
+ mock(driver).do_command("isElementPresent", ["id=foobar"]).twice do
677
+ result(is_element_present_results.shift)
678
+ end
679
+ mock(driver).do_command("click", ["id=foobar"]) {result}
680
+
681
+ test_case.click "id=foobar"
682
+ end
683
+
684
+ it "fails when element is not present" do
685
+ is_element_present_results = [false, false, false, false]
686
+ mock(driver).do_command("isElementPresent", ["id=foobar"]).times(4) do
687
+ result(is_element_present_results.shift)
688
+ end
689
+ dont_allow(driver).do_command("click", [])
690
+
691
+ proc {
692
+ test_case.click "id=foobar"
693
+ }.should raise_error(Test::Unit::AssertionFailedError)
694
+ end
695
+ end
696
+
697
+ describe TestCase, "#select" do
698
+ it_should_behave_like "Polonium::TestCase"
699
+
700
+ it "types when element is present and types" do
701
+ is_element_present_results = [false, true]
702
+ mock(driver).do_command("isElementPresent", ["id=foobar"]).twice do
703
+ result is_element_present_results.shift
704
+ end
705
+ mock(driver).do_command("select", ["id=foobar", "value=3"]) {result}
706
+
707
+ test_case.select "id=foobar", "value=3"
708
+ end
709
+
710
+ it "fails when element is not present" do
711
+ mock(driver).do_command("isElementPresent", ["id=foobar"]).times(4) do
712
+ result false
713
+ end
714
+ dont_allow(driver).do_command("select", ["id=foobar", "value=3"])
715
+
716
+ proc {
717
+ test_case.select "id=foobar", "value=3"
718
+ }.should raise_error(Test::Unit::AssertionFailedError)
719
+ end
720
+ end
721
+
722
+ describe TestCase, "#wait_for_and_click" do
723
+ it_should_behave_like "Polonium::TestCase"
724
+
725
+ it "click when element is present and types" do
726
+ is_element_present_results = [false, true]
727
+ mock(driver).do_command("isElementPresent", ["id=foobar"]).twice do
728
+ result is_element_present_results.shift
729
+ end
730
+ mock(driver).do_command("click", ["id=foobar"]) {result}
731
+
732
+ test_case.wait_for_and_click "id=foobar"
733
+ end
734
+
735
+ it "fails when element is not present" do
736
+ is_element_present_results = [false, false, false, false]
737
+ mock(driver).is_element_present("id=foobar").times(4) do
738
+ is_element_present_results.shift
739
+ end
740
+ dont_allow(driver).click
741
+
742
+ proc {
743
+ test_case.wait_for_and_click "id=foobar"
744
+ }.should raise_error(Test::Unit::AssertionFailedError)
745
+ end
746
+ end
747
+
748
+ describe TestCase, "#page" do
749
+ it_should_behave_like "Polonium::TestCase"
750
+
751
+ it "returns page" do
752
+ test_case.page.should == Page.new(driver)
753
+ end
754
+ end
755
+
756
+ describe "TestCase in test browser mode and test fails" do
757
+ it_should_behave_like "Polonium::TestCase"
758
+
759
+ it "should stop driver when configuration says to stop test" do
760
+ test_case.configuration = configuration = Polonium::Configuration.new
761
+ configuration.test_browser_mode
762
+
763
+ stub(test_case).passed? {false}
764
+ configuration.keep_browser_open_on_failure = false
765
+
766
+ mock(driver).stop.once
767
+ test_case.selenium_driver = driver
768
+
769
+ test_case.teardown
770
+ end
771
+
772
+ it "should not stop driver when configuration says not to stop test" do
773
+ test_case.configuration = "Polonium::Configuration"
774
+ mock(test_case.configuration).test_browser_mode?.returns(true)
775
+
776
+ stub(test_case).passed? {false}
777
+ mock(test_case.configuration).stop_driver?(false) {false}
778
+
779
+ test_case.selenium_driver = driver
780
+
781
+ test_case.teardown
782
+ end
783
+ end
784
+
785
+ describe "TestCase in test browser mode and test pass" do
786
+ it_should_behave_like "Polonium::TestCase"
787
+
788
+ it "should stop driver when configuration says to stop test" do
789
+ test_case.configuration = "Polonium::Configuration"
790
+ mock(test_case.configuration).test_browser_mode?.returns(true)
791
+
792
+ stub(test_case).passed?.returns(true)
793
+ mock(test_case.configuration).stop_driver?(true) {true}
794
+
795
+ mock(driver).stop.once
796
+ test_case.selenium_driver = driver
797
+
798
+ test_case.teardown
799
+ end
800
+
801
+ it "should not stop driver when configuration says not to stop test" do
802
+ test_case.configuration = "Polonium::Configuration"
803
+ mock(test_case.configuration).test_browser_mode?.returns(true)
804
+
805
+ stub(test_case).passed?.returns(true)
806
+ mock(test_case.configuration).stop_driver?(true) {false}
807
+
808
+ test_case.selenium_driver = driver
809
+
810
+ test_case.teardown
811
+ end
812
+ end
813
+
814
+ describe "TestCase not in suite browser mode" do
815
+ it_should_behave_like "Polonium::TestCase"
816
+
817
+ it "should not stop driver when tests fail" do
818
+ test_case.configuration = "Polonium::Configuration"
819
+ mock(test_case.configuration).test_browser_mode? {false}
820
+
821
+ def test_case.passed?;
822
+ false;
823
+ end
824
+
825
+ test_case.selenium_driver = driver
826
+
827
+ test_case.teardown
828
+ end
829
+
830
+ it "should stop driver when tests pass" do
831
+ test_case.configuration = "Polonium::Configuration"
832
+ mock(test_case.configuration).test_browser_mode? {false}
833
+
834
+ stub(test_case).passed?.returns(true)
835
+
836
+ test_case.selenium_driver = driver
837
+
838
+ test_case.teardown
839
+ end
840
+ end
841
+
842
+ describe "TestCase in test browser mode and test pass" do
843
+ it_should_behave_like "Polonium::TestCase"
844
+
845
+ it "should stop driver when configuration says to stop test" do
846
+ test_case.configuration = "Polonium::Configuration"
847
+ mock(test_case.configuration).test_browser_mode?.returns(true)
848
+
849
+ stub(test_case).passed?.returns(true)
850
+ mock(test_case.configuration).stop_driver?(true) {true}
851
+
852
+ mock(driver).stop.once
853
+ test_case.selenium_driver = driver
854
+
855
+ test_case.teardown
856
+ end
857
+
858
+ it "should not stop driver when configuration says not to stop test" do
859
+ test_case.configuration = "Polonium::Configuration"
860
+ mock(test_case.configuration).test_browser_mode? {true}
861
+
862
+ stub(test_case).passed?.returns(true)
863
+ mock(test_case.configuration).stop_driver?(true) {false}
864
+
865
+ test_case.selenium_driver = driver
866
+
867
+ test_case.teardown
868
+ end
869
+ end
870
+ end