require 'active_support/concern' module Softwear::Lib module Spec extend ActiveSupport::Concern module CapybaraHelpers def wait_for_ajax begin Timeout.timeout(Capybara.default_wait_time) do wait_for_jquery loop until finished_all_ajax_requests? end rescue sleep 0.1 end end def finished_all_ajax_requests? page.evaluate_script('jQuery.active').zero? end def wait_for_redirect original_url = current_path until current_path != original_url sleep(0.1) end end def wait_for_jquery until page.evaluate_script('jQuery.active') == 0 sleep(0.1) end end def accept_alert page.evaluate_script('window.confirm = function() { return true; }') yield end def dismiss_alert page.evaluate_script('window.confirm = function() { return false; }') yield # Restore existing default page.evaluate_script('window.confirm = function() { return true; }') end def eventually_fill_in(field, options={}) page.should have_css('#' + field) fill_in field, options end def within_row(num, &block) if RSpec.current_example.metadata[:js] within("table.index tbody tr:nth-child(#{num})", &block) else within(:xpath, all('table.index tbody tr')[num-1].path, &block) end end def column_text(num) if RSpec.current_example.metadata[:js] find("td:nth-child(#{num})").text else all('td')[num-1].text end end def find_label_by_text(text, options = {}) label = find_label(text) counter = 0 if !options[:try] && label.nil? raise "Could not find label by text #{text}" end label end def find_label(text) first(:xpath, "//label[text()[contains(.,'#{text}')]]") end end module Select2 def select2(text, options) label = find_label_by_text(options[:from], try: true) if label selector = "select[name='#{label['for']}']" else selector = options[:from] end find("#{selector}+span").click find('input.select2-search__field').set(text) first('li.select2-results__option').click end end included do include Warden::Test::Helpers Warden.test_mode! RSpec.configure do |config| config.include CapybaraHelpers, type: :feature config.include Select2, type: :feature end end end end