2014-01-29 57 views
6

你如何使用水豚与contenteditable="true"测试元素?水豚和contenteditable

有了这个天赋...

scenario "Using valid input" do 
    fill_in "name", with: "Zinn" 
    click_button "Update" 
    expect(page).to have_content("Update successful!") 
end 

...这生态文件...

<td id="name" contenteditable="true"><%= @name %></td> 
<td><button id="update" class="btn btn-sm btn-default">Update</button></td> 

我得到这个失败规范...

Capybara::ElementNotFound: 
    Unable to find field "name" 

回答

8

这里的我怎么最终解决了这个问题。

feature "Editing", js: true do 

    scenario "with valid input" do 
    el = find(:xpath, "//div[@contenteditable='true' and @name='name']") 
    el.set("Zinn") 
    el.native.send_keys(:return) 
    expect(page).to have_content("Update successful!") 
    expect(page).to have_content("Zinn") 
    end 

end 

根据this thread它看起来像你应该能够使用fill_in助手上CONTENTEDITABLE div但我不能得到那个工作。

+0

这似乎不适用于水豚2.4.1。使用'el.text'或'have_content'检查文本都显示原始文本。 – BM5k

+0

我应该补充一点,我使用phantomjs 1.9.7和poltergeist 1.5.1 – BM5k

+0

在Poltergeist JS中,它将是:'el.native.send_keys(:Enter)',而不是':return'。 – Mike