2016-09-23 92 views
0

我在使用水豚選擇自動填充字段時遇到問題。水豚/ Poltergeist無法從jQuery自動填充字段中選擇

我有下面的代碼:

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 

我還有下一個測試:

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 

和錯誤是:

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

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

而且我想下一個測試:

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

隨着下一個方法:

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 

這樣的錯誤是:

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 

在這兩個變體的形式與「LOND」填充在,但不選擇任何可用的變體之一。

謝謝!

回答

0

我不確定你想用所有execute_script的東西來完成什麼。假設您使用的是jQueryUI autocomplete,那麼只需將密鑰發送到文本輸入,然後單擊顯示的選項即可。所以,你的方法應該只是

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 

這裏是你可以運行它顯示一個工作要點 - https://gist.github.com/twalpole/a12aeef278c9c714b9e078ce2adfda99

+0

非常感謝,這就是工作! – verrom

相關問題