watir 6.0.0.beta5 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +5 -2
  3. data/CHANGES.md +10 -1
  4. data/Gemfile +4 -0
  5. data/README.md +37 -23
  6. data/Rakefile +6 -10
  7. data/appveyor.yml +13 -0
  8. data/lib/watir.rb +20 -21
  9. data/lib/watir/alert.rb +21 -5
  10. data/lib/watir/browser.rb +9 -3
  11. data/lib/watir/elements/checkbox.rb +0 -1
  12. data/lib/watir/elements/element.rb +92 -50
  13. data/lib/watir/elements/form.rb +1 -2
  14. data/lib/watir/elements/iframe.rb +6 -2
  15. data/lib/watir/elements/image.rb +1 -1
  16. data/lib/watir/elements/option.rb +0 -1
  17. data/lib/watir/elements/radio.rb +0 -1
  18. data/lib/watir/elements/select.rb +1 -10
  19. data/lib/watir/extensions/select_text.rb +1 -1
  20. data/lib/watir/legacy_wait.rb +126 -0
  21. data/lib/watir/locators/text_field/locator.rb +0 -3
  22. data/lib/watir/locators/text_field/selector_builder.rb +1 -4
  23. data/lib/watir/locators/text_field/selector_builder/xpath.rb +0 -2
  24. data/lib/watir/locators/text_field/validator.rb +1 -9
  25. data/lib/watir/row_container.rb +1 -1
  26. data/lib/watir/user_editable.rb +7 -5
  27. data/lib/watir/wait.rb +56 -135
  28. data/lib/watir/wait/timer.rb +29 -2
  29. data/lib/watir/window.rb +11 -1
  30. data/lib/watirspec.rb +81 -0
  31. data/lib/watirspec/guards.rb +58 -0
  32. data/lib/watirspec/implementation.rb +33 -0
  33. data/lib/watirspec/rake_tasks.rb +115 -0
  34. data/lib/watirspec/runner.rb +53 -0
  35. data/lib/watirspec/server.rb +99 -0
  36. data/lib/watirspec/server/app.rb +62 -0
  37. data/spec/browser_spec.rb +5 -5
  38. data/spec/click_spec.rb +1 -1
  39. data/spec/container_spec.rb +1 -1
  40. data/spec/element_locator_spec.rb +1 -1
  41. data/spec/element_spec.rb +20 -34
  42. data/spec/implementation_spec.rb +24 -0
  43. data/spec/input_spec.rb +1 -1
  44. data/spec/spec_helper.rb +35 -3
  45. data/spec/special_chars_spec.rb +1 -1
  46. data/spec/watirspec/.gitignore +2 -0
  47. data/spec/watirspec/after_hooks_spec.rb +179 -0
  48. data/spec/watirspec/alert_spec.rb +101 -0
  49. data/spec/watirspec/browser_spec.rb +291 -0
  50. data/spec/watirspec/cookies_spec.rb +147 -0
  51. data/spec/watirspec/drag_and_drop_spec.rb +49 -0
  52. data/spec/watirspec/element_hidden_spec.rb +65 -0
  53. data/spec/watirspec/elements/area_spec.rb +76 -0
  54. data/spec/watirspec/elements/areas_spec.rb +42 -0
  55. data/spec/watirspec/elements/button_spec.rb +270 -0
  56. data/spec/watirspec/elements/buttons_spec.rb +55 -0
  57. data/spec/watirspec/elements/checkbox_spec.rb +279 -0
  58. data/spec/watirspec/elements/checkboxes_spec.rb +44 -0
  59. data/spec/watirspec/elements/collections_spec.rb +16 -0
  60. data/spec/watirspec/elements/dd_spec.rb +126 -0
  61. data/spec/watirspec/elements/dds_spec.rb +42 -0
  62. data/spec/watirspec/elements/del_spec.rb +128 -0
  63. data/spec/watirspec/elements/dels_spec.rb +40 -0
  64. data/spec/watirspec/elements/div_spec.rb +199 -0
  65. data/spec/watirspec/elements/divs_spec.rb +42 -0
  66. data/spec/watirspec/elements/dl_spec.rb +144 -0
  67. data/spec/watirspec/elements/dls_spec.rb +43 -0
  68. data/spec/watirspec/elements/dt_spec.rb +126 -0
  69. data/spec/watirspec/elements/dts_spec.rb +42 -0
  70. data/spec/watirspec/elements/element_spec.rb +361 -0
  71. data/spec/watirspec/elements/elements_spec.rb +15 -0
  72. data/spec/watirspec/elements/em_spec.rb +100 -0
  73. data/spec/watirspec/elements/ems_spec.rb +43 -0
  74. data/spec/watirspec/elements/filefield_spec.rb +181 -0
  75. data/spec/watirspec/elements/filefields_spec.rb +43 -0
  76. data/spec/watirspec/elements/font_spec.rb +29 -0
  77. data/spec/watirspec/elements/form_spec.rb +70 -0
  78. data/spec/watirspec/elements/forms_spec.rb +44 -0
  79. data/spec/watirspec/elements/frame_spec.rb +121 -0
  80. data/spec/watirspec/elements/frames_spec.rb +41 -0
  81. data/spec/watirspec/elements/hidden_spec.rb +102 -0
  82. data/spec/watirspec/elements/hiddens_spec.rb +43 -0
  83. data/spec/watirspec/elements/hn_spec.rb +96 -0
  84. data/spec/watirspec/elements/hns_spec.rb +38 -0
  85. data/spec/watirspec/elements/iframe_spec.rb +174 -0
  86. data/spec/watirspec/elements/iframes_spec.rb +47 -0
  87. data/spec/watirspec/elements/image_spec.rb +164 -0
  88. data/spec/watirspec/elements/images_spec.rb +40 -0
  89. data/spec/watirspec/elements/ins_spec.rb +129 -0
  90. data/spec/watirspec/elements/inses_spec.rb +40 -0
  91. data/spec/watirspec/elements/label_spec.rb +79 -0
  92. data/spec/watirspec/elements/labels_spec.rb +40 -0
  93. data/spec/watirspec/elements/li_spec.rb +115 -0
  94. data/spec/watirspec/elements/link_spec.rb +174 -0
  95. data/spec/watirspec/elements/links_spec.rb +44 -0
  96. data/spec/watirspec/elements/lis_spec.rb +42 -0
  97. data/spec/watirspec/elements/map_spec.rb +79 -0
  98. data/spec/watirspec/elements/maps_spec.rb +41 -0
  99. data/spec/watirspec/elements/meta_spec.rb +23 -0
  100. data/spec/watirspec/elements/metas_spec.rb +40 -0
  101. data/spec/watirspec/elements/ol_spec.rb +89 -0
  102. data/spec/watirspec/elements/ols_spec.rb +40 -0
  103. data/spec/watirspec/elements/option_spec.rb +152 -0
  104. data/spec/watirspec/elements/p_spec.rb +115 -0
  105. data/spec/watirspec/elements/pre_spec.rb +115 -0
  106. data/spec/watirspec/elements/pres_spec.rb +40 -0
  107. data/spec/watirspec/elements/ps_spec.rb +40 -0
  108. data/spec/watirspec/elements/radio_spec.rb +263 -0
  109. data/spec/watirspec/elements/radios_spec.rb +43 -0
  110. data/spec/watirspec/elements/select_list_spec.rb +379 -0
  111. data/spec/watirspec/elements/select_lists_spec.rb +46 -0
  112. data/spec/watirspec/elements/span_spec.rb +129 -0
  113. data/spec/watirspec/elements/spans_spec.rb +40 -0
  114. data/spec/watirspec/elements/strong_spec.rb +92 -0
  115. data/spec/watirspec/elements/strongs_spec.rb +43 -0
  116. data/spec/watirspec/elements/table_nesting_spec.rb +51 -0
  117. data/spec/watirspec/elements/table_spec.rb +146 -0
  118. data/spec/watirspec/elements/tables_spec.rb +42 -0
  119. data/spec/watirspec/elements/tbody_spec.rb +96 -0
  120. data/spec/watirspec/elements/tbodys_spec.rb +62 -0
  121. data/spec/watirspec/elements/td_spec.rb +71 -0
  122. data/spec/watirspec/elements/tds_spec.rb +53 -0
  123. data/spec/watirspec/elements/text_field_spec.rb +302 -0
  124. data/spec/watirspec/elements/text_fields_spec.rb +44 -0
  125. data/spec/watirspec/elements/textarea_spec.rb +26 -0
  126. data/spec/watirspec/elements/textareas_spec.rb +24 -0
  127. data/spec/watirspec/elements/tfoot_spec.rb +91 -0
  128. data/spec/watirspec/elements/tfoots_spec.rb +68 -0
  129. data/spec/watirspec/elements/thead_spec.rb +91 -0
  130. data/spec/watirspec/elements/theads_spec.rb +68 -0
  131. data/spec/watirspec/elements/tr_spec.rb +80 -0
  132. data/spec/watirspec/elements/trs_spec.rb +61 -0
  133. data/spec/watirspec/elements/ul_spec.rb +79 -0
  134. data/spec/watirspec/elements/uls_spec.rb +39 -0
  135. data/spec/watirspec/html/alerts.html +12 -0
  136. data/spec/watirspec/html/aria_attributes.html +9 -0
  137. data/spec/watirspec/html/class_locator.html +8 -0
  138. data/spec/watirspec/html/clicks.html +19 -0
  139. data/spec/watirspec/html/closeable.html +13 -0
  140. data/spec/watirspec/html/collections.html +15 -0
  141. data/spec/watirspec/html/css/jquery-ui-1.8.17.custom.css +287 -0
  142. data/spec/watirspec/html/data_attributes.html +9 -0
  143. data/spec/watirspec/html/definition_lists.html +48 -0
  144. data/spec/watirspec/html/drag_and_drop.html +106 -0
  145. data/spec/watirspec/html/font.html +10 -0
  146. data/spec/watirspec/html/forms_with_input_elements.html +170 -0
  147. data/spec/watirspec/html/frame_1.html +22 -0
  148. data/spec/watirspec/html/frame_2.html +16 -0
  149. data/spec/watirspec/html/frames.html +11 -0
  150. data/spec/watirspec/html/hover.html +12 -0
  151. data/spec/watirspec/html/iframe_1.html +22 -0
  152. data/spec/watirspec/html/iframes.html +12 -0
  153. data/spec/watirspec/html/images.html +28 -0
  154. data/spec/watirspec/html/images/1.gif +0 -0
  155. data/spec/watirspec/html/images/2.gif +0 -0
  156. data/spec/watirspec/html/images/3.gif +0 -0
  157. data/spec/watirspec/html/images/button.png +0 -0
  158. data/spec/watirspec/html/images/circle.png +0 -0
  159. data/spec/watirspec/html/images/map.gif +0 -0
  160. data/spec/watirspec/html/images/map2.gif +0 -0
  161. data/spec/watirspec/html/images/minus.gif +0 -0
  162. data/spec/watirspec/html/images/originaltriangle.png +0 -0
  163. data/spec/watirspec/html/images/plus.gif +0 -0
  164. data/spec/watirspec/html/images/square.png +0 -0
  165. data/spec/watirspec/html/images/triangle.png +0 -0
  166. data/spec/watirspec/html/inner_outer.html +5 -0
  167. data/spec/watirspec/html/javascript/helpers.js +16 -0
  168. data/spec/watirspec/html/javascript/jquery-1.7.1.min.js +4 -0
  169. data/spec/watirspec/html/javascript/jquery-ui-1.8.17.custom.min.js +68 -0
  170. data/spec/watirspec/html/keylogger.html +15 -0
  171. data/spec/watirspec/html/modal_dialog.html +9 -0
  172. data/spec/watirspec/html/multiple_ids.html +14 -0
  173. data/spec/watirspec/html/nested_frame_1.html +1 -0
  174. data/spec/watirspec/html/nested_frame_2.html +9 -0
  175. data/spec/watirspec/html/nested_frame_3.html +14 -0
  176. data/spec/watirspec/html/nested_frames.html +10 -0
  177. data/spec/watirspec/html/nested_iframe_2.html +12 -0
  178. data/spec/watirspec/html/nested_iframes.html +9 -0
  179. data/spec/watirspec/html/nested_tables.html +203 -0
  180. data/spec/watirspec/html/non_control_elements.html +135 -0
  181. data/spec/watirspec/html/removed_element.html +24 -0
  182. data/spec/watirspec/html/right_click.html +11 -0
  183. data/spec/watirspec/html/special_chars.html +12 -0
  184. data/spec/watirspec/html/tables.html +121 -0
  185. data/spec/watirspec/html/timeout_window_location.html +19 -0
  186. data/spec/watirspec/html/uneven_table.html +20 -0
  187. data/spec/watirspec/html/wait.html +65 -0
  188. data/spec/watirspec/html/watirspec.css +0 -0
  189. data/spec/watirspec/html/window_switching.html +12 -0
  190. data/spec/watirspec/relaxed_locate_spec.rb +214 -0
  191. data/spec/watirspec/screenshot_spec.rb +29 -0
  192. data/spec/watirspec/wait_spec.rb +304 -0
  193. data/spec/watirspec/window_switching_spec.rb +411 -0
  194. data/spec/{implementation.rb → watirspec_helper.rb} +13 -13
  195. data/support/appveyor.cmd +9 -0
  196. data/support/doctest_helper.rb +5 -1
  197. data/support/travis.sh +0 -1
  198. data/watir.gemspec +3 -4
  199. metadata +323 -30
  200. data/.gitmodules +0 -3
