Skip to content

Instantly share code, notes, and snippets.

@rogercampos
Created June 29, 2012 14:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogercampos/3018161 to your computer and use it in GitHub Desktop.
Save rogercampos/3018161 to your computer and use it in GitHub Desktop.
Capybara helpers
def saop
save_and_open_page
end
def screenshot
page.driver.render("/home/roger/shot.jpg", :full => true)
end
def select_should_have(locator, text)
node = page.find_field(locator)
node.should_not be_nil
options = node.all("option")
selected = options.detect{|x| ["selected", "true", true].include?(x["selected"])}
selected.should_not be_nil
selected.text.should == text
end
def field_content(locator)
node = page.find_field(locator)
return nil if node.nil?
node["value"]
end
def field_should_have(locator, value)
field_content(locator).should == value
end
def stub_env(new_env, &block)
original_env = Rails.env
Rails.instance_variable_set("@_env", ActiveSupport::StringInquirer.new(new_env))
block.call
ensure
Rails.instance_variable_set("@_env", ActiveSupport::StringInquirer.new(original_env))
end
def go_back
page.evaluate_script "window.history.back()"
end
# Builds the current_path with query string. Usually we can't use current_url
# as it includes the host part, which can be different in selenium than in
# rack test.
def full_current_path
uri = URI.parse(current_url)
"#{uri.path}#{"?#{uri.query}" if uri.query}"
end
def field_should_be_disabled(locator)
check = page.find(locator)
check["disabled"].should == "disabled"
end
def using_locale(locale)
aux = I18n.locale
I18n.locale = locale.to_sym
res = yield
I18n.locale = aux
res
end
def wait_for_ajax(timeout = Capybara.default_wait_time)
begin
wait_until(timeout) do
page.evaluate_script 'jQuery.active != 0'
end
rescue Capybara::TimeoutError
raise "Wait for ajax was requested but no active Ajax request can be found"
end
begin
wait_until(timeout) do
page.evaluate_script 'jQuery.active == 0'
end
rescue Capybara::TimeoutError
raise "Ajax expired"
end
end
def trigger_click_by_id(button_id)
page.execute_script <<-JS
$("##{button_id}").trigger("click");
JS
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment