Rspec 如何使用Capybara+;闹鬼

Rspec 如何使用Capybara+;闹鬼,rspec,capybara,poltergeist,summernote,Rspec,Capybara,Poltergeist,Summernote,我有一个正在使用的textarea,我将onChange事件设置为它。我想用RSpec+Capybara+Poltergeist编写测试,以确认onChange事件正在工作 据我所知,浏览器中显示的文本区域实际上是带有“note editable”css类的div标记。如何设置文本并触发onChange事件 我编写了这样的代码,但出现了一个错误Capybara::ElementNotFound:无法找到字段“somelabel”: 编辑 我为此问题创建了一个示例应用程序: 原始HTML RSp

我有一个正在使用的textarea,我将onChange事件设置为它。我想用RSpec+Capybara+Poltergeist编写测试,以确认onChange事件正在工作

据我所知,浏览器中显示的文本区域实际上是带有“note editable”css类的div标记。如何设置文本并触发onChange事件

我编写了这样的代码,但出现了一个错误
Capybara::ElementNotFound:无法找到字段“somelabel”

编辑 我为此问题创建了一个示例应用程序:

原始HTML RSpec
Capybaras fill_in仅适用于html表单元素。由于页面的JS版本使用了一个带有contenteditable属性的DIV作为要填充的文本区域,因此填充将不起作用。相反,您需要找到div并直接调用#set。在您的项目中,以下内容应该有效

find('div[contenteditable]').set('This is awesome blog.')

将Summernote更新到0.8.2后,
find('div[contenteditable]').set('This awesome blog')
不起作用

我不得不通过
execute\u script
调用Summernote API,比如:

script = <<-JS
$('.some-field').summernote('editor.insertText', 'This is awesome blog.');
JS
execute_script(script)

你能给我们看完整的html吗?我添加了详细的信息。如果您需要更多,请让我知道。我主要使用ID和类作为定位器,但您可以在许多实例中使用名称,在您的情况下,请尝试
fill__in'blog[content]“with:text here
在“#blog_content”中填入:text here
在测试时拖放到JS以设置值应该是绝对的最后一个选项,因为它完全绕过了用户在使用应用程序时生成的事件,使您的测试几乎毫无意义。Summernote 0.8.2中没有任何东西可以改变它的行为-这里有一个要点显示它仍然在与恶鬼一起工作-所以我假设你的应用程序中发生了其他变化来破坏你的测试。你是对的。当我编辑时,我不得不调用
execute\u script
在我的规范中触发onChange回调。这是因为Poltergeist在设置元素值时不使用send\u键(可能很快就会改变)。要在不执行JS的情况下获得所需内容,请执行类似于
find('div[contenteditable]')的操作。发送_键([:ctrl,'a'],'This is awesome blog')
您可能需要执行
find('div[contenteditable]')。首先单击
,但这应该会触发更改事件。(注意:您可能需要将:ctrl替换为:meta或:alt,具体取决于您所在的平台,或者如果以前没有任何内容需要替换,则根本不需要[:ctrl,'a'])
$ ->
  $('.summernote').summernote()
require 'rails_helper'

feature 'Blogs' do
  scenario 'Create blog' do
    visit new_blog_path
    fill_in 'Title', with: 'Hello, world!'
    fill_in 'Content', with: 'This is awesome blog.'
    click_button 'Create Blog'
    expect(page).to have_content 'Blog was successfully created.'
    expect(page).to have_content 'Hello, world!'
    expect(page).to have_content 'This is awesome blog.'
  end

  scenario 'Create blog with JS', js: true do
    visit new_blog_path
    fill_in 'Title', with: 'Hello, world!'
    pending 'Capybara::ElementNotFound: Unable to find field "Content"'
    fill_in 'Content', with: 'This is awesome blog.'
    click_button 'Create Blog'
    expect(page).to have_content 'Blog was successfully created.'
    expect(page).to have_content 'Hello, world!'
    expect(page).to have_content 'This is awesome blog.'
  end
end
find('div[contenteditable]').set('This is awesome blog.')
script = <<-JS
$('.some-field').summernote('editor.insertText', 'This is awesome blog.');
JS
execute_script(script)
find('div[contenteditable]').send_keys('This is awesome blog.')