@@ -0,0 +1,101 @@
1
+ require "watirspec_helper"
2
+
3
+ describe 'Alert API' do
4
+ bug "https://github.com/detro/ghostdriver/issues/20", :phantomjs do
5
+ not_compliant_on :safari do
6
+ before do
7
+ browser.goto WatirSpec.url_for("alerts.html")
8
+ end
9
+
10
+ after do
11
+ browser.alert.ok if browser.alert.exists?
12
+ end
13
+
14
+ context 'alert' do
15
+ describe '#text' do
16
+ it 'returns text of alert' do
17
+ browser.button(id: 'alert').click
18
+ expect(browser.alert.text).to include('ok')
19
+ end
20
+ end
21
+
22
+ describe '#exists?' do
23
+ it 'returns false if alert is not present' do
24
+ expect(browser.alert).to_not exist
25
+ end
26
+
27
+ it 'returns true if alert is present' do
28
+ browser.button(id: 'alert').click
29
+ browser.wait_until(timeout: 10) { browser.alert.exists? }
30
+ end
31
+ end
32
+
33
+ describe '#ok' do
34
+ it 'closes alert' do
35
+ browser.button(id: 'alert').click
36
+ browser.alert.ok
37
+ expect(browser.alert).to_not exist
38
+ end
39
+ end
40
+
41
+ bug "https://code.google.com/p/chromedriver/issues/detail?id=26", [:chrome, :macosx] do
42
+ describe '#close' do
43
+ it 'closes alert' do
44
+ browser.button(id: 'alert').click
45
+ browser.alert.close
46
+ expect(browser.alert).to_not exist
47
+ end
48
+ end
49
+ end
50
+
51
+ not_compliant_on :relaxed_locate do
52
+ describe 'wait_until_present' do
53
+ it 'waits until alert is present and goes on' do
54
+ browser.button(id: 'timeout-alert').click
55
+ browser.alert.wait_until_present.ok
56
+
57
+ expect(browser.alert).to_not exist
58
+ end
59
+
60
+ it 'raises error if alert is not present after timeout' do
61
+ expect {
62
+ browser.alert.wait_until_present.ok
63
+ }.to raise_error(Watir::Wait::TimeoutError)
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ context 'confirm' do
70
+ describe '#ok' do
71
+ it 'accepts confirm' do
72
+ browser.button(id: 'confirm').click
73
+ browser.alert.ok
74
+ expect(browser.button(id: 'confirm').value).to eq "true"
75
+ end
76
+ end
77
+
78
+ describe '#close' do
79
+ it 'cancels confirm' do
80
+ browser.button(id: 'confirm').click
81
+ browser.alert.close
82
+ expect(browser.button(id: 'confirm').value).to eq "false"
83
+ end
84
+ end
85
+ end
86
+
87
+ context 'prompt' do
88
+ describe '#set' do
89
+ bug "https://bugzilla.mozilla.org/show_bug.cgi?id=1255906", :firefox do
90
+ it 'enters text to prompt' do
91
+ browser.button(id: 'prompt').click
92
+ browser.alert.set 'My Name'
93
+ browser.alert.ok
94
+ expect(browser.button(id: 'prompt').value).to eq 'My Name'
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,291 @@
1
+ require "watirspec_helper"
2
+
3
+ describe "Browser" do
4
+
5
+ describe "#exists?" do
6
+ after do
7
+ browser.window(index: 0).use
8
+ browser.windows[1..-1].each(&:close)
9
+ end
10
+
11
+ it "returns true if we are at a page" do
12
+ browser.goto(WatirSpec.url_for("non_control_elements.html"))
13
+ expect(browser).to exist
14
+ end
15
+
16
+ bug "https://bugzilla.mozilla.org/show_bug.cgi?id=1223277", :firefox do
17
+ it "returns false if window is closed" do
18
+ browser.goto WatirSpec.url_for("window_switching.html")
19
+ browser.a(id: "open").click
20
+ Watir::Wait.until { browser.windows.size == 2 }
21
+ browser.window(title: "closeable window").use
22
+ browser.a(id: "close").click
23
+ Watir::Wait.until { browser.windows.size == 1 }
24
+ expect(browser.exists?).to be false
25
+ end
26
+ end
27
+
28
+ bug "https://bugzilla.mozilla.org/show_bug.cgi?id=1290814", :firefox do
29
+ it "returns false after Browser#close" do
30
+ b = WatirSpec.new_browser
31
+ b.close
32
+ expect(b).to_not exist
33
+ end
34
+ end
35
+ end
36
+
37
+ # this should be rewritten - the actual string returned varies a lot between implementations
38
+ describe "#html" do
39
+ it "returns the DOM of the page as an HTML string" do
40
+ browser.goto(WatirSpec.url_for("right_click.html"))
41
+ html = browser.html.downcase # varies between browsers
42
+
43
+ expect(html).to match(/^<html/)
44
+ expect(html).to include('<meta ')
45
+ expect(html).to include(' content="text/html; charset=utf-8"')
46
+
47
+ not_compliant_on :internet_explorer do
48
+ expect(html).to include(' http-equiv="content-type"')
49
+ end
50
+
51
+ deviates_on :internet_explorer do
52
+ expect(html).to include(' http-equiv=content-type')
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "#title" do
58
+ it "returns the current page title" do
59
+ browser.goto(WatirSpec.url_for("non_control_elements.html"))
60
+ expect(browser.title).to eq "Non-control elements"
61
+ end
62
+ end
63
+
64
+ describe "#status" do
65
+ # for Firefox, this needs to be enabled in
66
+ # Preferences -> Content -> Advanced -> Change status bar text
67
+ #
68
+ # for IE9, this needs to be enabled in
69
+ # View => Toolbars -> Status bar
70
+ it "returns the current value of window.status" do
71
+ browser.goto(WatirSpec.url_for("non_control_elements.html"))
72
+
73
+ browser.execute_script "window.status = 'All done!';"
74
+ expect(browser.status).to eq "All done!"
75
+ end
76
+ end
77
+
78
+ bug "Capitalization bug fixed in upcoming release", %i(remote firefox) do
79
+ describe "#name" do
80
+ it "returns browser name" do
81
+ expect(browser.name).to eq WatirSpec.implementation.browser_args[1][:desired_capabilities].browser_name.to_sym
82
+ end
83
+ end
84
+ end
85
+
86
+ describe "#text" do
87
+ it "returns the text of the page" do
88
+ browser.goto(WatirSpec.url_for("non_control_elements.html"))
89
+ expect(browser.text).to include("Dubito, ergo cogito, ergo sum.")
90
+ end
91
+
92
+ it "returns the text also if the content-type is text/plain" do
93
+ # more specs for text/plain? what happens if we call other methods?
94
+ browser.goto(WatirSpec.url_for("plain_text"))
95
+ expect(browser.text.strip).to eq 'This is text/plain'
96
+ end
97
+
98
+ it "returns text of top most browsing context" do
99
+ browser.goto(WatirSpec.url_for("nested_iframes.html"))
100
+ browser.iframe(id: 'two').h3.exists?
101
+ expect(browser.text).to eq 'Top Layer'
102
+ end
103
+ end
104
+
105
+ describe "#url" do
106
+ it "returns the current url" do
107
+ browser.goto(WatirSpec.url_for("non_control_elements.html"))
108
+ expect(browser.url.casecmp(WatirSpec.url_for("non_control_elements.html"))).to eq 0
109
+ end
110
+
111
+ it "always returns top url" do
112
+ browser.goto(WatirSpec.url_for("frames.html"))
113
+ browser.frame.body.exists? # switches to frame
114
+ expect(browser.url.casecmp(WatirSpec.url_for("frames.html"))).to eq 0
115
+ end
116
+ end
117
+
118
+ describe "#title" do
119
+ it "returns the current title" do
120
+ browser.goto(WatirSpec.url_for("non_control_elements.html"))
121
+ expect(browser.title).to eq "Non-control elements"
122
+ end
123
+
124
+ it "always returns top title" do
125
+ browser.goto(WatirSpec.url_for("frames.html"))
126
+ browser.element(tag_name: 'title').text
127
+ browser.frame.body.exists? # switches to frame
128
+ expect(browser.title).to eq "Frames"
129
+ end
130
+ end
131
+
132
+ describe ".start" do
133
+ bug "https://bugzilla.mozilla.org/show_bug.cgi?id=1290814", :firefox do
134
+ it "goes to the given URL and return an instance of itself" do
135
+ driver, args = WatirSpec.implementation.browser_args
136
+ browser = Watir::Browser.start(WatirSpec.url_for("non_control_elements.html"), driver, args)
137
+
138
+ expect(browser).to be_instance_of(Watir::Browser)
139
+ expect(browser.title).to eq "Non-control elements"
140
+ browser.close
141
+ end
142
+ end
143
+ end
144
+
145
+ describe "#goto" do
146
+ not_compliant_on :internet_explorer do
147
+ it "adds http:// to URLs with no URL scheme specified" do
148
+ url = WatirSpec.host[%r{http://(.*)}, 1]
149
+ expect(url).to_not be_nil
150
+ browser.goto(url)
151
+ expect(browser.url).to match(%r[http://#{url}/?])
152
+ end
153
+ end
154
+
155
+ it "goes to the given url without raising errors" do
156
+ expect { browser.goto(WatirSpec.url_for("non_control_elements.html")) }.to_not raise_error
157
+ end
158
+
159
+ it "goes to the url 'about:blank' without raising errors" do
160
+ expect { browser.goto("about:blank") }.to_not raise_error
161
+ end
162
+
163
+ not_compliant_on :internet_explorer, :safari do
164
+ it "goes to a data URL scheme address without raising errors" do
165
+ expect { browser.goto("data:text/html;content-type=utf-8,foobar") }.to_not raise_error
166
+ end
167
+ end
168
+
169
+ compliant_on :ff_legacy do
170
+ it "goes to internal Firefox URL 'about:mozilla' without raising errors" do
171
+ expect { browser.goto("about:mozilla") }.to_not raise_error
172
+ end
173
+ end
174
+
175
+ compliant_on :opera do
176
+ it "goes to internal Opera URL 'opera:config' without raising errors" do
177
+ expect { browser.goto("opera:config") }.to_not raise_error
178
+ end
179
+ end
180
+
181
+ compliant_on :chrome do
182
+ it "goes to internal Chrome URL 'chrome://settings/browser' without raising errors" do
183
+ expect { browser.goto("chrome://settings/browser") }.to_not raise_error
184
+ end
185
+ end
186
+
187
+ it "updates the page when location is changed with setTimeout + window.location" do
188
+ browser.goto(WatirSpec.url_for("timeout_window_location.html"))
189
+ Watir::Wait.while { browser.url.include? 'timeout_window_location.html' }
190
+ expect(browser.url).to include("non_control_elements.html")
191
+ end
192
+ end
193
+
194
+ describe "#refresh" do
195
+ it "refreshes the page" do
196
+ browser.goto(WatirSpec.url_for("non_control_elements.html"))
197
+ browser.span(class: 'footer').click
198
+ expect(browser.span(class: 'footer').text).to include('Javascript')
199
+ browser.refresh
200
+ browser.span(class: 'footer').wait_until_present
201
+ expect(browser.span(class: 'footer').text).to_not include('Javascript')
202
+ end
203
+ end
204
+
205
+ describe "#execute_script" do
206
+ before { browser.goto(WatirSpec.url_for("non_control_elements.html")) }
207
+
208
+ it "executes the given JavaScript on the current page" do
209
+ expect(browser.pre(id: 'rspec').text).to_not eq "javascript text"
210
+ browser.execute_script("document.getElementById('rspec').innerHTML = 'javascript text'")
211
+ expect(browser.pre(id: 'rspec').text).to eq "javascript text"
212
+ end
213
+
214
+ it "executes the given JavaScript in the context of an anonymous function" do
215
+ expect(browser.execute_script("1 + 1")).to be_nil
216
+ expect(browser.execute_script("return 1 + 1")).to eq 2
217
+ end
218
+
219
+ it "returns correct Ruby objects" do
220
+ expect(browser.execute_script("return {a: 1, \"b\": 2}")).to eq Hash["a" => 1, "b" => 2]
221
+ expect(browser.execute_script("return [1, 2, \"3\"]")).to match_array([1, 2, "3"])
222
+ expect(browser.execute_script("return 1.2 + 1.3")).to eq 2.5
223
+ expect(browser.execute_script("return 2 + 2")).to eq 4
224
+ expect(browser.execute_script("return \"hello\"")).to eq "hello"
225
+ expect(browser.execute_script("return")).to be_nil
226
+ expect(browser.execute_script("return null")).to be_nil
227
+ expect(browser.execute_script("return undefined")).to be_nil
228
+ expect(browser.execute_script("return true")).to be true
229
+ expect(browser.execute_script("return false")).to be false
230
+ end
231
+
232
+ it "works correctly with multi-line strings and special characters" do
233
+ expect(browser.execute_script("//multiline rocks!
234
+ var a = 22; // comment on same line
235
+ /* more
236
+ comments */
237
+ var b = '33';
238
+ var c = \"44\";
239
+ return a + b + c")).to eq "223344"
240
+ end
241
+ end
242
+
243
+ not_compliant_on :safari do
244
+ describe "#back and #forward" do
245
+ it "goes to the previous page" do
246
+ browser.goto WatirSpec.url_for("non_control_elements.html")
247
+ orig_url = browser.url
248
+ browser.goto WatirSpec.url_for("tables.html")
249
+ new_url = browser.url
250
+ expect(orig_url).to_not eq new_url
251
+ browser.back
252
+ expect(orig_url).to eq browser.url
253
+ end
254
+
255
+ it "goes to the next page" do
256
+ urls = []
257
+ browser.goto WatirSpec.url_for("non_control_elements.html")
258
+ urls << browser.url
259
+ browser.goto WatirSpec.url_for("tables.html")
260
+ urls << browser.url
261
+
262
+ browser.back
263
+ expect(browser.url).to eq urls.first
264
+ browser.forward
265
+ expect(browser.url).to eq urls.last
266
+ end
267
+
268
+ it "navigates between several history items" do
269
+ urls = ["non_control_elements.html",
270
+ "tables.html",
271
+ "forms_with_input_elements.html",
272
+ "definition_lists.html"
273
+ ].map do |page|
274
+ browser.goto WatirSpec.url_for(page)
275
+ browser.url
276
+ end
277
+
278
+ 3.times { browser.back }
279
+ expect(browser.url).to eq urls.first
280
+ 2.times { browser.forward }
281
+ expect(browser.url).to eq urls[2]
282
+ end
283
+ end
284
+ end
285
+
286
+ it "raises UnknownObjectException when trying to access DOM elements on plain/text-page" do
287
+ browser.goto(WatirSpec.url_for("plain_text"))
288
+ expect { browser.div(id: 'foo').id }.to raise_unknown_object_exception
289
+ end
290
+
291
+ end
@@ -0,0 +1,147 @@
1
+ require "watirspec_helper"
2
+
3
+ bug "https://github.com/ariya/phantomjs/issues/13115", :phantomjs do
4
+ describe "Browser#cookies" do
5
+ after { browser.cookies.clear }
6
+
7
+ it 'gets an empty list of cookies' do
8
+ browser.goto WatirSpec.url_for 'collections.html' # no cookie set.
9
+ expect(browser.cookies.to_a).to eq []
10
+ end
11
+
12
+ it "gets any cookies set" do
13
+ browser.goto set_cookie_url
14
+
15
+ verify_cookies_count 1
16
+
17
+ cookie = browser.cookies.to_a.first
18
+ expect(cookie[:name]).to eq 'monster'
19
+ expect(cookie[:value]).to eq '1'
20
+ end
21
+
22
+ describe '#[]' do
23
+ before do
24
+ browser.goto set_cookie_url
25
+ verify_cookies_count 1
26
+ end
27
+
28
+ it 'returns cookie by symbol name' do
29
+ cookie = browser.cookies[:monster]
30
+ expect(cookie[:name]).to eq('monster')
31
+ expect(cookie[:value]).to eq('1')
32
+ end
33
+
34
+ it 'returns cookie by string name' do
35
+ cookie = browser.cookies['monster']
36
+ expect(cookie[:name]).to eq('monster')
37
+ expect(cookie[:value]).to eq('1')
38
+ end
39
+
40
+ it 'returns nil if there is no cookie with such name' do
41
+ expect(browser.cookies[:non_monster]).to eq(nil)
42
+ end
43
+ end
44
+
45
+ not_compliant_on :internet_explorer do
46
+ it 'adds a cookie' do
47
+ browser.goto set_cookie_url
48
+ verify_cookies_count 1
49
+
50
+ browser.cookies.add 'foo', 'bar'
51
+ verify_cookies_count 2
52
+
53
+ compliant_on :safari do
54
+ $browser.close
55
+ $browser = WatirSpec.new_browser
56
+ end
57
+ end
58
+ end
59
+
60
+ # TODO - Split this up into multiple tests or figure out which parts are not compliant
61
+ not_compliant_on :chrome, :internet_explorer, :safari, :firefox do
62
+ it 'adds a cookie with options' do
63
+ browser.goto set_cookie_url
64
+
65
+ expires = Time.now + 10000
66
+ options = {path: "/set_cookie",
67
+ secure: true,
68
+ expires: expires}
69
+
70
+ browser.cookies.add 'a', 'b', options
71
+ cookie = browser.cookies.to_a.find { |e| e[:name] == 'a' }
72
+ expect(cookie).to_not be_nil
73
+
74
+ expect(cookie[:name]).to eq 'a'
75
+ expect(cookie[:value]).to eq 'b'
76
+
77
+ expect(cookie[:path]).to eq "/set_cookie"
78
+ expect(cookie[:secure]).to be true
79
+
80
+ expect(cookie[:expires]).to be_kind_of(Time)
81
+ # a few ms slack
82
+ expect((cookie[:expires]).to_i).to be_within(2).of(expires.to_i)
83
+ end
84
+ end
85
+
86
+ not_compliant_on :internet_explorer do
87
+ it 'removes a cookie' do
88
+ browser.goto set_cookie_url
89
+ verify_cookies_count 1
90
+
91
+ browser.cookies.delete 'monster'
92
+ verify_cookies_count 0
93
+ end
94
+
95
+ bug "https://code.google.com/p/selenium/issues/detail?id=5487", :safari do
96
+ it 'clears all cookies' do
97
+ browser.goto set_cookie_url
98
+ browser.cookies.add 'foo', 'bar'
99
+ verify_cookies_count 2
100
+
101
+ browser.cookies.clear
102
+ verify_cookies_count 0
103
+ end
104
+ end
105
+ end
106
+
107
+ not_compliant_on :internet_explorer do
108
+ let(:file) { "#{Dir.tmpdir}/cookies" }
109
+
110
+ before do
111
+ browser.goto set_cookie_url
112
+ browser.cookies.save file
113
+ end
114
+
115
+ describe '#save' do
116
+ it 'saves cookies to file' do
117
+ expect(IO.read(file)).to eq(browser.cookies.to_a.to_yaml)
118
+ end
119
+ end
120
+
121
+ describe '#load' do
122
+ it 'loads cookies from file' do
123
+ browser.cookies.clear
124
+ browser.cookies.load file
125
+ expected = browser.cookies.to_a
126
+ actual = YAML.load(IO.read(file))
127
+
128
+ # https://code.google.com/p/selenium/issues/detail?id=6834
129
+ expected.each { |cookie| cookie.delete(:expires) }
130
+ actual.each { |cookie| cookie.delete(:expires) }
131
+
132
+ expect(actual).to eq(expected)
133
+ end
134
+ end
135
+ end
136
+
137
+ def set_cookie_url
138
+ # add timestamp to url to avoid caching in IE8
139
+ WatirSpec.url_for('set_cookie/index.html') + "?t=#{Time.now.to_i + Time.now.usec}"
140
+ end
141
+
142
+ def verify_cookies_count expected_size
143
+ cookies = browser.cookies.to_a
144
+ expect(cookies.size).to eq(expected_size), "expected #{expected_size} cookies, got #{cookies.size}: #{cookies.inspect}"
145
+ end
146
+ end
147
+ end