Capybara Selenium WebDriver没有看到我的测试数据

时间:2016-10-21 13:30:42

标签: selenium-webdriver rspec capybara

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次失败

2 个答案:

答案 0 :(得分:0)

您没有看到该资源,因为直到该页面已经加载之后才会创建该资源,但是您还没有真正在您的示例中使用Capybara,因为您已经完全绕过它并直接使用selenium实例。

get_driver中,当你想要一个与Capybara注册的驱动程序匹配的符号时,你将Capybara.current_driver设置为Selenium :: WebDriver的一个实例。在`login_user&#39;您正在创建一个存根对象(即,不是真实的/保存到数据库中)然后使用它来登录哪个不会工作,因为数据库中没有用户可以加载应用程序线程。我可能无法准确理解您尝试做什么,但我希望您的文件看起来更像

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

答案 1 :(得分:0)

将我的database_cleaner.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