Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 277 lines (212 sloc) 7.134 kb
7d63aa1 @lukemelia Extracted save_and_open_page related functionality to a module and inclu...
lukemelia authored
1 require "webrat/core/save_and_open_page"
996484c @brynary Refactoring selenium RC and app server boot code
authored
2 require "webrat/selenium/selenium_rc_server"
a6a4a7c @gaffo moving app server lookup out into a factory
gaffo authored
3 require "webrat/selenium/application_server_factory"
dd8688f @gaffo moved application_server to appservers/base
gaffo authored
4 require "webrat/selenium/application_servers/base"
7d63aa1 @lukemelia Extracted save_and_open_page related functionality to a module and inclu...
lukemelia authored
5
bd6cd43 @djanowski Point users in the right direction when selenium is not available.
djanowski authored
6 begin
fea21fc @epall Test for the appropriate Selenium import
epall authored
7 require "selenium/client"
bd6cd43 @djanowski Point users in the right direction when selenium is not available.
djanowski authored
8 rescue LoadError => e
fea21fc @epall Test for the appropriate Selenium import
epall authored
9 e.message << " (You may need to install the selenium-webdriver gem)"
bd6cd43 @djanowski Point users in the right direction when selenium is not available.
djanowski authored
10 raise e
11 end
2fc9f3f @brynary Move adapters to webrat/adapters, and Rails and Merb integration code in...
authored
12
7a5fbee @lukemelia Spiked a Selenium session type for webrat.
lukemelia authored
13 module Webrat
0888c68 @brynary Added auto-waiting #contain and #have_selector matchers for Selenium
authored
14 class TimeoutError < WebratError
15 end
f3a12ad @gaffo whitespace changes
gaffo authored
16
0888c68 @brynary Added auto-waiting #contain and #have_selector matchers for Selenium
authored
17 class SeleniumResponse
18 attr_reader :body
19 attr_reader :session
f3a12ad @gaffo whitespace changes
gaffo authored
20
0888c68 @brynary Added auto-waiting #contain and #have_selector matchers for Selenium
authored
21 def initialize(session, body)
22 @session = session
23 @body = body
24 end
f3a12ad @gaffo whitespace changes
gaffo authored
25
0888c68 @brynary Added auto-waiting #contain and #have_selector matchers for Selenium
authored
26 def selenium
27 session.selenium
28 end
29 end
f3a12ad @gaffo whitespace changes
gaffo authored
30
7b5da74 @brynary SeleniumSession shouldn't inherit Webrat::Session, it just acts like one
authored
31 class SeleniumSession
7d63aa1 @lukemelia Extracted save_and_open_page related functionality to a module and inclu...
lukemelia authored
32 include Webrat::SaveAndOpenPage
926bcc6 @snusnu extracted silence_stream into its own file under webrat/selenium
snusnu authored
33 include Webrat::Selenium::SilenceStream
f3a12ad @gaffo whitespace changes
gaffo authored
34
da68aa8 @brynary Cleaning up Selenium support. Mongrel and Selenium servers start/shutdow...
authored
35 def initialize(*args) # :nodoc:
7a5fbee @lukemelia Spiked a Selenium session type for webrat.
lukemelia authored
36 end
f3a12ad @gaffo whitespace changes
gaffo authored
37
2d427ed @brynary simualte and automate need to be there in SeleniumSession too
authored
38 def simulate
39 end
f3a12ad @gaffo whitespace changes
gaffo authored
40
2d427ed @brynary simualte and automate need to be there in SeleniumSession too
authored
41 def automate
42 yield
43 end
f3a12ad @gaffo whitespace changes
gaffo authored
44
72bcbca @brynary Renaming visits to visit
authored
45 def visit(url)
da68aa8 @brynary Cleaning up Selenium support. Mongrel and Selenium servers start/shutdow...
authored
46 selenium.open(url)
7a5fbee @lukemelia Spiked a Selenium session type for webrat.
lukemelia authored
47 end
f3a12ad @gaffo whitespace changes
gaffo authored
48
491ceb7 @brynary Deprecating old style Selenium methods
authored
49 webrat_deprecate :visits, :visit
f3a12ad @gaffo whitespace changes
gaffo authored
50
eabc7b6 @brynary Renaming fills_in to fill_in
authored
51 def fill_in(field_identifier, options)
f8f254d @gaffo [#257 state:resolved] fixed issue with rails encodd fields in webrat
gaffo authored
52 locator = "webrat=#{field_identifier}"
cd49c2d @balinterdi bugfix: selenium's wait_for_element method takes the number of seconds b...
balinterdi authored
53 selenium.wait_for_element locator, :timeout_in_seconds => 5
da68aa8 @brynary Cleaning up Selenium support. Mongrel and Selenium servers start/shutdow...
authored
54 selenium.type(locator, "#{options[:with]}")
7a5fbee @lukemelia Spiked a Selenium session type for webrat.
lukemelia authored
55 end
f3a12ad @gaffo whitespace changes
gaffo authored
56
491ceb7 @brynary Deprecating old style Selenium methods
authored
57 webrat_deprecate :fills_in, :fill_in
f3a12ad @gaffo whitespace changes
gaffo authored
58
0888c68 @brynary Added auto-waiting #contain and #have_selector matchers for Selenium
authored
59 def response
60 SeleniumResponse.new(self, response_body)
61 end
f3a12ad @gaffo whitespace changes
gaffo authored
62
2d86745 @brynary Cleaning up rdoc to only show public APIs
authored
63 def response_body #:nodoc:
da68aa8 @brynary Cleaning up Selenium support. Mongrel and Selenium servers start/shutdow...
authored
64 selenium.get_html_source
7a5fbee @lukemelia Spiked a Selenium session type for webrat.
lukemelia authored
65 end
f3a12ad @gaffo whitespace changes
gaffo authored
66
7f13a70 @rubbish Added current_url to SeleniumSession to have the same api for both sessi...
rubbish authored
67 def current_url
68 selenium.location
69 end
f3a12ad @gaffo whitespace changes
gaffo authored
70
eabc7b6 @brynary Renaming fills_in to fill_in
authored
71 def click_button(button_text_or_regexp = nil, options = {})
ca9d044 @lukemelia Add support in Selenium implementation for clicking a link or button usi...
lukemelia authored
72 if button_text_or_regexp.is_a?(Hash) && options == {}
73 pattern, options = nil, button_text_or_regexp
f3dfa32 @lukemelia Allow SeleniumSession's click_button to be called without an argument wi...
lukemelia authored
74 elsif button_text_or_regexp
ca9d044 @lukemelia Add support in Selenium implementation for clicking a link or button usi...
lukemelia authored
75 pattern = adjust_if_regexp(button_text_or_regexp)
76 end
77 pattern ||= '*'
e887720 @brynary More Webrat implicit waiting. Only load Webrat selectors once per run, n...
authored
78 locator = "button=#{pattern}"
f3a12ad @gaffo whitespace changes
gaffo authored
79
cd49c2d @balinterdi bugfix: selenium's wait_for_element method takes the number of seconds b...
balinterdi authored
80 selenium.wait_for_element locator, :timeout_in_seconds => 5
e887720 @brynary More Webrat implicit waiting. Only load Webrat selectors once per run, n...
authored
81 selenium.click locator
7a5fbee @lukemelia Spiked a Selenium session type for webrat.
lukemelia authored
82 end
f3a12ad @gaffo whitespace changes
gaffo authored
83
491ceb7 @brynary Deprecating old style Selenium methods
authored
84 webrat_deprecate :clicks_button, :click_button
f3a12ad @gaffo whitespace changes
gaffo authored
85
24ac5d3 @brynary Rename clicks_link and clicks_link_within to click_link and click_link_w...
authored
86 def click_link(link_text_or_regexp, options = {})
63e1053 @brynary Fix bug in Selenium when dealing with special characters in link text
authored
87 if link_text_or_regexp.is_a?(Regexp)
88 pattern = "evalregex:#{link_text_or_regexp.inspect}"
89 else
90 pattern = link_text_or_regexp.to_s
91 end
92
e887720 @brynary More Webrat implicit waiting. Only load Webrat selectors once per run, n...
authored
93 locator = "webratlink=#{pattern}"
cd49c2d @balinterdi bugfix: selenium's wait_for_element method takes the number of seconds b...
balinterdi authored
94 selenium.wait_for_element locator, :timeout_in_seconds => 5
e887720 @brynary More Webrat implicit waiting. Only load Webrat selectors once per run, n...
authored
95 selenium.click locator
7a5fbee @lukemelia Spiked a Selenium session type for webrat.
lukemelia authored
96 end
f3a12ad @gaffo whitespace changes
gaffo authored
97
491ceb7 @brynary Deprecating old style Selenium methods
authored
98 webrat_deprecate :clicks_link, :click_link
f3a12ad @gaffo whitespace changes
gaffo authored
99
24ac5d3 @brynary Rename clicks_link and clicks_link_within to click_link and click_link_w...
authored
100 def click_link_within(selector, link_text, options = {})
e887720 @brynary More Webrat implicit waiting. Only load Webrat selectors once per run, n...
authored
101 locator = "webratlinkwithin=#{selector}|#{link_text}"
cd49c2d @balinterdi bugfix: selenium's wait_for_element method takes the number of seconds b...
balinterdi authored
102 selenium.wait_for_element locator, :timeout_in_seconds => 5
e887720 @brynary More Webrat implicit waiting. Only load Webrat selectors once per run, n...
authored
103 selenium.click locator
cfdd667 @lukemelia Implemented clicks_link_within for selenium session.
lukemelia authored
104 end
f3a12ad @gaffo whitespace changes
gaffo authored
105
491ceb7 @brynary Deprecating old style Selenium methods
authored
106 webrat_deprecate :clicks_link_within, :click_link_within
f3a12ad @gaffo whitespace changes
gaffo authored
107
db0488c @brynary Updating usages of "selects" methods to "select"
authored
108 def select(option_text, options = {})
7a5fbee @lukemelia Spiked a Selenium session type for webrat.
lukemelia authored
109 id_or_name_or_label = options[:from]
f3a12ad @gaffo whitespace changes
gaffo authored
110
7a5fbee @lukemelia Spiked a Selenium session type for webrat.
lukemelia authored
111 if id_or_name_or_label
112 select_locator = "webrat=#{id_or_name_or_label}"
113 else
114 select_locator = "webratselectwithoption=#{option_text}"
115 end
f3a12ad @gaffo whitespace changes
gaffo authored
116
cd49c2d @balinterdi bugfix: selenium's wait_for_element method takes the number of seconds b...
balinterdi authored
117 selenium.wait_for_element select_locator, :timeout_in_seconds => 5
da68aa8 @brynary Cleaning up Selenium support. Mongrel and Selenium servers start/shutdow...
authored
118 selenium.select(select_locator, option_text)
7a5fbee @lukemelia Spiked a Selenium session type for webrat.
lukemelia authored
119 end
f3a12ad @gaffo whitespace changes
gaffo authored
120
491ceb7 @brynary Deprecating old style Selenium methods
authored
121 webrat_deprecate :selects, :select
f3a12ad @gaffo whitespace changes
gaffo authored
122
c5bf4f3 @brynary Rename chooses to choose
authored
123 def choose(label_text)
e887720 @brynary More Webrat implicit waiting. Only load Webrat selectors once per run, n...
authored
124 locator = "webrat=#{label_text}"
cd49c2d @balinterdi bugfix: selenium's wait_for_element method takes the number of seconds b...
balinterdi authored
125 selenium.wait_for_element locator, :timeout_in_seconds => 5
e887720 @brynary More Webrat implicit waiting. Only load Webrat selectors once per run, n...
authored
126 selenium.click locator
7a5fbee @lukemelia Spiked a Selenium session type for webrat.
lukemelia authored
127 end
f3a12ad @gaffo whitespace changes
gaffo authored
128
491ceb7 @brynary Deprecating old style Selenium methods
authored
129 webrat_deprecate :chooses, :choose
f3a12ad @gaffo whitespace changes
gaffo authored
130
ea193e1 @brynary Rename checks and unchecks to check and uncheck
authored
131 def check(label_text)
e887720 @brynary More Webrat implicit waiting. Only load Webrat selectors once per run, n...
authored
132 locator = "webrat=#{label_text}"
cd49c2d @balinterdi bugfix: selenium's wait_for_element method takes the number of seconds b...
balinterdi authored
133 selenium.wait_for_element locator, :timeout_in_seconds => 5
996eedb Switch to using selenium.click instead of .check when checking a checkbo...
Noah Davis authored
134 selenium.click locator
7a5fbee @lukemelia Spiked a Selenium session type for webrat.
lukemelia authored
135 end
56e6d15 add uncheck to selenium_session
Lee Bankewitz authored
136 alias_method :uncheck, :check
f3a12ad @gaffo whitespace changes
gaffo authored
137
491ceb7 @brynary Deprecating old style Selenium methods
authored
138 webrat_deprecate :checks, :check
f3a12ad @gaffo whitespace changes
gaffo authored
139
db81a97 @nando Added fire_event wrapper in Selenium sessions
nando authored
140 def fire_event(field_identifier, event)
141 locator = "webrat=#{Regexp.escape(field_identifier)}"
da68aa8 @brynary Cleaning up Selenium support. Mongrel and Selenium servers start/shutdow...
authored
142 selenium.fire_event(locator, "#{event}")
db81a97 @nando Added fire_event wrapper in Selenium sessions
nando authored
143 end
f3a12ad @gaffo whitespace changes
gaffo authored
144
92b4814 @nando Added key_down and key_up wrapper in Selenium session
nando authored
145 def key_down(field_identifier, key_code)
146 locator = "webrat=#{Regexp.escape(field_identifier)}"
da68aa8 @brynary Cleaning up Selenium support. Mongrel and Selenium servers start/shutdow...
authored
147 selenium.key_down(locator, key_code)
92b4814 @nando Added key_down and key_up wrapper in Selenium session
nando authored
148 end
f3a12ad @gaffo whitespace changes
gaffo authored
149
92b4814 @nando Added key_down and key_up wrapper in Selenium session
nando authored
150 def key_up(field_identifier, key_code)
151 locator = "webrat=#{Regexp.escape(field_identifier)}"
da68aa8 @brynary Cleaning up Selenium support. Mongrel and Selenium servers start/shutdow...
authored
152 selenium.key_up(locator, key_code)
153 end
f3a12ad @gaffo whitespace changes
gaffo authored
154
0888c68 @brynary Added auto-waiting #contain and #have_selector matchers for Selenium
authored
155 def wait_for(params={})
156 timeout = params[:timeout] || 5
157 message = params[:message] || "Timeout exceeded"
f3a12ad @gaffo whitespace changes
gaffo authored
158
0888c68 @brynary Added auto-waiting #contain and #have_selector matchers for Selenium
authored
159 begin_time = Time.now
f3a12ad @gaffo whitespace changes
gaffo authored
160
0888c68 @brynary Added auto-waiting #contain and #have_selector matchers for Selenium
authored
161 while (Time.now - begin_time) < timeout
162 value = nil
f3a12ad @gaffo whitespace changes
gaffo authored
163
0888c68 @brynary Added auto-waiting #contain and #have_selector matchers for Selenium
authored
164 begin
165 value = yield
977f643 @gaffo catches spec failed exception again
gaffo authored
166 rescue Exception => e
6bfdcc5 @gaffo Fixed Spec in Test Unit more thoroghly
gaffo authored
167 unless is_ignorable_wait_for_exception?(e)
168 raise e
169 end
0888c68 @brynary Added auto-waiting #contain and #have_selector matchers for Selenium
authored
170 end
f3a12ad @gaffo whitespace changes
gaffo authored
171
0888c68 @brynary Added auto-waiting #contain and #have_selector matchers for Selenium
authored
172 return value if value
f3a12ad @gaffo whitespace changes
gaffo authored
173
0888c68 @brynary Added auto-waiting #contain and #have_selector matchers for Selenium
authored
174 sleep 0.25
175 end
f3a12ad @gaffo whitespace changes
gaffo authored
176
f51b745 @brynary When a timeout occurs in wait_for, include the HTML from Selenium in the...
authored
177 error_message = "#{message} (after #{timeout} sec)"
68d5fde @noahd1 Move verbose selenium output that can clutter build output behind settin...
noahd1 authored
178
179 if $browser && Webrat.configuration.selenium_verbose_output
f51b745 @brynary When a timeout occurs in wait_for, include the HTML from Selenium in the...
authored
180 error_message += <<-EOS
181
182
183 HTML of the page was:
184
185 #{selenium.get_html_source}"
186 EOS
187 end
188
189 raise Webrat::TimeoutError.new(error_message)
0888c68 @brynary Added auto-waiting #contain and #have_selector matchers for Selenium
authored
190 true
191 end
f3a12ad @gaffo whitespace changes
gaffo authored
192
2a4959e @brynary Expose #selenium method for direct access to selenium object
authored
193 def selenium
da68aa8 @brynary Cleaning up Selenium support. Mongrel and Selenium servers start/shutdow...
authored
194 return $browser if $browser
195 setup
196 $browser
92b4814 @nando Added key_down and key_up wrapper in Selenium session
nando authored
197 end
f3a12ad @gaffo whitespace changes
gaffo authored
198
2a4959e @brynary Expose #selenium method for direct access to selenium object
authored
199 webrat_deprecate :browser, :selenium
f3a12ad @gaffo whitespace changes
gaffo authored
200
201
7d63aa1 @lukemelia Extracted save_and_open_page related functionality to a module and inclu...
lukemelia authored
202 def save_and_open_screengrab
71dcfb3 @noahd1 Save and open page directory specified via configuration
noahd1 authored
203 return unless File.exist?(Webrat.configuration.saved_pages_dir)
f3a12ad @gaffo whitespace changes
gaffo authored
204
71dcfb3 @noahd1 Save and open page directory specified via configuration
noahd1 authored
205 filename = "#{Webrat.configuration.saved_pages_dir}/webrat-#{Time.now.to_i}.png"
f3a12ad @gaffo whitespace changes
gaffo authored
206
7d63aa1 @lukemelia Extracted save_and_open_page related functionality to a module and inclu...
lukemelia authored
207 if $browser.chrome_backend?
208 $browser.capture_entire_page_screenshot(filename, '')
209 else
210 $browser.capture_screenshot(filename)
211 end
977f643 @gaffo catches spec failed exception again
gaffo authored
212 open_in_browser(filename)
f3a12ad @gaffo whitespace changes
gaffo authored
213
7d63aa1 @lukemelia Extracted save_and_open_page related functionality to a module and inclu...
lukemelia authored
214 end
f3a12ad @gaffo whitespace changes
gaffo authored
215
977f643 @gaffo catches spec failed exception again
gaffo authored
216 protected
6bfdcc5 @gaffo Fixed Spec in Test Unit more thoroghly
gaffo authored
217 def is_ignorable_wait_for_exception?(exception) #:nodoc:
218 if defined?(::Spec::Expectations::ExpectationNotMetError)
219 return true if exception.class == ::Spec::Expectations::ExpectationNotMetError
220 end
221 return true if [::Selenium::CommandError, Webrat::WebratError].include?(exception.class)
222 return false
977f643 @gaffo catches spec failed exception again
gaffo authored
223 end
f3a12ad @gaffo whitespace changes
gaffo authored
224
3b8fc55 @brynary RDoc tweaks
authored
225 def setup #:nodoc:
996484c @brynary Refactoring selenium RC and app server boot code
authored
226 Webrat::Selenium::SeleniumRCServer.boot
a6a4a7c @gaffo moving app server lookup out into a factory
gaffo authored
227 Webrat::Selenium::ApplicationServerFactory.app_server_instance.boot
f3a12ad @gaffo whitespace changes
gaffo authored
228
c79d221 added selenium_browser_key to allow running on more than firefox, and co...
cornel.borcean authored
229 create_browser
da68aa8 @brynary Cleaning up Selenium support. Mongrel and Selenium servers start/shutdow...
authored
230 $browser.start
f3a12ad @gaffo whitespace changes
gaffo authored
231
e887720 @brynary More Webrat implicit waiting. Only load Webrat selectors once per run, n...
authored
232 extend_selenium
233 define_location_strategies
492a301 @lukemelia For your viewing pleasure... maximize the selenium window at after initi...
lukemelia authored
234 $browser.window_maximize
da68aa8 @brynary Cleaning up Selenium support. Mongrel and Selenium servers start/shutdow...
authored
235 end
f3a12ad @gaffo whitespace changes
gaffo authored
236
237
c79d221 added selenium_browser_key to allow running on more than firefox, and co...
cornel.borcean authored
238 def create_browser
5c382a5 @brynary Code formatting
authored
239 $browser = ::Selenium::Client::Driver.new(
240 Webrat.configuration.selenium_server_address || "localhost",
241 Webrat.configuration.selenium_server_port,
242 Webrat.configuration.selenium_browser_key,
243 "http://#{Webrat.configuration.application_address}:#{Webrat.configuration.application_port_for_selenium}"
244 )
c79d221 added selenium_browser_key to allow running on more than firefox, and co...
cornel.borcean authored
245 $browser.set_speed(0) unless Webrat.configuration.selenium_server_address
f3a12ad @gaffo whitespace changes
gaffo authored
246
da68aa8 @brynary Cleaning up Selenium support. Mongrel and Selenium servers start/shutdow...
authored
247 at_exit do
248 silence_stream(STDOUT) do
249 $browser.stop
250 end
251 end
252 end
f3a12ad @gaffo whitespace changes
gaffo authored
253
2d86745 @brynary Cleaning up rdoc to only show public APIs
authored
254 def adjust_if_regexp(text_or_regexp) #:nodoc:
ca9d044 @lukemelia Add support in Selenium implementation for clicking a link or button usi...
lukemelia authored
255 if text_or_regexp.is_a?(Regexp)
256 "evalregex:#{text_or_regexp.inspect}"
257 else
567f5ad @rossta Text treated as regexp for buttons
rossta authored
258 "evalregex:/#{text_or_regexp}/"
33d2cdc changed selenium_port to application_port to reflect what the variable i...
cornel.borcean authored
259 end
ca9d044 @lukemelia Add support in Selenium implementation for clicking a link or button usi...
lukemelia authored
260 end
f3a12ad @gaffo whitespace changes
gaffo authored
261
2d86745 @brynary Cleaning up rdoc to only show public APIs
authored
262 def extend_selenium #:nodoc:
ca9d044 @lukemelia Add support in Selenium implementation for clicking a link or button usi...
lukemelia authored
263 extensions_file = File.join(File.dirname(__FILE__), "selenium_extensions.js")
264 extenions_js = File.read(extensions_file)
da68aa8 @brynary Cleaning up Selenium support. Mongrel and Selenium servers start/shutdow...
authored
265 selenium.get_eval(extenions_js)
ca9d044 @lukemelia Add support in Selenium implementation for clicking a link or button usi...
lukemelia authored
266 end
f3a12ad @gaffo whitespace changes
gaffo authored
267
2d86745 @brynary Cleaning up rdoc to only show public APIs
authored
268 def define_location_strategies #:nodoc:
1af45a7 @lukemelia Extracted Selenium location strategies to their own .js files for easier...
lukemelia authored
269 Dir[File.join(File.dirname(__FILE__), "location_strategy_javascript", "*.js")].sort.each do |file|
270 strategy_js = File.read(file)
271 strategy_name = File.basename(file, '.js')
da68aa8 @brynary Cleaning up Selenium support. Mongrel and Selenium servers start/shutdow...
authored
272 selenium.add_location_strategy(strategy_name, strategy_js)
1af45a7 @lukemelia Extracted Selenium location strategies to their own .js files for easier...
lukemelia authored
273 end
7a5fbee @lukemelia Spiked a Selenium session type for webrat.
lukemelia authored
274 end
275 end
82eabc3 @atmos small fixup for sinatra + selenium, use rackup instead of mongrel_rails ...
atmos authored
276 end
Something went wrong with that request. Please try again.