I have the following test that works in Rack::Test but not using Selenium. I.e. if I add , js: true to the describe block, I get an error message in Firefox saying that it couldn't find the License with id=(the id of #l)
describe "should hide allocation rule # for pdf & clickthrough licenses" do
it "reads current state and shows/hides fields appropriately" do
#l = FactoryGirl.create(:license:,
way: License::CLICK_WAY)
visit edit_admin_license_path(#l)
end
end
Why? I must be missing something. I can verify with Sequel Pro that the record is not getting saved when using js: true.
I need this spec to run in Selenium because I have javascript to test.
The simple solution was to turn transactional fixtures off.
Why does my Cucumber test fail when run with Selenium?
in spec/spec_helper.rb:
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before :each do
if Capybara.current_driver == :rack_test
DatabaseCleaner.strategy = :transaction
else
DatabaseCleaner.strategy = :truncation
end
DatabaseCleaner.start
end
config.after do
DatabaseCleaner.clean
end
end
and in the Gemfile, test section
gem 'database_cleaner'
Related
Here is my setup:
airbrake.rb
require 'airbrake'
Airbrake.configure do |c|
c.ignore_environments = [:test, :development]
c.project_id = ENV['PROJECT_ID']
c.project_key = ENV['PROJECT_KEY']
end
use Airbrake::Rack::Middleware
spec_helper.rb
RSpec.configure do |config|
config.before(:suite) do
FactoryGirl.reload
FactoryGirl.define do
to_create { |instance| instance.save }
end
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
Airbrake.configure(:test) do |c|
c.project_id = ENV['PROJECT_ID']
c.project_key = ENV['PROJECT_KEY']
end
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
config.include FactoryGirl::Syntax::Methods
end
worker_test_spec.rb
require 'spec_helper'
RSpec.describe NotificationWorker do
it "perform should call Airbrake#notify" do
anotification_worker = LNotificationWorker.new
airbrake_notification_worker.perform("some error message"))
expect(Airbrake).to receive(:notify).with("some error message")
end
end
I call Airbrake#notify in other (non-Sidekiq) tests, and they find the appropriate ENV variables just fine.
Yet if I run the above Sidekiq test with the above setup, I get the following error:
Airbrake::Error:
the 'default' notifier isn't configured
But if I change the Airbrake config in spec_helper.rb to:
Airbrake.configure do |c|
c.project_id = ENV['PROJECT_ID']
c.project_key = ENV['PROJECT_KEY']
end
the ENV keys are able to be found in the tests. Why is this?
When you say Airbrake.configure(:test), it does not mean "configure Airbrake for the test RAILS_ENV". Rather :test creates a non-default named notifier. Then you can send specific notifications to that notifier by saying Airbrake.notify("oops", {time: Time.now}, :test). But that is not about development/test/production, it is about categorizing your notifications.
So the problem is that you have configured a notifier named test, but you have not yet configured one named default, and default is what Airbrake wants to use when you don't tell it otherwise. That's why your spec passes when you say simply Airbrake.configure { ... }.
I'm writing Selenium tests, using Watir-Webdriver and RSpec, which can be a bit spotty when they're first being developed. I've run into a situation where I want to create something on the UI in before :all, however it can throw exceptions (based on timing or poor loading). When that happens I want to take a screenshot.
Here's what I have:
RSpec.configure do |config|
config.before(:all) do |group| #ExampleGroup
#browser = Watir::Browser.new $BROWSER
begin
yield #Fails on yield, there is no block
rescue StandardError => e
Utilities.create_screenshot(#browser)
raise(e)
end
end
end
I run it and get an error:
LocalJumpError: no block given (yield)
The reason I assumed yielding would work is RSpec's definition of before:
def before(*args, &block)
hooks.register :append, :before, *args, &block
end
How can I wrap the code I've put in my before :all in a begin/rescue block without having to do it on every suite?
Thanks in advanced.
The code you've written in the before hook is the &block you're referring to in RSpec::Hooks#before. The hook yields to your code, then runs your tests after the yield is complete.
As for how to make this work, I think this should do:
RSpec.configure do |config|
# ensures tests are run in order
config.order = 'defined'
# initiates Watir::Browser before all tests
config.before(:all) do
#browser = Watir::Browser.new $BROWSER
end
# executes Utilities.create_screenshot if an exception is raised by RSpec
# and the test is tagged with the :first metadata
config.around(:each) do |example|
example.run
Utilities.create_screenshot(#browser) if example.exception && example.metadata[:first]
end
end
This configuration requires the first test be tagged with metadata:
describe Thing, :first do
it "does something" do
# ...
end
end
This way, you'll only take a screenshot at the beginning of your run for a failing test, and not after every failing test. If you'd rather not mess with metadata (or prefer your tests are run in random order), you could do something like this:
RSpec.configure do |config|
# initiates Watir::Browser before all tests
config.before(:all) do
#test_count = 0
#browser = Watir::Browser.new $BROWSER
end
# executes Utilities.create_screenshot if an exception is raised by RSpec
# and the test is the first to run
config.around(:each) do |example|
#test_count += 1
example.run
Utilities.create_screenshot(#browser) if example.exception && #test_count == 1
end
end
This works for me. Instead of begin/rescue in the before :all hook,
config.after :each do
example_exceptions = []
RSpec.world.example_groups.each do |example_group|
example_group.examples.each do |example|
example_exceptions << !example.exception.nil?
end
end
has_exceptions = example_exceptions.any? {|exception| exception}
#Handle if anything has exceptions
end
So, with respect to integration testing using Capybara and RSpec, I know I can do this:
page.driver.browser.manage.window.resize_to(x,y)
per How to set Browser Window size in Rspec (Selenium) for specific RSpec tests, but is there a way to do this globally so that every test that is affected by media queries doesn't have to define this?
A proper way to do it for all js tests is to add following inside spec_helper.rb RSpec.configure block
config.before(:each, js: true) do
Capybara.page.driver.browser.manage.window.maximize
end
to maximize the window. Change to resize_to(x,y) to set any window size.
EDIT: If you happen to be using Poltergeist the correct way to do it is
config.before(:each, js: true) do
Capybara.page.driver.browser.resize(x,y)
end
You could define that under before(:all)
describe "Test" do
before(:all) do
...
...
page.driver.browser.manage.window.resize_to(x,y) #Mention it here
end
it "should find everything" do
...
end
after(:all) do
...
end
end
Perhaps due to a recent change in Capybara, what worked for me was:
before do
Capybara.page.current_window.resize_to(x, y)
end
For test runtime in Capybara 2.2.4 version you can achieve this by doing
before do
handle = Capybara.page.driver.current_window_handle
Capybara.page.driver.resize_window_to(handle, width, height)
end
Or
before do
Capybara.page.current_window.resize_to(width, height)
end
If you get Capybara::NotSupportedByDriverError: Capybara::Driver::Base#current_window_handle YOU MUST CHANGE YOUR DRIVER FOR EXAMPLE USE JAVASCRIPT DRIVER!
before do
Capybara.page.current_window.resize_to(width, height)
end
scenario js: true do
# your test here
end
#tirdadc if you're using Poltergeist, you can add something like this to your rails_helper.rb file:
Capybara.register_driver :poltergeist do |app|
options = {
# js_errors: true,
# cookies: true,
window_size: [320, 568] # iphone 5
}
Capybara::Poltergeist::Driver.new(app, options)
end
I am trying to integrate BDD in my rails app via rspec. I am using guard and spork-rails to speed the monitoring process up. I am getting this error:
An error occurred in an after hook
ActiveRecord::StatementInvalid: ArgumentError: prepare called on a closed database:
rollback transaction occurred at /Users/davidhahn/.rvm/gems/ruby-1.9.3-p286/gems/sqlite3-1.3.6/lib/sqlite3/database.rb:91:in initialize
I ran rake db:test:prepare and it ran without any errors. Since I'm using sqlite I checked to make sure that the user_man_test.sqlite file was in db/. My test is just a simple integration test:
require 'spec_helper'
describe "Authentication" do
describe "Login Page" do
it "should have the h1 'Welcome to User Management'" do
visit '/log_in'
page.should have_selector('h1', text: 'Welcome to User Management')
end
end
describe "Login" do
before { visit '/log_in' }
describe "with invalid information" do
before { click_button "Login" }
it { should have_selector('h1', text: 'Welcome to User Management') }
it { should have_selector('div.alert.alert-error', text: 'Invalid') }
end
end
end
My spec_helper.rb looks like:
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
config.order = "random"
end
end
Spork.each_run do
# This code will be run each time you run your specs.
end
~
Thanks for the help
Instead of spending hours trying to configure spork properly, I would advise you to look at Zeus. I'm sorry if this doesn't exactly answer your question, but I spent almost one year with spork, had tons of trouble configuring every time that I added a new test gem, and when I made the switch, everything magically worked (and in my experience, Zeus' performance is much better than Spork).
I'm having some problems using Sinatra with Capybara.
I want to test a pure javascript application. It's just a plain index.html that is being served by Sinatra.
require "sinatra"
get "/" do
File.read("public/index.html")
end
Let's say for example that I want to test this code.
$("a.link").click(function(){
$(this).replaceWith("New String");
});
Click me!
Then the test would look something like this.
describe "requests", js: true do
it "should display a message" do
visit "/"
click_link "Click me!"
page.should have_content("New String")
end
end
The problem is that nothing happens. According to Ryan Bates screencast Firefox should start and run the test if js: true is added to the describe block.
Here is my spec_helper file.
require "rspec"
require "capybara"
require "capybara/dsl"
Capybara.javascript_driver = :selenium
require_relative "./../server"
Capybara.app = Sinatra::Application
Capybara.javascript_driver = :selenium
Capybara.default_wait_time = 10
RSpec.configure do |config|
config.mock_with :rspec
config.include Capybara
end
Here is the output when running rspec rspec/request_spec.rb.
requests
should display a message (FAILED - 1)
Failures:
1) requests should display a message
Failure/Error: page.should have_content("New String")
expected #has_content?("New String") to return true, got false
# ./spec/request_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 4.38 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/request_spec.rb:2 # requests should display a message
I created an complete example project on Github that can be found here:
https://github.com/oleander/capybara-js-fails
Anyone knows why it fails?
Here is the original answer from Jonas Nicklas.
You need to require 'capybara/rspec' and set :type => :request.
See the Capybara README section on "Using Capybara with RSpec".
/Jonas
Here is a working example on Github.