Created
November 13, 2015 10:23
-
-
Save afa/1f50e7c3e61b69f46258 to your computer and use it in GitHub Desktop.
Tuning capybara + capybara-webkit + database_cleaner + DeferredGarbageCollection + feature macros
This file contains hidden or 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
| # spec/support/feature_macros.rb | |
| module FeatureMacros | |
| extend ActiveSupport::Concern | |
| # get current domain ("lvh.me:12345") | |
| def domain(port = Capybara.current_session.server.try(:port) || 3000) | |
| port &&= ":#{ port }" | |
| "#{ DEFAULT_HOST }#{ port }" | |
| end | |
| def href_url(href) | |
| s = "http:#{ href.sub(/\A(http\:)/, '').sub(/(\/*)\z/, '') }/" | |
| end | |
| def href_path(href) | |
| href.sub(/\A(http\:\/{2})([^\/]+)/, '').sub(/(\?.*)\z/, '') | |
| end | |
| # switching methods for debug purpose | |
| module ClassMethods | |
| def selenium! | |
| Capybara.javascript_driver = :selenium | |
| end | |
| def webkit! | |
| Capybara.javascript_driver = :webkit | |
| end | |
| end | |
| end |
This file contains hidden or 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
| # spec/spec_helper.rb | |
| # This file is copied to spec/ when you run 'rails generate rspec:install' | |
| ENV["RAILS_ENV"] ||= 'test' | |
| require File.expand_path("../../config/environment", __FILE__) | |
| require 'rspec/rails' | |
| require 'capybara/rspec' | |
| require 'sucker_punch/testing/inline' | |
| require 'should_not/rspec' | |
| DEFAULT_HOST = "lvh.me" | |
| Capybara.configure do |config| | |
| config.javascript_driver = :webkit | |
| config.always_include_port = true | |
| config.app_host = "http://#{ DEFAULT_HOST }" | |
| end | |
| # Requires supporting ruby files with custom matchers and macros, etc, | |
| # in spec/support/ and its subdirectories. | |
| Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } | |
| # Checks for pending migrations before tests are run. | |
| # If you are not using ActiveRecord, you can remove this line. | |
| ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) | |
| RSpec.configure do |config| | |
| config.expect_with :rspec do |c| | |
| c.syntax = :expect | |
| end | |
| # ## Mock Framework | |
| # | |
| # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: | |
| # | |
| # config.mock_with :mocha | |
| # config.mock_with :flexmock | |
| # config.mock_with :rr | |
| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures | |
| # config.fixture_path = "#{::Rails.root}/spec/fixtures" | |
| # If you're not using ActiveRecord, or you'd prefer not to run each of your | |
| # examples within a transaction, remove the following line or assign false | |
| # instead of true. | |
| # config.use_transactional_fixtures = true | |
| # If true, the base class of anonymous controllers will be inferred | |
| # automatically. This will be the default behavior in future versions of | |
| # rspec-rails. | |
| config.infer_base_class_for_anonymous_controllers = false | |
| # Run specs in random order to surface order dependencies. If you find an | |
| # order dependency and want to debug it, you can fix the order by providing | |
| # the seed, which is printed after each run. | |
| # --seed 1234 | |
| config.order = "random" | |
| # tune GC | |
| config.before(:all) { DeferredGarbageCollection.start } | |
| config.after(:all) do | |
| DeferredGarbageCollection.reconsider | |
| FileUtils.rm_rf(Settings.uploads.temp_path) | |
| end | |
| config.before(:suite) do | |
| DatabaseCleaner.clean_with(:truncation) | |
| end | |
| config.around(:each) do |example| | |
| DatabaseCleaner.strategy = if example.metadata[:js] || example.metadata[:type] == 'feature' | |
| :truncation | |
| else | |
| :transaction | |
| end | |
| DatabaseCleaner.start | |
| ActionMailer::Base.deliveries.clear | |
| example.run | |
| Capybara.reset_sessions! | |
| DatabaseCleaner.clean | |
| end | |
| config.treat_symbols_as_metadata_keys_with_true_values = true | |
| config.include FactoryGirl::Syntax::Methods | |
| config.include Devise::TestHelpers, type: 'controller' | |
| config.include FeatureMacros, type: 'feature' | |
| config.include Capybara::Angular::DSL, type: 'feature' | |
| end |
This file contains hidden or 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
| # spec/features/users_guest_area_spec.rb | |
| require 'spec_helper' | |
| feature 'Guest area' do | |
| # Rails.configuration.domain is used inside the Application, so we set it to the current Capybara session domain with port! | |
| background { Rails.configuration.domain = domain } | |
| def click_url_link(css) | |
| link = find(css) | |
| new_url = href_url(link[:href]) | |
| link.click | |
| new_url | |
| end | |
| def click_path_link(css) | |
| link = find(css) | |
| new_path = href_path(link[:href]) | |
| link.click | |
| new_path | |
| end | |
| # selenium! | |
| context 'at home page', :js do | |
| scenario 'observe open club' do | |
| 7.times { create(:published_club) } | |
| visit home_path | |
| expect(page).to have_css('section.promo-wrap .ready-meeting', count: 6) | |
| find('#fetch_more_open_clubs').click | |
| new_url = click_url_link('#open_club_7 a.title') | |
| expect(current_url).to eq new_url | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment