forked from brynary/webrat
/
selenium_session.rb
240 lines (184 loc) · 6.32 KB
/
selenium_session.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
require "webrat/core/save_and_open_page"
require "webrat/selenium/selenium_rc_server"
require "webrat/selenium/application_server"
module Webrat
class TimeoutError < WebratError
end
class SeleniumResponse
attr_reader :body
attr_reader :session
def initialize(session, body)
@session = session
@body = body
end
def selenium
session.selenium
end
end
class SeleniumSession
include Webrat::SaveAndOpenPage
include Webrat::Selenium::SilenceStream
def initialize(*args) # :nodoc:
end
def simulate
end
def automate
yield
end
def visit(url)
selenium.open(url)
end
webrat_deprecate :visits, :visit
def fill_in(field_identifier, options)
locator = "webrat=#{Regexp.escape(field_identifier)}"
selenium.wait_for_element locator, :timeout_in_seconds => 5
selenium.type(locator, "#{options[:with]}")
end
webrat_deprecate :fills_in, :fill_in
def response
SeleniumResponse.new(self, response_body)
end
def response_body #:nodoc:
selenium.get_html_source
end
def current_url
selenium.location
end
def click_button(button_text_or_regexp = nil, options = {})
if button_text_or_regexp.is_a?(Hash) && options == {}
pattern, options = nil, button_text_or_regexp
elsif button_text_or_regexp
pattern = adjust_if_regexp(button_text_or_regexp)
end
pattern ||= '*'
locator = "button=#{pattern}"
selenium.wait_for_element locator, :timeout_in_seconds => 5
selenium.click locator
end
webrat_deprecate :clicks_button, :click_button
def click_link(link_text_or_regexp, options = {})
pattern = adjust_if_regexp(link_text_or_regexp)
locator = "webratlink=#{pattern}"
selenium.wait_for_element locator, :timeout_in_seconds => 5
selenium.click locator
end
webrat_deprecate :clicks_link, :click_link
def click_link_within(selector, link_text, options = {})
locator = "webratlinkwithin=#{selector}|#{link_text}"
selenium.wait_for_element locator, :timeout_in_seconds => 5
selenium.click locator
end
webrat_deprecate :clicks_link_within, :click_link_within
def select(option_text, options = {})
id_or_name_or_label = options[:from]
if id_or_name_or_label
select_locator = "webrat=#{id_or_name_or_label}"
else
select_locator = "webratselectwithoption=#{option_text}"
end
selenium.wait_for_element select_locator, :timeout_in_seconds => 5
selenium.select(select_locator, option_text)
end
webrat_deprecate :selects, :select
def choose(label_text)
locator = "webrat=#{label_text}"
selenium.wait_for_element locator, :timeout_in_seconds => 5
selenium.click locator
end
webrat_deprecate :chooses, :choose
def check(label_text)
locator = "webrat=#{label_text}"
selenium.wait_for_element locator, :timeout_in_seconds => 5
selenium.click locator
end
alias_method :uncheck, :check
webrat_deprecate :checks, :check
def fire_event(field_identifier, event)
locator = "webrat=#{Regexp.escape(field_identifier)}"
selenium.fire_event(locator, "#{event}")
end
def key_down(field_identifier, key_code)
locator = "webrat=#{Regexp.escape(field_identifier)}"
selenium.key_down(locator, key_code)
end
def key_up(field_identifier, key_code)
locator = "webrat=#{Regexp.escape(field_identifier)}"
selenium.key_up(locator, key_code)
end
def wait_for(params={})
timeout = params[:timeout] || 5
message = params[:message] || "Timeout exceeded"
begin_time = Time.now
while (Time.now - begin_time) < timeout
value = nil
begin
value = yield
rescue ::Spec::Expectations::ExpectationNotMetError, ::Selenium::CommandError, Webrat::WebratError
value = nil
end
return value if value
sleep 0.25
end
raise Webrat::TimeoutError.new(message + " (after #{timeout} sec)")
true
end
def selenium
return $browser if $browser
setup
$browser
end
webrat_deprecate :browser, :selenium
def save_and_open_screengrab
return unless File.exist?(saved_page_dir)
filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.png"
if $browser.chrome_backend?
$browser.capture_entire_page_screenshot(filename, '')
else
$browser.capture_screenshot(filename)
end
open_in_browser(filename)
end
protected
def setup #:nodoc:
Webrat::Selenium::SeleniumRCServer.boot
Webrat::Selenium::ApplicationServer.boot
create_browser
$browser.start
extend_selenium
define_location_strategies
$browser.window_maximize
end
def create_browser
$browser = ::Selenium::Client::Driver.new(:host => Webrat.configuration.selenium_server_address || "localhost",
:port => Webrat.configuration.selenium_server_port,
:browser => Webrat.configuration.selenium_browser_key,
:url => "http://#{Webrat.configuration.application_address}:#{Webrat.configuration.application_port}",
:timeout_in_seconds => Webrat.configuration.selenium_browser_startup_timeout)
$browser.set_speed(0) unless Webrat.configuration.selenium_server_address
at_exit do
silence_stream(STDOUT) do
$browser.stop
end
end
end
def adjust_if_regexp(text_or_regexp) #:nodoc:
if text_or_regexp.is_a?(Regexp)
"evalregex:#{text_or_regexp.inspect}"
else
"evalregex:/#{text_or_regexp}/"
end
end
def extend_selenium #:nodoc:
extensions_file = File.join(File.dirname(__FILE__), "selenium_extensions.js")
extenions_js = File.read(extensions_file)
selenium.get_eval(extenions_js)
end
def define_location_strategies #:nodoc:
Dir[File.join(File.dirname(__FILE__), "location_strategy_javascript", "*.js")].sort.each do |file|
strategy_js = File.read(file)
strategy_name = File.basename(file, '.js')
selenium.add_location_strategy(strategy_name, strategy_js)
end
end
end
end