summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Mercier <tylermercier@gmail.com>2013-07-23 14:37:20 -0600
committerTyler Mercier <tylermercier@gmail.com>2013-07-23 14:37:20 -0600
commit985ae524144ca8442413567fd987324b534f1f3e (patch)
treeaa49f32679b9e51fb2e445e1701081db35249c05
parent74b40e14c5b6a7e8152032d43519993ad93f1365 (diff)
switch to poltergeist for browser tests.
-rw-r--r--Gemfile4
-rw-r--r--Gemfile.lock1
-rw-r--r--spec/requests/.gitkeep0
-rw-r--r--spec/requests/google_spec.rb (renamed from spec/features/google_spec.rb)14
-rw-r--r--spec/requests/landing_page_spec.rb (renamed from spec/features/landing_page_spec.rb)0
-rw-r--r--spec/spec_helper.rb49
-rw-r--r--spec/support/authorization_helpers.rb13
-rw-r--r--spec/support/capybara_ext.rb107
8 files changed, 160 insertions, 28 deletions
diff --git a/Gemfile b/Gemfile
index c791804..3a85a7a 100644
--- a/Gemfile
+++ b/Gemfile
@@ -46,8 +46,10 @@ group :development, :test do
gem 'rspec-fakes'
gem 'teaspoon'
gem 'capybara'
- gem 'database_cleaner'
+ #gem 'capybara-screenshot'
+ gem 'poltergeist'
gem 'selenium-webdriver'
+ gem 'database_cleaner'
gem 'simplecov'
gem 'factory_girl_rails'
end
diff --git a/Gemfile.lock b/Gemfile.lock
index eb86a20..7393579 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -227,6 +227,7 @@ DEPENDENCIES
jbuilder (~> 1.2)
jquery-rails
pg
+ poltergeist
rails (= 4.0.0)
rails_12factor
rspec-fakes
diff --git a/spec/requests/.gitkeep b/spec/requests/.gitkeep
deleted file mode 100644
index e69de29..0000000
--- a/spec/requests/.gitkeep
+++ /dev/null
diff --git a/spec/features/google_spec.rb b/spec/requests/google_spec.rb
index 24b4b7e..86932fb 100644
--- a/spec/features/google_spec.rb
+++ b/spec/requests/google_spec.rb
@@ -14,18 +14,4 @@ describe "Google", js: true do
page.should_not have_content('Microsoft')
end
end
-
- describe "searching for something important" do
- before :each do
- visit 'http://www.google.com'
- within('#gs_lc0') do
- fill_in 'gbqfq', with: 'fairgoods'
- end
- find('#gbqfb').click
- end
-
- it "should find something important" do
- expect(page).to have_content('Fairgoods')
- end
- end
end
diff --git a/spec/features/landing_page_spec.rb b/spec/requests/landing_page_spec.rb
index 0321186..0321186 100644
--- a/spec/features/landing_page_spec.rb
+++ b/spec/requests/landing_page_spec.rb
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 3d49429..92da919 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -1,5 +1,6 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
+
require 'simplecov'
SimpleCov.start do
add_filter '/app/assets/'
@@ -12,13 +13,25 @@ SimpleCov.start do
add_filter '/script/'
add_filter '/spec/'
end
+
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
+
+Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
+
require 'rspec/autorun'
require 'capybara/rails'
require 'capybara/rspec'
+require 'capybara/rspec'
+require 'capybara/poltergeist'
+#require 'capybara-screenshot/rspec'
require 'database_cleaner'
+Capybara.register_driver :poltergeist_with_long_timeout do |app|
+ Capybara::Poltergeist::Driver.new(app, timeout: 30)
+end
+Capybara.javascript_driver = :poltergeist_with_long_timeout #unless FeatureFlag::BROWSER
+
# 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 }
@@ -28,17 +41,6 @@ Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
- # ## 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.
@@ -54,17 +56,38 @@ RSpec.configure do |config|
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
+
+ config.include CapybaraExt
+
+ config.include Capybara::DSL, type: :request
+ config.include Capybara::RSpecMatchers, type: :request
+
+ #config.extend AuthorizationHelpers::Controller, type: :controller
+
config.include FactoryGirl::Syntax::Methods
+
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
- config.before(:each) do
+ # Feature specs cannot use a transaction because Capybara runs in a
+ # separate thread with a different database connection.
+ config.before type: :request do
+ DatabaseCleaner.strategy = :truncation
+ end
+
+ # Reset so other non-feature specs don't have to deal with slow truncation.
+ config.after type: :request do
+ DatabaseCleaner.strategy = :transaction
+ end
+
+ config.before do
DatabaseCleaner.start
+ ActionMailer::Base.deliveries.clear
end
- config.after(:each) do
+ config.after do
DatabaseCleaner.clean
end
end
diff --git a/spec/support/authorization_helpers.rb b/spec/support/authorization_helpers.rb
new file mode 100644
index 0000000..1b08a21
--- /dev/null
+++ b/spec/support/authorization_helpers.rb
@@ -0,0 +1,13 @@
+module AuthorizationHelpers
+ module Controller
+ # def stub_authorization!(roles = [Role.new(id: 1, name: 'admin')])
+ # let(:ability_user) { stub_model(User)}
+
+ # before do
+ # controller.stub(:current_user => ability_user)
+ # controller.should_receive(:authorize!).any_number_of_times.and_return(true)
+ # ability_user.stub(:roles).and_return(roles)
+ # end
+ # end
+ end
+end
diff --git a/spec/support/capybara_ext.rb b/spec/support/capybara_ext.rb
new file mode 100644
index 0000000..35b50ce
--- /dev/null
+++ b/spec/support/capybara_ext.rb
@@ -0,0 +1,107 @@
+# http://techblog.fundinggates.com/blog/2012/08/capybara-2-0-upgrade-guide/
+# https://github.com/mattheworiordan/capybara-screenshot
+module CapybaraExt
+ def page!
+ save_and_open_page
+ end
+
+ def wait_until(timeout = Capybara.default_wait_time)
+ Timeout.timeout(timeout) do
+ sleep(0.1) until value = yield
+ value
+ end
+ end
+
+ def retry_on_timeout(n = 3, &block)
+ begin
+ block.call
+ rescue Capybara::ElementNotFound
+ if n > 0
+ puts "Catched error: #{e.message}. #{n-1} more attempts."
+ retry_on_timeout(n - 1, &block)
+ else
+ raise
+ end
+ end
+ end
+
+ def click_icon(type)
+ first(".icon-#{type}").click
+ end
+
+ def eventually_fill_in(field, options={})
+ Capybara.wait_until do
+ find_field field
+ end
+ fill_in field, options
+ end
+
+ def within_row(num, &block)
+ within("table.index tbody tr:nth-child(#{num})", &block)
+ end
+
+ def column_text(num)
+ find("td:nth-child(#{num})").text
+ end
+
+ def set_select2_field(field, value)
+ page.execute_script %Q{$('#{field}').select2('val', '#{value}')}
+ end
+
+ def select2_search(value, options)
+ id = find_label_by_text(options[:from])
+ options[:from] = "#s2id_#{id}"
+ targetted_select2_search(value, options)
+ end
+
+ def targetted_select2_search(value, options)
+ #p %Q{$('#{options[:from]}').select2('open')}
+ page.execute_script %Q{$('#{options[:from]}').select2('open')}
+ container = options[:dropdown_css] || ".select2-with-searchbox"
+ #p "$('#{container} input.select2-input').val('#{value}').trigger('keyup-change');"
+ page.execute_script "$('#{container} input.select2-input').val('#{value}').trigger('keyup-change');"
+ select_select2_result(value)
+ end
+
+ def select2(value, options)
+ id = find_label_by_text(options[:from])
+
+ # generate select2 id
+ options[:from] = "#s2id_#{id}"
+ targetted_select2(value, options)
+ end
+
+ def targetted_select2(value, options)
+ # find select2 element and click it
+ find(options[:from]).find('a').click
+ select_select2_result(value)
+ end
+
+ def select_select2_result(value)
+ #p %Q{$("div.select2-result-label:contains('#{value}')").mouseup()}
+ sleep(1)
+ page.execute_script(%Q{$("div.select2-result-label:contains('#{value}')").mouseup()})
+ end
+
+ def find_label_by_text(text)
+ label = find_label(text)
+ counter = 0
+
+ # Because JavaScript testing is prone to errors...
+ while label.nil? && counter < 10
+ sleep(1)
+ counter += 1
+ label = find_label(text)
+ end
+
+ if label.nil?
+ raise "Could not find label by text #{text}"
+ end
+
+ label ? label['for'] : text
+ end
+
+ def find_label(text)
+ first(:xpath, "//label[text()='#{text}']")
+ end
+end