forked from watir/watir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatirspec_helper.rb
216 lines (175 loc) · 5.6 KB
/
watirspec_helper.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
require 'watirspec'
require 'spec_helper'
class ImplementationConfig
def initialize(imp)
@imp = imp
end
def browser
@browser ||= (ENV['WATIR_BROWSER'] || :chrome).to_sym
end
def remote_browser
@remote_browser ||= (ENV['REMOTE_BROWSER'] || :chrome).to_sym
end
def configure
set_webdriver
start_remote_server if remote? && !ENV["REMOTE_SERVER_URL"]
set_browser_args
set_guard_proc
end
private
def start_remote_server
require 'selenium/server'
@server ||= Selenium::Server.new(remote_server_jar,
port: Selenium::WebDriver::PortProber.above(4444),
log: !!$DEBUG,
background: true,
timeout: 60)
@server.start
at_exit { @server.stop }
end
def remote_server_jar
if ENV['LOCAL_SELENIUM']
local = File.expand_path('../selenium/buck-out/gen/java/server/src/org/openqa/grid/selenium/selenium.jar')
end
if File.exist?(ENV['REMOTE_SERVER_BINARY'] || '')
ENV['REMOTE_SERVER_BINARY']
elsif ENV['LOCAL_SELENIUM'] && File.exists?(local)
local
elsif !Dir.glob('*selenium*.jar').empty?
Dir.glob('*selenium*.jar').first
else
Selenium::Server.download :latest
end
rescue SocketError
# not connected to internet
raise Watir::Exception::Error, "unable to find or download selenium-server-standalone jar"
end
def set_webdriver
@imp.name = :webdriver
@imp.browser_class = Watir::Browser
end
def set_browser_args
args = case browser
when :firefox
firefox_args
when :ff_legacy
ff_legacy_args
when :chrome
chrome_args
when :remote
remote_args
else
{desired_capabilities: Selenium::WebDriver::Remote::Capabilities.send(browser)}
end
if ENV['SELECTOR_STATS']
listener = SelectorListener.new
args.merge!(listener: listener)
at_exit { listener.report }
end
@imp.browser_args = [browser, args]
end
def ie?
[:internet_explorer].include? browser
end
def safari?
browser == :safari
end
def phantomjs?
browser == :phantomjs
end
def remote?
browser == :remote
end
def set_guard_proc
matching_guards = [:webdriver]
if remote?
matching_browser = remote_browser
matching_guards << :remote
matching_guards << [:remote, matching_browser]
matching_guards << [:remote, :ff_legacy] if @ff_legacy
else
matching_browser = browser
end
matching_guards << :ff_legacy if @ff_legacy
browser_instance = WatirSpec.new_browser
browser_version = browser_instance.driver.capabilities.version
matching_browser_with_version = "#{browser}#{browser_version}".to_sym
matching_guards << matching_browser_with_version if browser_version
matching_guards << matching_browser
matching_guards << [matching_browser, Selenium::WebDriver::Platform.os]
matching_guards << :relaxed_locate if Watir.relaxed_locate?
matching_guards << :not_relaxed_locate unless Watir.relaxed_locate?
if !Selenium::WebDriver::Platform.linux? || ENV['DESKTOP_SESSION']
# some specs (i.e. Window#maximize) needs a window manager on linux
matching_guards << :window_manager
end
@imp.guard_proc = lambda { |args|
args.any? { |arg| matching_guards.include?(arg) }
}
ensure
browser_instance.close if browser_instance
end
def firefox_args
path = ENV['FIREFOX_BINARY']
Selenium::WebDriver::Firefox::Binary.path = path if path
{desired_capabilities: Selenium::WebDriver::Remote::Capabilities.firefox}
end
def ff_legacy_args
@browser = :firefox
@ff_legacy = true
caps = Selenium::WebDriver::Remote::Capabilities.firefox(marionette: false)
path = ENV['FF_LEGACY_BINARY']
Selenium::WebDriver::Firefox::Binary.path = path if path
{desired_capabilities: caps}
end
def chrome_args
opts = {args: ["--disable-translate"],
desired_capabilities: Selenium::WebDriver::Remote::Capabilities.chrome}
if url = ENV['WATIR_CHROME_SERVER']
opts[:url] = url
end
if driver = ENV['WATIR_CHROME_DRIVER']
Selenium::WebDriver::Chrome.driver_path = driver
end
if path = ENV['WATIR_CHROME_BINARY']
Selenium::WebDriver::Chrome.path = path
end
if ENV['TRAVIS']
opts[:args] << "--no-sandbox" # https://github.com/travis-ci/travis-ci/issues/938
end
opts
end
def remote_args
url = ENV["REMOTE_SERVER_URL"] || "http://127.0.0.1:#{@server.port}/wd/hub"
opts = {}
if remote_browser == :ff_legacy
path = ENV['FF_LEGACY_BINARY']
opts[:firefox_binary] = path if path
@remote_browser = :firefox
@ff_legacy = true
opts[:marionette] = false
elsif remote_browser == :firefox
path = ENV['FIREFOX_BINARY']
opts[:firefox_binary] = path if path
end
caps = Selenium::WebDriver::Remote::Capabilities.send(remote_browser, opts)
{url: url, desired_capabilities: caps}
end
class SelectorListener < Selenium::WebDriver::Support::AbstractEventListener
def initialize
@counts = Hash.new(0)
end
def before_find(how, what, driver)
@counts[how] += 1
end
def report
total = @counts.values.inject(0) { |mem, var| mem + var }
puts "\nSelenium selector stats: "
@counts.each do |how, count|
puts "\t#{how.to_s.ljust(20)}: #{count * 100 / total} (#{count})"
end
end
end
end
ImplementationConfig.new(WatirSpec.implementation).configure
WatirSpec.run!