My test database objects are not viewable by Capybara Selenium WebDriver. Does anyone see an issue with my setup or what I might be missing?
rails_helper.rb
...
RSpec.configure do |config|
config.use_transactional_fixtures = false
...
end
database_clearner.rb
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:all) do
DatabaseCleaner.start
end
config.before(:each, type: :feature) do
# :rack_test driver's Rack app under test shares database connection
# with the specs, so continue to use transaction strategy for speed.
driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test
if !driver_shares_db_connection_with_specs
# Driver is probably for an external browser with an app
# under test that does *not* share a database connection with the
# specs, so use truncation strategy.
DatabaseCleaner.strategy = :truncation
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
config.after(:all) do
DatabaseCleaner.clean
end
end
external_user_viewing_resources_sel_spec.rb
require 'rails_helper'
require 'support/selenium_helper'
RSpec.feature "External user " do
before(:all) do
get_driver
login_user(:external_user)
end
after(:all) do
#@driver.find_element(:id, "logout").click()
#quit_driver
end
scenario "can view and click resources (helpful links)", js: true do
@d1_inst_resource = create(:d1_inst_resource)
puts @d1_inst_resource.inspect
@driver.find_element(:id, 'helpful_links_panel')
@driver.find_element(:id, "#{@d1_inst_resource.id}").click();
end
end
selenium_helper.rb
require 'selenium-webdriver'
def get_driver
Capybara.current_driver = Selenium::WebDriver.for :firefox #:chrome
@driver = Capybara.current_driver
end
def quit_driver
@driver.quit
Capybara.use_default_driver
end
def login_user(user)
@user = build_stubbed(user)
@driver.get "http://localhost:3000"
@driver.find_element(:id, "username").send_keys("#{@user.email}")
@driver.find_element(:id, "password").send_keys("#{@user.password}")
@driver.find_element(:css, "button[type='submit']").click()
end
error
..............#<Resource id: 10534, text: "Text1", url: "MyText", position: 1, d1: true, d2: false, d3: false, conference: false, institution: true, start_date: "2016-10-06", end_date: "2020-10-06", created_at: "2016-10-21 13:28:15", updated_at: "2016-10-21 13:28:15">
F...*
Failures:
1) External user can view and click resources (helpful links)
Failure/Error: @driver.find_element(:id, "#{@d1_inst_resource.id}").click();
Selenium::WebDriver::Error::NoSuchElementError:
Unable to locate element: {"method":"id","selector":"10534"}
I've seen other articles but they do not solve my issue. I understand that they run in different threads, but I feel like the configuration above has taken care of that...
Capybara with :js => true causes test to fail
*****UPDATED CODE*****
selenium_helper.rb
def login_user(user)
@user = build_stubbed(user)
page.visit "/"
page.fill_in "username", with: @user.email
page.fill_in "password", with: @user.password
page.find("button[type='submit']").click()
end
external_user_viewing_resources_sel_spec.rb
require 'rails_helper'
require 'support/selenium_helper'
RSpec.feature "External user " do
before(:each) do
Capybara.current_driver = :selenium
@d1_inst_resource = create(:d1_inst_resource)
puts Resource.count
login_user(:external_user)
puts 'test script count'
puts Resource.get_resource_by_member_type_and_division(@user).count
end
scenario "can view and click resources (helpful links)", js: true do
puts page.first('.userName').text
expect(page.first('.userName').text).to eq("#{@user.first_name.upcase} #{@user.last_name.upcase}")
page.find(:id, "#{@d1_inst_resource.id}")
page.find(:id, "#{@d1_inst_resource.id}").click()
end
end
main_controller.rb
def index
@resources = Resource.get_resource_by_member_type_and_division(@user)
puts 'index query count'
puts @resources.count
@resources
end
error
1
index query count
0
test script count
1
REVDIST TUSER1
F
Failures:
1) External user can view and click resources (helpful links)
Failure/Error: page.find(:id, "#{@d1_inst_resource.id}")
Capybara::ElementNotFound:
Unable to find id "11060"
# /Users/meffinger1/.rvm/gems/ruby-2.2.5/gems/capybara-2.9.1/lib/capybara/node/finders.rb:44:in `block in find'
# /Users/meffinger1/.rvm/gems/ruby-2.2.5/gems/capybara-2.9.1/lib/capybara/node/base.rb:85:in `synchronize'
# /Users/meffinger1/.rvm/gems/ruby-2.2.5/gems/capybara-2.9.1/lib/capybara/node/finders.rb:33:in `find'
# /Users/meffinger1/.rvm/gems/ruby-2.2.5/gems/capybara-2.9.1/lib/capybara/session.rb:735:in `block (2 levels) in <class:Session>'
# ./spec/features/selenium/external_user_viewing_resources_sel_spec.rb:28:in `block (2 levels) in <top (required)>'
Finished in 56.01 seconds (files took 6.33 seconds to load) 1 example, 1 failure