Skip to content

Instantly share code, notes, and snippets.

@HuckyDucky HuckyDucky/gist:10219649
Last active Oct 9, 2017

Embed
What would you like to do?
Testing setup for Capybara and autocomplete
#############################################################################################################
## This is a gist that contains my complete setup, minus gems, that I needed to get my jQuery autocomplete ##
## testing out properly with the RSpec/Capybara/PhantomJS stack. ##
#############################################################################################################
#########################
## spec/spec_helper.rb ##
#########################
ENV["RAILS_ENV"] = 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rubygems'
require 'rspec'
require 'rspec/rails'
require 'rspec/mocks'
# require 'rspec/autorun'
require 'forgery'
require 'factory_girl'
require 'active_support/testing/setup_and_teardown'
require 'capybara'
require 'webmock/rspec'
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
WebMock.disable_net_connect!(:allow_localhost => true)
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.infer_base_class_for_anonymous_controllers = false
config.run_all_when_everything_filtered = true
config.treat_symbols_as_metadata_keys_with_true_values = true
config.mock_with :rspec
config.include FactoryGirl::Syntax::Methods
config.include Rails.application.routes.url_helpers
config.include Capybara::DSL
config.include FeatureHelper, type: :request
config.order = "random"
end
##############################
## spec/support/capybara.rb ##
##############################
require 'capybara/rspec'
require 'capybara/poltergeist'
require 'capybara-screenshot/rspec'
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, { js_errors: true, timeout: 30 })
end
Capybara.javascript_driver = :poltergeist
Capybara.default_wait_time = 10
Capybara.run_server = true
Capybara.server_port = 7000
Capybara.app_host = "http://localhost:#{Capybara.server_port}"
Capybara::Screenshot.autosave_on_failure = true
Capybara.ignore_hidden_elements = true
######################################
## spec/support/database_cleaner.rb ##
######################################
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation, {:except => %w[aaia_brands repositories standards ebay_categories] })
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation, {:except => %w[aaia_brands repositories standards ebay_categories] }
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each, js: true) do
current_path.should == current_path
end
config.append_after(:each) do
DatabaseCleaner.clean
end
end
####################################
## spec/support/feature_helper.rb ##
####################################
module FeatureHelper
def fill_autocomplete(field, options = {})
fill_in field, with: options[:with]
page.execute_script %Q{ $('##{field}').trigger('focus') }
page.execute_script %Q{ $('##{field}').trigger('keydown') }
selector = %Q{ul.ui-autocomplete li.ui-menu-item a:contains('#{options[:with]}')}
page.should have_selector('ul.ui-autocomplete li.ui-menu-item a')
# page.find('ul.ui-autocomplete li.ui-menu-item a', :text => options[:with]).trigger(:mouseover).click()
# page.execute_script %Q{ $("#{selector}").trigger('mouseenter').click() }
page.execute_script "$(\"#{selector}\").mouseenter().click()"
end
def select_nth_option(id, n)
option_xpath = "//*[@id='#{id}']/option[#{n}]"
option = find(:xpath, option_xpath).text
select(option, from: id)
end
def within_row(text, &block)
within :xpath, "//table//tr[td[contains(.,\"#{text}\")]]" do
yield
end
end
end
# spec/support/shared_db_connection.rb
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || ConnectionPool::Wrapper.new(:size => 1) { retrieve_connection }
# @@shared_connection || retrieve_connection
end
###############################################
## spec/requests/product_integration_spec.rb ##
###############################################
require "spec_helper"
describe "Product Integration" do
it "lets a user search for a product", js: true do
# Other stuff
fill_autocomplete('products_search', with: product.part_number)
# This is an example of something that should be on your screen after you make the selection
page.should have_content("You selected #{product.part_number}")
end
end
#################################################################
## And here's a very basic jQuery autocomplete in CoffeeScript ##
## It calls a controller action called "search_products. ##
## Don't forget to set it up in your config/routes.rb too. ##
#################################################################
$("#products_search").autocomplete
source: (request, response) ->
options = { "term": request.term }
$.get "search_products", options, (data) ->
response data
minLength: 2
select: (event, ui) ->
# Do whatever you want here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.