2016-10-21 28 views
0

我的测试数据库对象不能被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失败

+0

页面上元素的id实际上是10534吗?而不是像resource_10534? –

+0

好问题,这是查看代码<%= resource.text %>

回答

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 
0

你没有看到这个资源,因为它没有被创建,直到这个页面已经被加载完毕,然而你也没有真正在你的例子中使用Capybara,因为你完全绕过了它并直接使用了selenium实例。

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

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 
+0

你的反应清除了很多水豚硒混乱,谢谢。我清理了代码并发布了更新。我使用的是底层用户b/c,表格是在不同的模式中,并且用户实际上已经存在。 –

+0

但是,我仍然没有看到该资源。我看到它在测试中创建(在puts'测试脚本计数之后'),但在浏览器启动时,它不能在索引方法中查看。我仍然认为我错过了配置的东西。 –

+0

在login_user的最后我的回答中指出,在页面上添加一项检查以确保用户实际登录,并检查test.log以确认登录正在发生。另外,每个场景都是独立的,所以你的login_user需要在before(:each)之前(:all)之前 - 也是为什么你把'js:true'元数据关闭了? –

相关问题