2016-09-23 7 views
0

私はCapybaraでオートコンプリートフィールドから選択する際に問題があります。Capybara/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 

フォームが「ロンド」で充填されるが、利用可能な変異体の誰もが選択されていないされていない変異体。

ありがとうございます!

答えて

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

おかげでたくさん、働いていたthatsの! – verrom

関連する問題