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
/
filefield_spec.rb
182 lines (147 loc) · 6.81 KB
/
filefield_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
# encoding: utf-8
require File.expand_path("../spec_helper", __FILE__)
describe "FileField" do
before :each do
browser.goto(WatirSpec.url_for("forms_with_input_elements.html"))
end
describe "#exist?" do
it "returns true if the file field exists" do
expect(browser.file_field(id: 'new_user_portrait')).to exist
expect(browser.file_field(id: /new_user_portrait/)).to exist
expect(browser.file_field(name: 'new_user_portrait')).to exist
expect(browser.file_field(name: /new_user_portrait/)).to exist
expect(browser.file_field(class: 'portrait')).to exist
expect(browser.file_field(class: /portrait/)).to exist
expect(browser.file_field(index: 0)).to exist
expect(browser.file_field(xpath: "//input[@id='new_user_portrait']")).to exist
end
it "returns the first file field if given no args" do
expect(browser.file_field).to exist
end
it "returns true for element with upper case type" do
expect(browser.file_field(id: "new_user_resume")).to exist
end
it "returns false if the file field doesn't exist" do
expect(browser.file_field(id: 'no_such_id')).to_not exist
expect(browser.file_field(id: /no_such_id/)).to_not exist
expect(browser.file_field(name: 'no_such_name')).to_not exist
expect(browser.file_field(name: /no_such_name/)).to_not exist
expect(browser.file_field(class: 'no_such_class')).to_not exist
expect(browser.file_field(class: /no_such_class/)).to_not exist
expect(browser.file_field(index: 1337)).to_not exist
expect(browser.file_field(xpath: "//input[@id='no_such_id']")).to_not exist
end
it "raises TypeError when 'what' argument is invalid" do
expect { browser.file_field(id: 3.14).exists? }.to raise_error(TypeError)
end
it "raises MissingWayOfFindingObjectException when 'how' argument is invalid" do
expect { browser.file_field(no_such_how: 'some_value').exists? }.to raise_error(Watir::Exception::MissingWayOfFindingObjectException)
end
end
# Attribute methods
describe "#class_name" do
it "returns the class attribute if the text field exists" do
expect(browser.file_field(index: 0).class_name).to eq "portrait"
end
it "raises UnknownObjectException if the text field doesn't exist" do
expect { browser.file_field(index: 1337).class_name }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#id" do
it "returns the id attribute if the text field exists" do
expect(browser.file_field(index: 0).id).to eq "new_user_portrait"
end
it "raises UnknownObjectException if the text field doesn't exist" do
expect { browser.file_field(index: 1337).id }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#name" do
it "returns the name attribute if the text field exists" do
expect(browser.file_field(index: 0).name).to eq "new_user_portrait"
end
it "raises UnknownObjectException if the text field doesn't exist" do
expect { browser.file_field(index: 1337).name }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#title" do
it "returns the title attribute if the text field exists" do
expect(browser.file_field(id: "new_user_portrait").title).to eq "Smile!"
end
end
describe "#type" do
it "returns the type attribute if the text field exists" do
expect(browser.file_field(index: 0).type).to eq "file"
end
it "raises UnknownObjectException if the text field doesn't exist" do
expect { browser.file_field(index: 1337).type }.to raise_error(Watir::Exception::UnknownObjectException)
end
end
describe "#respond_to?" do
it "returns true for all attribute methods" do
expect(browser.file_field(index: 0)).to respond_to(:class_name)
expect(browser.file_field(index: 0)).to respond_to(:id)
expect(browser.file_field(index: 0)).to respond_to(:name)
expect(browser.file_field(index: 0)).to respond_to(:title)
expect(browser.file_field(index: 0)).to respond_to(:type)
expect(browser.file_field(index: 0)).to respond_to(:value)
end
end
# Manipulation methods
describe "#set" do
not_compliant_on :safari do
bug "https://bugzilla.mozilla.org/show_bug.cgi?id=1260233", :firefox do
bug "https://github.com/detro/ghostdriver/issues/183", :phantomjs do
it "is able to set a file path in the field and click the upload button and fire the onchange event" do
browser.goto WatirSpec.url_for("forms_with_input_elements.html")
path = File.expand_path(__FILE__)
element = browser.file_field(name: "new_user_portrait")
element.set path
expect(element.value).to include(File.basename(path)) # only some browser will return the full path
expect(messages.first).to include(File.basename(path))
browser.button(name: "new_user_submit").click
end
end
it "raises an error if the file does not exist" do
expect {
browser.file_field.set(File.join(Dir.tmpdir, 'unlikely-to-exist'))
}.to raise_error(Errno::ENOENT)
end
end
end
end
bug "https://github.com/detro/ghostdriver/issues/183", :phantomjs do
not_compliant_on :safari do
describe "#value=" do
bug "https://bugzilla.mozilla.org/show_bug.cgi?id=1260233", :firefox do
it "is able to set a file path in the field and click the upload button and fire the onchange event" do
browser.goto WatirSpec.url_for("forms_with_input_elements.html")
path = File.expand_path(__FILE__)
element = browser.file_field(name: "new_user_portrait")
element.value = path
expect(element.value).to include(File.basename(path)) # only some browser will return the full path
end
not_compliant_on :internet_explorer do
bug "Raises File not found error - File Bug Report", :firefox do
it "does not raise an error if the file does not exist" do
path = File.join(Dir.tmpdir, 'unlikely-to-exist')
browser.file_field.value = path
expected = path
expected.gsub!("/", "\\") if WatirSpec.platform == :windows
expect(browser.file_field.value).to include(File.basename(expected)) # only some browser will return the full path
end
end
not_compliant_on %i(chrome windows) do
bug "Raises File not found error - File Bug Report", :firefox do
it "does not alter its argument" do
value = '/foo/bar'
browser.file_field.value = value
expect(value).to eq '/foo/bar'
end
end
end
end
end
end
end
end
end