为什么这些水豚测试在每个单独运行时都有效,但在两个运行时都没有

时间:2014-08-03 01:52:04

标签: ruby selenium rspec capybara

每项测试都是单独进行的,即

rspec spec/features/visit_home_page_root_spec.rb:15

rspec spec/features/visit_home_page_root_spec.rb:22

但是如果同时运行它们,即

rspec spec/features/visit_home_page_root_spec.rb

第一个失败:

  1) Add and edit lets me edit a group
     Failure/Error: find('div#side div a', text: 'New Group').click
     Capybara::ElementNotFound:
       Unable to find css "div#side div a" with text "New Group"
     # ./spec/features/visit_home_page_root_spec.rb:16:in `block (2 levels) in <top (required)>'

Finished in 0.65388 seconds
2 examples, 1 failure

为什么会这样?

require 

describe "Add and edit", :type => :feature do

  before :all do
    User.create(:username => 'rubdubdub@google.com', :password => 'esceptionalitynessish')
    visit '/ladmin/login'
    fill_in 'username', :with => 'rubdubdub@google.com'
    fill_in 'password', :with => 'esceptionalitynessish'
    find('input[value="Login"]').click
    expect(page).to have_content 'Logout'
    expect(page).to have_no_content 'Login'
  end 

  it "lets me add a group" do
    find('div#side div a', text: 'New Group').click
    fill_in 'group[group_name]', with: 'Group Add'
    click_button 'Save'
    expect(page).to have_content('Group Add')
  end 

  it "lets me edit a group" do
    visit root_path
    find('div#side div a', text: 'New Group').click
    fill_in 'group[group_name]', with: 'Group Edit'
    click_button 'Save'
    visit root_path
    find('div#side a', text: "Groups").click
    find('div#main a[1]', text: "Edit").click
    fill_in 'group[group_name]', with: 'Group Edit changed'
    click_button 'Save'
    expect(page).to have_content('Group Edit changed')
  end 

end

1 个答案:

答案 0 :(得分:0)

使用before :all创建用户一次但使用before :each

登录两次解决了问题

所以我改变了

before :all do
  User.create(:username => 'rubdubdub@google.com', :password => 'esceptionalitynessish')
  visit '/ladmin/login'
  fill_in 'username', :with => 'rubdubdub@google.com'
  fill_in 'password', :with => 'esceptionalitynessish'
  find('input[value="Login"]').click
  expect(page).to have_content 'Logout'
  expect(page).to have_no_content 'Login'
end 

before :all do
  User.create(:username => 'rubdubdub@google.com', :password => 'esceptionalitynessish')
end 

before :each do
  visit '/ladmin/login'
  fill_in 'username', :with => 'rubdubdub@google.com'
  fill_in 'password', :with => 'esceptionalitynessish'
  find('input[value="Login"]').click
  expect(page).to have_content 'Logout'
  expect(page).to have_no_content 'Login'
end