forked from watir/watirspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_spec.rb
208 lines (172 loc) · 7.64 KB
/
image_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
# encoding: utf-8
require File.expand_path("../spec_helper", __FILE__)
describe "Image" do
before :each do
browser.goto(WatirSpec.url_for("images.html"))
end
# Exists method
describe "#exists?" do
it "returns true when the image exists" do
expect(browser.image(id: 'square')).to exist
expect(browser.image(id: /square/)).to exist
not_compliant_on :watir_classic do
expect(browser.image(src: 'images/circle.png')).to exist
end
deviates_on :watir_classic do
expect(browser.image(src: %r{images/circle.png})).to exist
end
expect(browser.image(src: /circle/)).to exist
expect(browser.image(alt: 'circle')).to exist
expect(browser.image(alt: /cir/)).to exist
expect(browser.image(title: 'Circle')).to exist
end
it "returns the first image if given no args" do
expect(browser.image).to exist
end
it "returns false when the image doesn't exist" do
expect(browser.image(id: 'no_such_id')).to_not exist
expect(browser.image(id: /no_such_id/)).to_not exist
expect(browser.image(src: 'no_such_src')).to_not exist
expect(browser.image(src: /no_such_src/)).to_not exist
expect(browser.image(alt: 'no_such_alt')).to_not exist
expect(browser.image(alt: /no_such_alt/)).to_not exist
expect(browser.image(title: 'no_such_title')).to_not exist
expect(browser.image(title: /no_such_title/)).to_not exist
end
it "raises TypeError when 'what' argument is invalid" do
expect { browser.image(id: 3.14).exists? }.to raise_error(TypeError)
end
it "raises MissingWayOfFindingObjectException when 'how' argument is invalid" do
expect { browser.image(no_such_how: 'some_value').exists? }.to raise_error(Watir::Exception::MissingWayOfFindingObjectException)
end
end
# Attribute methods
describe "#alt" do
it "returns the alt attribute of the image if the image exists" do
expect(browser.image(id: 'square').alt).to eq "square"
expect(browser.image(title: 'Circle').alt).to eq 'circle'
end
it "returns an empty string if the image exists and the attribute doesn't" do
expect(browser.image(index: 0).alt).to eq ""
end
it "raises UnknownObjectException if the image doesn't exist" do
expect { browser.image(index: 1337).alt }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#id" do
it "returns the id attribute of the image if the image exists" do
expect(browser.image(title: 'Square').id).to eq 'square'
end
it "returns an empty string if the image exists and the attribute doesn't" do
expect(browser.image(index: 0).id).to eq ""
end
it "raises UnknownObjectException if the image doesn't exist" do
expect { browser.image(index: 1337).id }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#src" do
it "returns the src attribute of the image if the image exists" do
expect(browser.image(id: 'square').src).to include("square.png")
end
it "returns an empty string if the image exists and the attribute doesn't" do
expect(browser.image(index: 0).src).to eq ""
end
it "raises UnknownObjectException if the image doesn't exist" do
expect { browser.image(index: 1337).src }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#title" do
it "returns the title attribute of the image if the image exists" do
expect(browser.image(id: 'square').title).to eq 'Square'
end
it "returns an empty string if the image exists and the attribute doesn't" do
expect(browser.image(index: 0).title).to eq ""
end
it "raises UnknownObjectException if the image doesn't exist" do
expect { browser.image(index: 1337).title }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#respond_to?" do
it "returns true for all attribute methods" do
expect(browser.image(index: 0)).to respond_to(:class_name)
expect(browser.image(index: 0)).to respond_to(:id)
expect(browser.image(index: 0)).to respond_to(:style)
expect(browser.image(index: 0)).to respond_to(:text)
end
end
# Manipulation methods
describe "#click" do
it "raises UnknownObjectException when the image doesn't exist" do
expect { browser.image(id: 'missing_attribute').click }.to raise_error(Watir::Exception::UnknownObjectException)
expect { browser.image(class: 'missing_attribute').click }.to raise_error(Watir::Exception::UnknownObjectException)
expect { browser.image(src: 'missing_attribute').click }.to raise_error(Watir::Exception::UnknownObjectException)
expect { browser.image(alt: 'missing_attribute').click }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
not_compliant_on :webdriver, :watir_classic do
not_compliant_on :watir_classic do
describe "#file_created_date" do
it "returns the date the image was created as reported by the file system" do
browser.goto(WatirSpec.url_for("images.html", needs_server: true))
image = browser.image(index: 1)
path = "#{File.dirname(__FILE__)}/html#{image.src.gsub(WatirSpec.host, '')}"
image.file_created_date.to_i.to eq File.mtime(path).to_i
end
end
end
describe "#file_size" do
it "returns the file size of the image if the image exists" do
expect(browser.image(id: 'square').file_size).to eq File.size("#{WatirSpec.files}/images/square.png".sub("file://", ''))
end
end
end
it "raises UnknownObjectException if the image doesn't exist" do
expect { browser.image(index: 1337).file_size }.to raise_error(Watir::Exception::UnknownObjectException)
end
describe "#height" do
it "returns the height of the image if the image exists" do
expect(browser.image(id: 'square').height).to eq 88
end
it "raises UnknownObjectException if the image doesn't exist" do
expect { browser.image(index: 1337).height }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#width" do
it "returns the width of the image if the image exists" do
expect(browser.image(id: 'square').width).to eq 88
end
it "raises UnknownObjectException if the image doesn't exist" do
expect { browser.image(index: 1337).width }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
# Other
describe "#loaded?" do
it "returns true if the image has been loaded" do
expect(browser.image(title: 'Circle')).to be_loaded
expect(browser.image(alt: 'circle')).to be_loaded
expect(browser.image(alt: /circle/)).to be_loaded
end
it "returns false if the image has not been loaded" do
expect(browser.image(id: 'no_such_file')).to_not be_loaded
end
it "raises UnknownObjectException if the image doesn't exist" do
expect { browser.image(id: 'no_such_image').loaded? }.to raise_error(Watir::Exception::UnknownObjectException)
expect { browser.image(src: 'no_such_image').loaded? }.to raise_error(Watir::Exception::UnknownObjectException)
expect { browser.image(alt: 'no_such_image').loaded? }.to raise_error(Watir::Exception::UnknownObjectException)
expect { browser.image(index: 1337).loaded? }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
not_compliant_on :webdriver do
describe "#save" do
it "saves the image to a file" do
file = "#{File.expand_path Dir.pwd}/sample.img.dat"
begin
browser.image(index: 1).save(file)
expect(File.exist?(file)).to be true
ensure
File.delete(file) if File.exist?(file)
end
end
end
end
end