0

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

2
  • Is the id of the element on the page actually 10534? and not something like resource_10534? Oct 21, 2016 at 15:05
  • Good question this is the view code <a id="<%= resource.id %>" href="<%= resource.url.html_safe %>" target="_blank"><%= resource.text %></a><br>
    – min_123
    Oct 21, 2016 at 15:23

2 Answers 2

0

You're not seeing the resource because it isn't being created until after the page has already been loaded, however you're also not really using Capybara in your examples since you're bypassing it completely and using selenium instances directly.

In get_driver you're setting Capybara.current_driver to an instance of Selenium::WebDriver when it's expecting a symbol matching one of the drivers registered with Capybara. In `login_user' you're creating a stubbed object (ie. not real/saved to the database) and then using that to login which won't work since theres no user in the database for the app thread to load. I may not be understanding exactly what you're trying to do but I would expect your files to look more like

external_user_viewing_resources_sel_spec.rb

require 'rails_helper'
require 'support/selenium_helper'

RSpec.feature "External user " do

before(:each) do
  login_user(:external_user)
end

scenario "can view and click resources (helpful links)", js: true do
  @d1_inst_resource = create(:d1_inst_resource)
  page.visit("whatever page should should show the record")
  page.find(:id, @d1_inst_resource.id").click();
  # some check for what should happen based on the click
end
end

selenium_helper.rb

def login_user(user)
  @user = create(user)
  page.visit "http://localhost:3000" #Is this meant to be a different system than the app under test? if not it should just be page.visit '/'
  page.fill_in("username", with: @user.email)
  page.fill_in("password", with: @user.password)
  page.find(:css, "button[type='submit']").click()
  #some check for whatever happens on the page to show the user has logge in
end
6
  • Your response cleared up a lot of Capybara Selenium confusion, thanks. I cleaned up the code and posted the updates. I'm using the subbed user b/c the table is in a different schema and the user actually already exists.
    – min_123
    Oct 22, 2016 at 3:43
  • However, i'm still not seeing the resource. I see that its created (after puts 'test script count') in the test but its not viewable in the index method when the browser launches. I still think i'm missing something with the configuration.
    – min_123
    Oct 22, 2016 at 3:47
  • Adding a check for something on the page as indicated in my answer at the end of login_user to make sure the user is actually logging in, and check your test.log to confirm login is happening. Additionally, every scenario is separate so your login_user needs to be in a before(:each) not before(:all) - also why did you take the js: true metadata off the scenario? Oct 22, 2016 at 5:25
  • The creation of the resource should be in the before(:each) too if you want it created for every scenario in the feature Oct 22, 2016 at 5:33
  • Thanks, i've updated the code. The above test shows that the user is logged in.
    – min_123
    Oct 22, 2016 at 13:48
0

Changing my database_cleaner.rb file to this fixed the issue, they are no longer running in two different threads.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.