forked from watir/watirspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
div_spec.rb
217 lines (185 loc) · 8.51 KB
/
div_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
# encoding: utf-8
require File.expand_path("../spec_helper", __FILE__)
describe "Div" do
before :each do
browser.goto(WatirSpec.url_for("non_control_elements.html"))
end
# Exists method
describe "#exists?" do
it "returns true if the element exists" do
expect(browser.div(:id, "header")).to exist
expect(browser.div(:id, /header/)).to exist
expect(browser.div(:title, "Header and primary navigation")).to exist
expect(browser.div(:title, /Header and primary navigation/)).to exist
expect(browser.div(:text, "This is a footer.")).to exist
expect(browser.div(:text, /This is a footer\./)).to exist
expect(browser.div(:class, "profile")).to exist
expect(browser.div(:class, /profile/)).to exist
expect(browser.div(:index, 0)).to exist
expect(browser.div(:xpath, "//div[@id='header']")).to exist
end
it "returns the first div if given no args" do
expect(browser.div).to exist
end
it "returns false if the element does not exist" do
expect(browser.div(:id, "no_such_id")).to_not exist
expect(browser.div(:id, /no_such_id/)).to_not exist
expect(browser.div(:title, "no_such_title")).to_not exist
expect(browser.div(:title, /no_such_title/)).to_not exist
expect(browser.div(:text, "no_such_text")).to_not exist
expect(browser.div(:text, /no_such_text/)).to_not exist
expect(browser.div(:class, "no_such_class")).to_not exist
expect(browser.div(:class, /no_such_class/)).to_not exist
expect(browser.div(:index, 1337)).to_not exist
expect(browser.div(:xpath, "//div[@id='no_such_id']")).to_not exist
end
it "raises TypeError when 'what' argument is invalid" do
expect { browser.div(:id, 3.14).exists? }.to raise_error(TypeError)
end
it "raises MissingWayOfFindingObjectException when 'how' argument is invalid" do
expect { browser.div(:no_such_how, 'some_value').exists? }.to raise_error(MissingWayOfFindingObjectException)
end
end
# Attribute methods
describe "#class_name" do
it "returns the class attribute if the element exists" do
expect(browser.div(:id, "footer").class_name).to eq "profile"
end
it "returns an empty string if the element exists but the attribute doesn't" do
expect(browser.div(:id, "content").class_name).to eq ""
end
it "raises UnknownObjectException if the element does not exist" do
expect { browser.div(:id, "no_such_id").class_name }.to raise_error(UnknownObjectException)
expect { browser.div(:title, "no_such_title").class_name }.to raise_error(UnknownObjectException)
expect { browser.div(:index, 1337).class_name }.to raise_error(UnknownObjectException)
expect { browser.div(:xpath, "//div[@id='no_such_id']").class_name }.to raise_error(UnknownObjectException)
end
end
describe "#id" do
it "returns the id attribute if the element exists" do
expect(browser.div(:index, 1).id).to eq "outer_container"
end
it "returns an empty string if the element exists, but the attribute doesn't" do
expect(browser.div(:index, 0).id).to eq ""
end
it "raises UnknownObjectException if the element does not exist" do
expect { browser.div(:id, "no_such_id").id }.to raise_error(UnknownObjectException)
expect { browser.div(:title, "no_such_id").id }.to raise_error(UnknownObjectException)
expect { browser.div(:index, 1337).id }.to raise_error(UnknownObjectException)
end
it "should take all conditions into account when locating by id" do
browser.goto WatirSpec.url_for "multiple_ids.html"
expect(browser.div(:id => "multiple", :class => "bar").class_name).to eq "bar"
end
end
describe "#style" do
not_compliant_on :internet_explorer do
it "returns the style attribute if the element exists" do
expect(browser.div(:id, 'best_language').style).to eq "color: red; text-decoration: underline; cursor: pointer;"
end
end
deviates_on :internet_explorer8 do
it "returns the style attribute if the element exists" do
expect(browser.div(:id, 'best_language').style).to eq "COLOR: red; CURSOR: pointer; TEXT-DECORATION: underline"
end
end
deviates_on :internet_explorer9 do
it "returns the style attribute if the element exists" do
expect(browser.div(:id, 'best_language').style).to eq "color: red; text-decoration: underline; cursor: pointer;"
end
end
it "returns an empty string if the element exists but the attribute doesn't" do
expect(browser.div(:id, 'promo').style).to eq ""
end
it "raises UnknownObjectException if the element does not exist" do
expect {browser.div(:id, "no_such_id").style }.to raise_error(UnknownObjectException)
end
end
describe "#text" do
it "returns the text of the div" do
expect(browser.div(:id, "footer").text.strip).to eq "This is a footer."
expect(browser.div(:title, "Closing remarks").text.strip).to eq "This is a footer."
expect(browser.div(:xpath, "//div[@id='footer']").text.strip).to eq "This is a footer."
end
it "returns an empty string if the element exists but contains no text" do
expect(browser.div(:index, 0).text.strip).to eq ""
end
it "returns an empty string if the div is hidden" do
expect(browser.div(:id, 'hidden').text).to eq ""
end
it "raises UnknownObjectException if the element does not exist" do
expect { browser.div(:id, "no_such_id").text }.to raise_error(UnknownObjectException)
expect { browser.div(:title, "no_such_title").text }.to raise_error(UnknownObjectException)
expect { browser.div(:index, 1337).text }.to raise_error(UnknownObjectException)
expect { browser.div(:xpath, "//div[@id='no_such_id']").text }.to raise_error(UnknownObjectException)
end
end
describe "#respond_to?" do
it "returns true for all attribute methods" do
expect(browser.div(:index, 0)).to respond_to(:class_name)
expect(browser.div(:index, 0)).to respond_to(:id)
expect(browser.div(:index, 0)).to respond_to(:style)
expect(browser.div(:index, 0)).to respond_to(:text)
end
end
# Manipulation methods
describe "#click" do
it "fires events when clicked" do
expect(browser.div(:id, 'best_language').text).to_not eq 'Ruby!'
browser.div(:id, 'best_language').click
expect(browser.div(:id, 'best_language').text).to eq 'Ruby!'
end
it "raises UnknownObjectException if the element does not exist" do
expect { browser.div(:id, "no_such_id").click }.to raise_error(UnknownObjectException)
expect { browser.div(:title, "no_such_title").click }.to raise_error(UnknownObjectException)
expect { browser.div(:index, 1337).click }.to raise_error(UnknownObjectException)
expect { browser.div(:xpath, "//div[@id='no_such_id']").click }.to raise_error(UnknownObjectException)
end
end
not_compliant_on [:webdriver, :iphone] do
describe "#double_click" do
it "fires the ondblclick event" do
browser.div(:id, 'html_test').double_click
expect(messages).to include('double clicked')
end
end
describe "#right_click" do
it "fires the oncontextmenu event" do
browser.goto(WatirSpec.url_for("right_click.html"))
browser.div(:id, "click").right_click
bug "https://github.com/detro/ghostdriver/issues/125", :phantomjs do
expect(messages.first).to eq 'right-clicked'
end
end
end
end
describe "#html" do
not_compliant_on :internet_explorer do
it "returns the HTML of the element" do
html = browser.div(:id, 'footer').html.downcase
expect(html).to include('id="footer"')
expect(html).to include('title="closing remarks"')
expect(html).to include('class="profile"')
expect(html).to_not include('<div id="content">')
expect(html).to_not include('</body>')
end
end
deviates_on :internet_explorer do
it "returns the HTML of the element" do
html = browser.div(:id, 'footer').html.downcase
expect(html).to include('title="closing remarks"')
expect(html).to_not include('</body>')
deviates_on :internet_explorer8 do
expect(html).to include('id=footer')
expect(html).to include('class=profile')
expect(html).to_not include('<div id=content>')
end
deviates_on :internet_explorer9 do
expect(html).to include('id="footer"')
expect(html).to include('class="profile"')
expect(html).to_not include('<div id="content">')
end
end
end
end
end