Capybara testing editablegrid features not working - capybara

Using editablegrid.net. Really like the layout and functionality.
The only problem is feature testing.
Nothing seems to work in capybara - page snap doesn't show the page content for the actual grid, matchers dont see content rendered in js.
I have to use an older phantom (v1.9.2) as support for capybara doesn't seem to be there on mac os x yet.
Anyone had any luck feature testing?
Things tried
page refresh: page.driver.browser.navigate.refresh
made sure turned on using turbolinks events for load
tried selenium and poltergeist
Setup details
versions
rails 4.1.0
capybara (2.9.1)
rspec (3.5.0)
phantomjs 192
selenium-webdriver (2.53.4)
poltergeist (1.5.1)
capybara (~> 2.1)
cliver (~> 0.3.1)
multi_json (~> 1.0)
websocket-driver (>= 0.2.0)
$ phantomjs -v
1.9.2
UPDATE
updated versions
$ phantomjs -v
2.1.1
$ gem list | grep polter
poltergeist (1.10.0)
$ selenium-webdriver (2.53.4)
however when I do a simple test
visit 'address_books/'+user.address_book.id.to_s
page.save_screenshot("file1.png", :full => true)
sleep 4.seconds # just in case the delayed redraw takes time
page.save_screenshot("file2.png", :full => true)
expect(page).to have_content(/Person A.*2 8888 9999.*home/)
where this page includes an editablegrid that works fine in development (See example screen shot (http://imgbox.com/rqwx1vHe) ) doesn't render in the save screen shots ( http://imgbox.com/t1CG6hQa )
other notes
I tried selenium also, same problem : it is clear that the grid does not display.
I have used turbo links load for the event to trigger page load.
grid code
var editableGrid = null;
$(document).on('turbolinks:load', function() {
editableGrid = new EditableGrid("AddressBookGridJSON");
editableGrid.tableLoaded = function() { this.renderGrid("tablecontent", "testgrid"); };
editableGrid.loadJSON('/address_books.json');
// $.getJSON('/address_books.json', function(json) {
// editableGrid.load({"metadata": json.metadata, "data": json.data});
// console.log(editableGrid);
// });
});
UPDATE 2
index.json.jbuilder
json.set! :metadata do
json.array! #address_book.generate_view_table_meta_data
end
json.set! :data do
json.array! #contacts do |contact|
json.id contact.id
json.set! :values do
json.name contact.name
json.phone_number contact.get_presentation_number
json.phone_type contact.get_humanize_phone_type
json.action contact.get_url_to_view_delete
end
end
end
UPDATE 3
Data base cleaner code loaded
require 'database_cleaner'
RSpec.configure do |config|
config.before(:each) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
#DatabaseCleaner.strategy = :transaction
# if we use transaction the data is NOT saved during the actual rspec scenario. we must have commit
# during rspec so external compare processs can access data
# TODO make this conditional based on the compare plans process only
# tests will not have :nontransactional defined except compareplans testing
if self.class.metadata[:nontransactional]
DatabaseCleaner.strategy = :truncation
else
DatabaseCleaner.strategy = :transaction
end
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.append_after(:each) do
DatabaseCleaner.clean
end
end

Related

Puma causes Ruby 3 Debug to show ANSI escape sequences when pressing arrow keys (Mac ZSH)

I'm on Mac OS 12.3.1 using Z shell.
When using the Ruby Debug gem (1.5) in Puma 5.6.4 with Ruby 3.0.0 the arrow keys do not work and instead print out the ANSI escape sequences (e.g. ^[[A for up). If I understand correctly, Puma is running on /bin/sh, which doesn't support the arrow keys.
Certainly I'm not the only one trying to debug a Rails 7 app on a Mac. Anyone else run into this and have a workaround?
I have same problem with debug gem. I found it is caused by databasecleaner.
My workaround is
RSpec.configure do |config|
config.before(:suite) do
#DatabaseCleaner[:mongoid].clean_with(:deletion)
end
config.before(:each) do
# DatabaseCleaner[:mongoid].strategy = :deletion
end
config.before(:each, :js => true) do
#DatabaseCleaner[:mongoid].strategy = :deletion
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
# DatabaseCleaner.clean
end
config.before(:all) do
# DatabaseCleaner.start
end
config.after(:all) do
# DatabaseCleaner.clean
end
end

Clean out, or reset test-database with Rspec and Mongoid 5.0 on Rails 4

When I run my rspec tests, many fail due to stale data in my mongodb database. AFAIK it is far better to test with a clean database.
How can I clean and/or re-seed the database before each test?
You can use database_cleaner gem to accomplish this task.
From their documentation:
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do |example|
DatabaseCleaner.strategy= :truncation
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
As you are using Mongoid ORM, you may also need to specify it explicitly:
# How to setup your ORM explicitly
DatabaseCleaner[:mongoid].strategy = :truncation
Update:
I see an open issue for MongoID 5
To make it work, you can monkey patch the Mongo Ruby driver class as mentioned in the issue.
module Mongo
class Collection
class View
def remove_all
remove(0)
end
end
end
end
Athough it's not a great solution!
The fix to the problem is there in the master branch of database_cleaner(1.4.1) gem. Install the gem from master to fix the problem (until there is the version bump). Expected to get fixed in next version.
gem 'database_cleaner', :git => 'https://github.com/DatabaseCleaner/database_cleaner.git'

RSpec - shared DB conn. & transactional fixtures not working

I tried to follow José Valim's advice on faster test suites, but to no avail.
I use Spork and put the AR monkey patch in the Spork.each_run block (see the spec helper below).
However, my request specs fail because the database is not cleaned between runs - specifically, I get errors such as expected 1 record, got X when doing assertions like Model.should have(1).record.
Update
The problem lies with request specs using Javascript. See the following spec - it fails when I use js: true, but not if I remove that (I use RSpec's config.treat_symbols_as_metadata_keys_with_true_values = true, fwiw.)
# encoding: UTF-8
require 'spec_helper'
feature "Create a shift", :js do
scenario %q(
In order to plan the workload
As a coordinator
I want to schedule shifts
And I want to see when they're scheduled for
) do
visit shifts_path
click_link "new_shift_#{Date.current}"
fill_in 'shift_note', with: 'Casper - luk'
click_link_or_button 'submit'
Shift.should have(1).record
Shift.last.begins_at.should == Date.current
page.should have_selector("ol[data-date=\"#{Date.current}\"] li#shift_#{Shift.last.id}")
end
end
I can tell it's related to the DB not being cleaned, because it fails the first time (expected 1 record, got 0), passes the second time (because there's 1 record in the DB) and then fails again on any subsequent runs (expected 1 record, got 2 etc.)
I'm trying to avoid using a gem like DatabaseCleaner, to keep dependencies as few as possible, and to avoid a speed decrease in the test suite.
Any help/info/pointers are greatly appreciated!
Relevant info:
Rails 3.2.2
RSpec 2.9.0
Capybara 1.1.2
Capybara-webkit 0.11.0
FactoryGirl 2.6.4
Spork 0.9.0
Guard 1.0.1
Guard-spork 0.5.2
on a Macbook Air, OS X 10.7.3 (if that's relevant)
And my spec helper:
# encoding: UTF-8
require 'rubygems'
require 'spork'
Spork.prefork do
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rspec'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.use_transactional_fixtures = true
config.treat_symbols_as_metadata_keys_with_true_values = true
config.infer_base_class_for_anonymous_controllers = false
config.include Factory::Syntax::Methods
Capybara.javascript_driver = :webkit
Rails.logger.level = 4 # speed - http://blog.plataformatec.com.br/tag/capybara/
end
end
Spork.each_run do
require 'factory_girl_rails'
class ActiveRecord::Base
mattr_accessor :shared_connection
##shared_connection = nil
def self.connection
##shared_connection || retrieve_connection
end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
end
After much investigating, the problem seems to be with specs using JS in general, and not really the AR monkey patch.
I've re-phrased the problem in a new question here: RSpec+Capybara request specs w/ JS not working

Database Cleaner mongomapper and cucumber

#/features/support/env.rb
...
require 'database_cleaner'
require 'database_cleaner/cucumber'
require 'database_cleaner/mongo_mapper/truncation'
DatabaseCleaner.strategy = :truncation
Before do
DatabaseCleaner.start
end
After do
DatabaseCleaner.clean
end
Result
Exception encountered by DatabaseCleaner in Cucumber After block: ActiveRecord::ConnectionNotEstablished
If I comment Before and After block - tests work fine without DatabaseCleaner
ree-1.8.7-2011.03
rails (2.3.12)
mongo_mapper (0.7.5)
cucumber (0.8.7)
cucumber-rails (0.3.2)
database_cleaner (0.6.7)
looks like it's still using ActiveRecord, try disabling it

FixtureReplacement with Cucumber

I am using Cucumber with Selenium, FixtureReplacement and DatabaseCleaner.
Funnily enough, my data I created with FixtureReplacement is not accessible from my tests.
I have added an own rails environment for selenium and I am using an own profile for my enhanced selenium features.
My cucumber setup for the selenium profile is:
Webrat.configure do |config|
config.mode = :selenium
config.application_environment = :selenium
end
Cucumber::Rails::World.use_transactional_fixtures = false
require "database_cleaner"
# Clean the database once when starting
DatabaseCleaner.clean_with :truncation
DatabaseCleaner.strategy = :truncation
Before do
DatabaseCleaner.start
include FixtureReplacement
end
After do
DatabaseCleaner.clean
end
# this is necessary to have webrat "wait_for" the response body to be available
# when writing steps that match against the response body returned by selenium
World(Webrat::Selenium::Matchers)
FixtureReplacement works well, I have tested it in the Rails console.
I am running my selenium features with:
RAILS_ENV=selenium cucumber -p selenium features/enhanced/test.feature
Does anybody know a solution to this problem?
Best regards
I wonder if you are using Database Cleaner correctly? In my env.rb, I am using it like this:
Before do
require 'database_cleaner'
require 'database_cleaner/cucumber'
DatabaseCleaner.strategy = :truncation
end
This works for me when using Factory Girl.
This had nothing to do with Fixtures. I thought I cannot access my data, because I couldn't login.
The following fixed it:
Cucumber + selenium fails randomly

Resources