forked from watir/watirspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser_spec.rb
269 lines (227 loc) · 8.93 KB
/
browser_spec.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# encoding: utf-8
require File.expand_path("../spec_helper", __FILE__)
describe "Browser" do
describe "#exists?" do
it "returns true if we are at a page" do
browser.goto(WatirSpec.url_for("non_control_elements.html"))
browser.should exist
end
not_compliant_on(:safariwatir) do
it "returns false after Browser#close" do
b = WatirSpec.new_browser
b.close
b.should_not exist
end
end
end
# this should be rewritten - the actual string returned varies alot between implementations
describe "#html" do
not_compliant_on :ie do
it "returns the DOM of the page as an HTML string" do
browser.goto(WatirSpec.url_for("right_click.html"))
html = browser.html.downcase # varies between browsers
html.should =~ /^<html/
html.should include('<meta ')
html.should include(' content="text/html; charset=utf-8"')
html.should include(' http-equiv="content-type"')
end
end
deviates_on :ie do
it "returns the DOM of the page as an HTML string" do
browser.goto(WatirSpec.url_for("right_click.html"))
html = browser.html.downcase # varies between browsers
html.should =~ /^<html/
html.should include('<meta content="text/html; charset=utf-8" http-equiv=content-type>')
end
end
end
describe "#title" do
it "returns the current page title" do
browser.goto(WatirSpec.url_for("non_control_elements.html"))
browser.title.should == "Non-control elements"
end
end
describe "#status" do
# for firefox, this needs to be enabled in
# Preferences -> Content -> Advanced -> Change status bar text
#
# for IE9, this needs to be enabled in
# View => Toolbars -> Status bar
not_compliant_on [:webdriver, :firefox], [:watir, :ie9] do
it "returns the current value of window.status" do
browser.goto(WatirSpec.url_for("non_control_elements.html"))
browser.execute_script "window.status = 'All done!';"
browser.status.should == "All done!"
end
end
end
describe "#text" do
it "returns the text of the page" do
browser.goto(WatirSpec.url_for("non_control_elements.html"))
browser.text.should include("Dubito, ergo cogito, ergo sum.")
end
it "returns the text also if the content-type is text/plain" do
# more specs for text/plain? what happens if we call other methods?
browser.goto(WatirSpec.url_for("plain_text", :needs_server => true))
browser.text.strip.should == 'This is text/plain'
end
end
describe "#url" do
it "returns the current url" do
browser.goto(WatirSpec.url_for("non_control_elements.html", :needs_server => true))
browser.url.should == WatirSpec.url_for("non_control_elements.html", :needs_server => true)
end
end
describe ".start" do
not_compliant_on(:webdriver, :safariwatir) {
it "goes to the given URL and return an instance of itself" do
url = WatirSpec.url_for("non_control_elements.html")
browser = WatirSpec.implementation.browser_class.start(WatirSpec.url_for("non_control_elements.html"))
browser.should be_instance_of(WatirSpec.implementation.browser_class)
browser.title.should == "Non-control elements"
browser.close
end
}
# we need to specify what browser to use
deviates_on(:webdriver) {
it "goes to the given URL and return an instance of itself" do
driver = (ENV['WATIR_WEBDRIVER_BROWSER'] || :firefox).to_sym
browser = Watir::Browser.start(WatirSpec.url_for("non_control_elements.html"), driver)
browser.should be_instance_of(Watir::Browser)
browser.title.should == "Non-control elements"
browser.close
end
}
end
describe "#goto" do
not_compliant_on [:webdriver, :ie] do
it "adds http:// to URLs with no URL scheme specified" do
url = WatirSpec.host[%r{http://(.*)}, 1]
url.should_not be_nil
browser.goto(url)
browser.url.should =~ %r[http://#{url}/?]
end
end
it "goes to the given url without raising errors" do
lambda { browser.goto(WatirSpec.url_for("non_control_elements.html")) }.should_not raise_error
end
it "goes to the url 'about:blank' without raising errors" do
lambda { browser.goto("about:blank") }.should_not raise_error
end
not_compliant_on :ie, [:webdriver, :safari] do
it "goes to a data URL scheme address without raising errors" do
lambda { browser.goto("data:text/html;content-type=utf-8,foobar") }.should_not raise_error
end
end
compliant_on :firefox do
it "goes to internal Firefox URL 'about:mozilla' without raising errors" do
lambda { browser.goto("about:mozilla") }.should_not raise_error
end
end
compliant_on :opera do
it "goes to internal Opera URL 'opera:config' without raising errors" do
lambda { browser.goto("opera:config") }.should_not raise_error
end
end
compliant_on :chrome do
it "goes to internal Chrome URL 'chrome://settings/browser' without raising errors" do
lambda { browser.goto("chrome://settings/browser") }.should_not raise_error
end
end
it "updates the page when location is changed with setTimeout + window.location" do
browser.goto(WatirSpec.url_for("timeout_window_location.html"))
sleep 1
browser.url.should include("non_control_elements.html")
end
end
describe "#refresh" do
it "refreshes the page" do
browser.goto(WatirSpec.url_for("non_control_elements.html"))
browser.span(:class, 'footer').click
browser.span(:class, 'footer').text.should include('Javascript')
browser.refresh
browser.span(:class, 'footer').text.should_not include('Javascript')
end
end
describe "#execute_script" do
it "executes the given JavaScript on the current page" do
browser.goto(WatirSpec.url_for("non_control_elements.html"))
browser.pre(:id, 'rspec').text.should_not == "javascript text"
browser.execute_script("document.getElementById('rspec').innerHTML = 'javascript text'")
browser.pre(:id, 'rspec').text.should == "javascript text"
end
it "executes the given JavaScript in the context of an anonymous function" do
browser.goto(WatirSpec.url_for("non_control_elements.html"))
browser.execute_script("1 + 1").should be_nil
browser.execute_script("return 1 + 1").should == 2
end
end
describe "#back and #forward" do
it "goes to the previous page" do
browser.goto WatirSpec.url_for("non_control_elements.html", :needs_server => true)
orig_url = browser.url
browser.goto(WatirSpec.url_for("tables.html", :needs_server => true))
new_url = browser.url
orig_url.should_not == new_url
browser.back
orig_url.should == browser.url
end
it "goes to the next page" do
urls = []
browser.goto WatirSpec.url_for("non_control_elements.html", :needs_server => true)
urls << browser.url
browser.goto WatirSpec.url_for("tables.html", :needs_server => true)
urls << browser.url
browser.back
browser.url.should == urls.first
browser.forward
browser.url.should == urls.last
end
it "navigates between several history items" do
urls = [ "non_control_elements.html",
"tables.html",
"forms_with_input_elements.html",
"definition_lists.html"
].map do |page|
browser.goto WatirSpec.url_for(page, :needs_server => true)
browser.url
end
3.times { browser.back }
browser.url.should == urls.first
2.times { browser.forward }
browser.url.should == urls[2]
end
end
describe "#add_checker" do
it "raises ArgumentError when not given any arguments" do
lambda { browser.add_checker }.should raise_error(ArgumentError)
end
it "runs the given proc on each page load" do
output = ''
proc = Proc.new { |browser| output << browser.text }
begin
browser.add_checker(proc)
browser.goto(WatirSpec.url_for("non_control_elements.html"))
output.should include('Dubito, ergo cogito, ergo sum')
ensure
browser.disable_checker(proc)
end
end
end
describe "#disable_checker" do
it "removes a previously added checker" do
output = ''
checker = lambda { |browser| output << browser.text }
browser.add_checker(checker)
browser.goto(WatirSpec.url_for("non_control_elements.html"))
output.should include('Dubito, ergo cogito, ergo sum')
browser.disable_checker(checker)
browser.goto(WatirSpec.url_for("definition_lists.html"))
output.should_not include('definition_lists')
end
end
it "raises UnknownObjectException when trying to access DOM elements on plain/text-page" do
browser.goto(WatirSpec.url_for("plain_text", :needs_server => true))
lambda { browser.div(:id, 'foo').id }.should raise_error(UnknownObjectException)
end
end