简体   繁体   English

Capybara / Poltergeist无法从jQuery自动完成字段中选择

[英]Capybara/Poltergeist can't select from a jQuery autocomplete field

I have a problem with selecting from autocomplete field with Capybara. 我在从Capybara的自动完成字段中选择时遇到问题。

I have next code: 我有下一个代码:

def choose_autocomplete_result(text, input_id="input[data-autocomplete]")
  page.execute_script %Q{ $('#{input_id}').trigger("focus") }
  page.execute_script %Q{ $('#{input_id}').trigger("keydown") }
  sleep 1
  page.execute_script %Q{ $('.ui-menu-item a:contains("#{text}")').trigger("mouseenter").trigger("click"); }
end

And I have next test: 我还有下一个测试:

scenario 'Check autocomplete', js: true do
  find('.prop_address').native.send_keys 'lond'
  choose_autocomplete_result 'London', '.commercial_property_addresses_attributes_0_address'
  expect(page).to have_text('Some text')
end

And the error is: 错误是:

Failure/Error: find('#output').find('div').trigger('click')

     Capybara::ElementNotFound:
       Unable to find css "#output"

Also I tried next test: 我也尝试下一个测试:

scenario 'Check autocomplete', js: true do
  fill_autocomplete('.prop_address', with: 'lond', select: 'London')
  expect(page).to have_text('Some text')
end

With next method: 使用下一个方法:

def fill_autocomplete(css_id, options = {})
  find("#{css_id}").native.send_keys options[:with]
  page.execute_script %{ $('#{css_id}').trigger('focus') }
  page.execute_script %{ $('#{css_id}').trigger('keydown') }
  selector = %{ul.ui-autocomplete li.ui-menu-item:contains("#{options[:select]}")}
  expect(page).to have_selector('ul.ui-autocomplete li.ui-menu-item')
  page.execute_script %{ $('#{selector}').trigger('mouseenter').click() }
end

In this way error is: 这样,错误是:

Failure/Error: expect(page).to have_selector('ul.ui-autocomplete li.ui-menu-item')
       expected to find css "ul.ui-autocomplete li.ui-menu-item" but there were no matches

In both variants the form is filled in with 'lond', but no one of the available variants is not selected. 在这两种变体中,表格均以“ lond”填充,但未选择任何可用的变体。

Thanks! 谢谢!

I'm not sure what you're trying to accomplish with all the execute_script stuff. 我不确定您要用所有execute_script东西来完成什么。 Assuming you're using the jQueryUI autocomplete all that should be required is to send keys to the text input and then click on the shown choice. 假设您正在使用jQueryUI自动完成功能 ,那么所需要做的就是将键发送到文本输入,然后单击显示的选项。 So your method should just be 所以你的方法应该是

def fill_autocomplete(css_selector, options = {})
  find(css_selector).send_keys options[:with]   # No .native. needed
  find("ul.ui-autocomplete li.ui-menu-item", text: options[:select]).click()
end

Here's a gist you can run showing it working - https://gist.github.com/twalpole/a12aeef278c9c714b9e078ce2adfda99 这是您可以运行显示其要点的要点-https: //gist.github.com/twalpole/a12aeef278c9c714b9e078ce2adfda99

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 参考错误:找不到变量:jQuery with Poltergeist/Capybara - ReferenceError: Can't find variable: jQuery with Poltergeist/Capybara 无法从jQuery自动完成中选择建议 - Can't select suggestion from jQuery autocomplete 如何从Capybara / Poltergeist测试中触发JQuery的“更改”功能 - How to trigger a JQuery 'change' function from a Capybara/Poltergeist test 黄瓜/水豚测试无法区分Jquery自动完成表单 - Cucumber/Capybara Tests can't tell Jquery Autocomplete Forms Apart 如何让jQuery与PhantomJS,Poltergeist和Capybara一起工作 - How to have jQuery work with PhantomJS, Poltergeist and Capybara Select 第一个字段来自 jQuery 自动完成 function 自动完成 - Select the first field from jQuery autocomplete function automatically 无法从 jquery 自动完成中删除项目符号 - Can't remove bullets from jquery autocomplete jQuery UI自动完成-无法获取在输入字段中键入的数据 - JQuery UI AutoComplete - can't get data typed in the input field jQuery AutoComplete字段不起作用,如何解决? - jQuery AutoComplete field won't work, how can I fix it? jquery:如果用户未从自动完成列表中选择数据,则jQuery自动完成清除文本框 - jquery : jquery autocomplete clear text box if user does't select data from autocomplete list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM