/
i_edriver_spec.rb
192 lines (143 loc) · 6.37 KB
/
i_edriver_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
# frozen_string_literal: true
require 'spec_helper'
describe Webdrivers::IEdriver do
let(:iedriver) { described_class }
before do
iedriver.remove
iedriver.required_version = nil
end
describe '#update' do
context 'when evaluating #correct_binary?' do
it 'does not download when latest version and current version match' do
allow(iedriver).to receive(:latest_version).and_return(Gem::Version.new('0.3.0'))
allow(iedriver).to receive(:current_version).and_return(Gem::Version.new('0.3.0'))
iedriver.update
expect(iedriver.send(:exists?)).to be false
end
it 'does not download when offline, but binary exists' do
allow(Webdrivers::System).to receive(:call).and_return('something IEDriverServer.exe 3.5.1 something else')
allow(Net::HTTP).to receive(:get_response).and_raise(SocketError)
allow(iedriver).to receive(:exists?).and_return(true)
iedriver.update
expect(File.exist?(iedriver.driver_path)).to be false
end
it 'raises ConnectionError when offline, and no binary exists' do
allow(Net::HTTP).to receive(:get_response).and_raise(SocketError)
allow(iedriver).to receive(:exists?).and_return(false)
expect { iedriver.update }.to raise_error(Webdrivers::ConnectionError)
end
end
context 'when correct binary is found' do
before { allow(iedriver).to receive(:correct_binary?).and_return(true) }
it 'does not download' do
iedriver.update
expect(iedriver.current_version).to be_nil
end
it 'does not raise exception if offline' do
allow(Net::HTTP).to receive(:get_response).and_raise(SocketError)
iedriver.update
expect(iedriver.current_version).to be_nil
end
end
context 'when correct binary is not found' do
before { allow(iedriver).to receive(:correct_binary?).and_return(false) }
it 'downloads binary' do
iedriver.update
expect(File.exist?(iedriver.driver_path)).to eq true
end
it 'raises ConnectionError if offline' do
allow(Net::HTTP).to receive(:get_response).and_raise(SocketError)
msg = %r{Can not reach https://selenium-release.storage.googleapis.com/}
expect { iedriver.update }.to raise_error(Webdrivers::ConnectionError, msg)
end
end
end
describe '#current_version' do
it 'returns nil if binary does not exist on the system' do
allow(iedriver).to receive(:driver_path).and_return('')
expect(iedriver.current_version).to be_nil
end
it 'returns a Gem::Version instance if binary is on the system' do
allow(iedriver).to receive(:exists?).and_return(true)
return_value = 'something IEDriverServer.exe 3.5.1 something else'
allow(Webdrivers::System).to receive(:call).with(iedriver.driver_path, '--version').and_return return_value
expect(iedriver.current_version).to eq Gem::Version.new('3.5.1')
end
end
describe '#latest_version' do
it 'finds the latest version from parsed hash' do
base = 'https://selenium-release.storage.googleapis.com/'
hash = {Gem::Version.new('3.4.0') => "#{base}/3.4/IEDriverServer_Win32_3.4.0.zip",
Gem::Version.new('3.5.0') => "#{base}/3.5/IEDriverServer_Win32_3.5.0.zip",
Gem::Version.new('3.5.1') => "#{base}/3.5/IEDriverServer_Win32_3.5.1.zip"}
allow(iedriver).to receive(:downloads).and_return(hash)
expect(iedriver.latest_version).to eq Gem::Version.new('3.5.1')
end
it 'correctly parses the downloads page' do
expect(iedriver.send(:downloads)).not_to be_empty
end
it 'creates cached file' do
allow(Webdrivers::Network).to receive(:get).and_return('3.4.0')
iedriver.latest_version
expect(File.exist?("#{Webdrivers::System.install_dir}/IEDriverServer.version")).to eq true
end
it 'does not make network calls if cache is valid and driver exists' do
allow(Webdrivers).to receive(:cache_time).and_return(3600)
Webdrivers::System.cache_version('IEDriverServer', '3.4.0')
allow(Webdrivers::System).to receive(:exists?).and_return(true)
allow(Webdrivers::Network).to receive(:get)
expect(iedriver.latest_version).to eq Gem::Version.new('3.4.0')
expect(Webdrivers::Network).not_to have_received(:get)
end
it 'makes a network call if cache is expired' do
Webdrivers::System.cache_version('IEDriverServer', '3.4.0')
base = 'https://selenium-release.storage.googleapis.com/'
hash = {Gem::Version.new('3.4.0') => "#{base}/3.4/IEDriverServer_Win32_3.4.0.zip",
Gem::Version.new('3.5.0') => "#{base}/3.5/IEDriverServer_Win32_3.5.0.zip",
Gem::Version.new('3.5.1') => "#{base}/3.5/IEDriverServer_Win32_3.5.1.zip"}
allow(iedriver).to receive(:downloads).and_return(hash)
allow(Webdrivers::System).to receive(:valid_cache?)
expect(iedriver.latest_version).to eq Gem::Version.new('3.5.1')
expect(iedriver).to have_received(:downloads)
expect(Webdrivers::System).to have_received(:valid_cache?)
end
end
describe '#required_version=' do
it 'returns the version specified as a Float' do
iedriver.required_version = 0.12
expect(iedriver.required_version).to eq Gem::Version.new('0.12')
end
it 'returns the version specified as a String' do
iedriver.required_version = '0.12.1'
expect(iedriver.required_version).to eq Gem::Version.new('0.12.1')
end
end
describe '#remove' do
it 'removes existing iedriver' do
iedriver.update
iedriver.remove
expect(iedriver.current_version).to be_nil
end
it 'does not raise exception if no iedriver found' do
expect { iedriver.remove }.not_to raise_error
end
end
describe '#install_dir' do
it 'uses ~/.webdrivers as default value' do
expect(Webdrivers::System.install_dir).to include('.webdriver')
end
it 'uses provided value' do
install_dir = File.expand_path(File.join(ENV['HOME'], '.webdrivers2'))
Webdrivers.install_dir = install_dir
expect(Webdrivers::System.install_dir).to eq install_dir
ensure
Webdrivers.install_dir = nil
end
end
describe '#driver_path' do
it 'returns full location of binary' do
expected_path = File.absolute_path "#{File.join(ENV['HOME'])}/.webdrivers/IEDriverServer.exe"
expect(iedriver.driver_path).to eq(expected_path)
end
end
end