我的测试数据库对象不能被Capybara Selenium WebDriver查看。有没有人看到我的设置有问题或我可能错过了什么?Capybara Selenium WebDriver没有看到我的测试数据
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
错误
..............#<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"}
我见过的其他物品,但他们不解决我的问题。据我所知,他们在不同的线程中运行,但我觉得上面的配置已经采取的是照顾......
Capybara with :js => true causes test to fail
*****更新的代码*****
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()
末
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
错误
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)>'
在56.01秒成品(文件采取6.33秒加载) 1例如,1失败
页面上元素的id实际上是10534吗?而不是像resource_10534? –
好问题,这是查看代码<%= resource.text %>
–