2013-07-31 126 views
3

我們正試圖將水豚引入我們的rspec示例中,並且沒有太多運氣。我不知道這是否是confusion between capybara and rails integration testing的一部分,但...爲什麼水豚不能找到表單元素?

這裏的RSpec的例子:

require 'spec_helper' 

describe "planning a trip", :type => :feature do 
    before :each do 
    FactoryGirl.create(:user) 
    end 

    it "creates a new trip" do 
    visit '/trips/new' 
    save_and_open_page 
    within("#new_trip") do 
     fill_in '#trip_from_place_nongeocoded_address', :with => '730 w peachtree st, atlanta, ga' 
     fill_in '#trip_to_place_nongeocoded_address', :with => 'georgia state capitol, atlanta, ga' 
    end 
    click_link 'Plan it' 
    expect(page).to have_content 'Success' 
    end 
end 

和這裏的HTML的相關部分,從水豚的save_and_open_page:

<form accept-charset="UTF-8" action="/trips" class= 
    "simple_form form-horizontal" id="new_trip" method="post" 
    novalidate="novalidate"> 

    <div class= 
    "control-group string required trip_from_place_nongeocoded_address"> 
    <label class="string required control-label" for= 
    "trip_from_place_nongeocoded_address"><abbr title= 
    "required">*</abbr> From</label> 

     <div class="controls"> 
     <input class="string required" id= 
     "trip_from_place_nongeocoded_address" name= 
     "trip[from_place][nongeocoded_address]" placeholder= 
     "Enter address" size="50" type="text"> 
     </div> 
    </div> 
[...etc...] 

但在運行RSpec的例子失敗:

1) planning a trip creates a new trip 
    Failure/Error: fill_in '#trip_from_place_nongeocoded_address', :with => '730 w peachtree st, atlanta, ga' 
    Capybara::ElementNotFound: 
     Unable to find field "#trip_from_place_nongeocoded_address" 
    # ./spec/features/plan_a_trip_spec.rb:12:in `block (3 levels) in <top (required)>' 
    # ./spec/features/plan_a_trip_spec.rb:11:in `block (2 levels) in <top (required)>' 

建議?

+0

我不知道爲什麼水豚不能找到那個領域,但你應該能夠告訴水豚你的標籤的名字,而不是o f選擇器到DOM元素。 'fill_in'From',:with =>'...''。水豚會找到那些作爲他們標籤文本的字段,看起來您已經設置好了。我更喜歡這也是因爲它反映了用戶會做什麼。 –

+0

但是如果你有本地化?你認爲默認的語言環境? – denishaskin

回答

1

據我所知,如果你的輸入有:

id="trip_from_place_nongeocoded_address" 

然後,你需要:

fill_in 'trip_from_place_nongeocoded_address' 

...沒有#

Capybara: How do I fill in a input field by its ID

+0

是的,我沒有仔細閱讀API文檔。我認爲這是一個CSS或xpath定位器。不是。 – denishaskin