This repository has been archived by the owner on Jun 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
button_spec.rb
271 lines (226 loc) · 10.5 KB
/
button_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
270
271
# encoding: utf-8
require File.expand_path("../spec_helper", __FILE__)
describe "Button" do
before :each do
browser.goto(WatirSpec.url_for("forms_with_input_elements.html"))
end
# Exists method
describe "#exists?" do
it "returns true if the button exists (tag = :input)" do
expect(browser.button(id: "new_user_submit")).to exist
expect(browser.button(id: /new_user_submit/)).to exist
expect(browser.button(name: "new_user_reset")).to exist
expect(browser.button(name: /new_user_reset/)).to exist
expect(browser.button(value: "Button")).to exist
expect(browser.button(value: /Button/)).to exist
not_compliant_on :internet_explorer do
expect(browser.button(src: "images/button.png")).to exist
end
expect(browser.button(src: /button\.png/)).to exist
expect(browser.button(text: "Button 2")).to exist
expect(browser.button(text: /Button 2/)).to exist
expect(browser.button(class: "image")).to exist
expect(browser.button(class: /image/)).to exist
expect(browser.button(index: 0)).to exist
expect(browser.button(xpath: "//input[@id='new_user_submit']")).to exist
expect(browser.button(alt: "Create a new user")).to exist
expect(browser.button(alt: /Create a/)).to exist
end
it "returns true if the button exists (tag = :button)" do
expect(browser.button(name: "new_user_button_2")).to exist
expect(browser.button(name: /new_user_button_2/)).to exist
expect(browser.button(value: "button_2")).to exist
expect(browser.button(value: /button_2/)).to exist
expect(browser.button(text: 'Button 2')).to exist
expect(browser.button(text: /Button 2/)).to exist
expect(browser.button(value: 'Button 2')).to exist
expect(browser.button(value: /Button 2/)).to exist
end
it "returns true if the button exists (how = :caption)" do
expect(browser.button(caption: "Button 2")).to exist
expect(browser.button(caption: /Button 2/)).to exist
end
it "returns the first button if given no args" do
expect(browser.button).to exist
end
it "returns true for element with upper case type" do
expect(browser.button(id: "new_user_button_preview")).to exist
end
it "returns false if the button doesn't exist" do
expect(browser.button(id: "no_such_id")).to_not exist
expect(browser.button(id: /no_such_id/)).to_not exist
expect(browser.button(name: "no_such_name")).to_not exist
expect(browser.button(name: /no_such_name/)).to_not exist
expect(browser.button(value: "no_such_value")).to_not exist
expect(browser.button(value: /no_such_value/)).to_not exist
expect(browser.button(src: "no_such_src")).to_not exist
expect(browser.button(src: /no_such_src/)).to_not exist
expect(browser.button(text: "no_such_text")).to_not exist
expect(browser.button(text: /no_such_text/)).to_not exist
expect(browser.button(class: "no_such_class")).to_not exist
expect(browser.button(class: /no_such_class/)).to_not exist
expect(browser.button(index: 1337)).to_not exist
expect(browser.button(xpath: "//input[@id='no_such_id']")).to_not exist
end
it "checks the tag name and type attribute when locating by xpath" do
expect(browser.button(xpath: "//input[@type='text']")).to_not exist
expect(browser.button(xpath: "//input[@type='button']")).to exist
end
it "raises TypeError when 'what' argument is invalid" do
expect { browser.button(id: 3.14).exists? }.to raise_error(TypeError)
end
it "raises MissingWayOfFindingObjectException when 'how' argument is invalid" do
expect { browser.button(no_such_how: 'some_value').exists? }.to raise_error(Watir::Exception::MissingWayOfFindingObjectException)
end
end
# Attribute methods
describe "#class_name" do
it "returns the class name of the button" do
expect(browser.button(name: "new_user_image").class_name).to eq "image"
end
it "returns an empty string if the button has no class name" do
expect(browser.button(name: "new_user_submit").class_name).to eq ""
end
end
describe "#id" do
it "returns the id if the button exists" do
expect(browser.button(index: 0).id).to eq 'new_user_submit'
expect(browser.button(index: 1).id).to eq 'new_user_reset'
expect(browser.button(index: 2).id).to eq 'new_user_button'
end
it "raises UnknownObjectException if button does not exist" do
expect { browser.button(index: 1337).id }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#name" do
it "returns the name if button exists" do
expect(browser.button(index: 0).name).to eq 'new_user_submit'
expect(browser.button(index: 1).name).to eq 'new_user_reset'
expect(browser.button(index: 2).name).to eq 'new_user_button'
end
it "raises UnknownObjectException if the button does not exist" do
expect { browser.button(name: "no_such_name").name }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#src" do
it "returns the src attribute for the button image" do
# varies between browsers
expect(browser.button(name: "new_user_image").src).to include("images/button.png")
end
it "raises UnknownObjectException if the button does not exist" do
expect { browser.button(name: "no_such_name").src }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#style" do
not_compliant_on :internet_explorer, :safari do
it "returns the style attribute if the button exists" do
expect(browser.button(id: 'delete_user_submit').style).to eq "border: 4px solid red;"
end
end
deviates_on :safari do
it "returns the style attribute if the button exists" do
expect(browser.button(id: 'delete_user_submit').style).to eq "border: 4px solid red;"
end
end
it "returns an empty string if the element exists and the attribute doesn't exist" do
expect(browser.button(id: 'new_user_submit').style).to eq ""
end
it "raises UnknownObjectException if the button does not exist" do
expect { browser.button(name: "no_such_name").style }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#title" do
it "returns the title of the button" do
expect(browser.button(index: 0).title).to eq 'Submit the form'
end
it "returns an empty string for button without title" do
expect(browser.button(index: 1).title).to eq ''
end
end
describe "#type" do
it "returns the type if button exists" do
expect(browser.button(index: 0).type).to eq 'submit'
expect(browser.button(index: 1).type).to eq 'reset'
expect(browser.button(index: 2).type).to eq 'button'
end
it "raises UnknownObjectException if button does not exist" do
expect { browser.button(name: "no_such_name").type }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#value" do
it "returns the value if button exists" do
expect(browser.button(index: 0).value).to eq 'Submit'
expect(browser.button(index: 1).value).to eq 'Reset'
expect(browser.button(index: 2).value).to eq 'Button'
end
it "raises UnknownObjectException if button does not exist" do
expect { browser.button(name: "no_such_name").value }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#text" do
it "returns the text of the button" do
expect(browser.button(index: 0).text).to eq 'Submit'
expect(browser.button(index: 1).text).to eq 'Reset'
expect(browser.button(index: 2).text).to eq 'Button'
expect(browser.button(index: 3).text).to eq 'Preview'
end
it "raises UnknownObjectException if the element does not exist" do
expect { browser.button(id: "no_such_id").text }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#respond_to?" do
it "returns true for all attribute methods" do
expect(browser.button(index: 0)).to respond_to(:class_name)
expect(browser.button(index: 0)).to respond_to(:id)
expect(browser.button(index: 0)).to respond_to(:name)
expect(browser.button(index: 0)).to respond_to(:src)
expect(browser.button(index: 0)).to respond_to(:style)
expect(browser.button(index: 0)).to respond_to(:title)
expect(browser.button(index: 0)).to respond_to(:type)
expect(browser.button(index: 0)).to respond_to(:value)
end
end
# Access methods
describe "#enabled?" do
it "returns true if the button is enabled" do
expect(browser.button(name: 'new_user_submit')).to be_enabled
end
it "returns false if the button is disabled" do
expect(browser.button(name: 'new_user_submit_disabled')).to_not be_enabled
end
it "raises UnknownObjectException if the button doesn't exist" do
expect { browser.button(name: "no_such_name").enabled? }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#disabled?" do
it "returns false when button is enabled" do
expect(browser.button(name: 'new_user_submit')).to_not be_disabled
end
it "returns true when button is disabled" do
expect(browser.button(name: 'new_user_submit_disabled')).to be_disabled
end
it "raises UnknownObjectException if button does not exist" do
expect { browser.button(name: "no_such_name").disabled? }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
# Manipulation methods
describe "#click" do
it "clicks the button if it exists" do
browser.goto(WatirSpec.url_for("forms_with_input_elements.html"))
browser.button(id: 'delete_user_submit').click
Watir::Wait.until { !browser.url.include? 'forms_with_input_elements.html'}
expect(browser.text).to include("Semantic table")
end
it "fires events" do
browser.button(id: 'new_user_button').click
expect(browser.button(id: 'new_user_button').value).to eq 'new_value_set_by_onclick_event'
end
it "raises UnknownObjectException when clicking a button that doesn't exist" do
expect { browser.button(value: "no_such_value").click }.to raise_error(Watir::Exception::UnknownObjectException)
expect { browser.button(id: "no_such_id").click }.to raise_error(Watir::Exception::UnknownObjectException)
end
it "raises ObjectDisabledException when clicking a disabled button" do
expect { browser.button(value: "Disabled").click }.to raise_error(Watir::Exception::ObjectDisabledException)
end
end
end