[英]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.