Created
August 29, 2011 18:51
-
-
Save agirorn/1179105 to your computer and use it in GitHub Desktop.
Control the speed of Capybara Selenium from the cucumber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When /^I drag a tree node somewhere that results in a lot of ajax calls$/ do | |
tree_node = page.find('#tree_node_1') | |
drop_spot = page.find('#drop_spot') | |
delay_capybara do | |
tree_node.drag_to(drop_spot) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'selenium-webdriver' | |
# Pute this file in features/support/ | |
# Control the speed af the Capybara Selenium driver. | |
# you can now do the following to control the speed of 'Cucumber/Capybara/Selenium' | |
# CAPYBARA_DELAY=slow be cucumber | |
# CAPYBARA_DELAY=medium be cucumber | |
# CAPYBARA_DELAY=fast be cucumber | |
# | |
# for a fine grained control do this: | |
# CAPYBARA_DELAY=0.5 be cucumber | |
# CAPYBARA_DELAY=1 be cucumber | |
module ::Selenium::WebDriver::Firefox | |
class Bridge | |
attr_accessor :delay | |
DELAY_SPEEDS = { | |
:slow => 0.3, | |
:medium => 0.1, | |
:fast => 0.0 | |
} | |
def capybara_compute_delay value | |
value = value.to_s | |
return value.to_f if value =~ /\A(\d+\.\d+|\d+)\z/ | |
if value =~ /\A\w+\z/ and DELAY_SPEEDS.include? value.to_sym | |
return DELAY_SPEEDS[value.to_sym] | |
end | |
return 0 | |
end | |
def execute(*args) | |
result = raw_execute(*args)['value'] | |
sleep( capybara_compute_delay(delay) ) if delay | |
result | |
end | |
end | |
end | |
module CapybaraDelayHelpers | |
def capybara_delay_speed= value | |
page.driver.browser.send(:bridge).delay = value | |
end | |
def delay_capybara(options = {:speed => :medium}, &code) | |
old_capybara_delay_speed = page.driver.browser.send(:bridge).delay | |
page.driver.browser.send(:bridge).delay = options[:speed] | |
yield | |
page.driver.browser.send(:bridge).delay = old_capybara_delay_speed | |
end | |
end | |
World(CapybaraDelayHelpers) | |
Before('@javascript') do | |
if ENV.include? 'CAPYBARA_DELAY' | |
capybara_delay_speed = ENV['CAPYBARA_DELAY'] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment