[英]how to test / interact with AJAX autocomplete and Capybara / Poltergeist
我正在嘗試與位於以下網址的外部網站進行交互: http : //is.gd/LtgYEk我需要能夠使用id =“ textOrigen”填寫輸入,這里是html
<p>
<label class="form-label">Departing from:</label>
<span class="label-field">
<input type="text" autocomplete="off" onblur="onblur2('textOrigen');" onfocus="initID('textOrigen');" size="17" id="textOrigen" name="text" maxlength="40" style="position:static;color: #505050;">
<select style="display:none" onchange="clearValidate(); Origen();" class="validate[dynamic]" id="cmbOrigen" name="cmbOrigen">
<option value="-1" selected="selected">Origin</option>
</select>
<label class="label-error" id="lblerrorOrigen"></label>
</span>
</p>
我使用'capybara / poltergeist'編寫了一個簡單的ruby腳本
我無法復制瀏覽器的行為,即:單擊時,輸入字段的默認值會突出顯示,因此在您開始輸入時會被刪除。
我忘記了嘗試過的所有不同變體,但嘗試了許多。 我發現了另一個SO帖子 ,該帖子似乎有用,但並沒有幫助
這是填充此字段的方法的最新修訂:
def session.fill_autocomplete(field, options = {})
execute_script %Q{ $('##{field}').trigger('focus') }
fill_in field, with: options[:with]
execute_script %Q{ $('##{field}').trigger('focus') }
execute_script %Q{ $('##{field}').trigger('keydown') }
selector = %Q{#output div:contains('#{options[:with]}')}
execute_script "$(\"#{selector}\").mouseenter().click()"
end
正如我所寫的腳本非常簡單,其他唯一相關的部分是使用以下方式實例化會話時:
session = Capybara::Session.new(:poltergeist)
任何幫助將不勝感激。
我注意到使用正確版本的phantomjs是基礎。
盡管2.x已發布,但我注意到phantomjs 1.8.2的行為比預期的要多得多,並且沒有那么多的錯誤。
我目前正在RailsAdmin中成功測試自動完成字段, 沒有使用任何延遲技術 。
def fill_in_autocomplete(selector, text)
find(selector).native.send_keys(*text.chars)
end
def choose_autocomplete_entry(text)
find('ul.ui-autocomplete').should have_content(text)
page.execute_script("$('.ui-menu-item:contains(\"#{text}\")').find('a').trigger('mouseenter').click()")
end
fill_in_autocomplete的示例選擇器為:
".author_field .ui-autocomplete-input"
經過多種測試后,我找到了解決方案。 關鍵是增加一些延遲以允許自動建議div填充。
這是有效的方法:
def session.fill_city(field, options = {})
sleep 3
script = %Q{ $("#{field}").focus().keypress().val("#{options[:with]}") }
execute_script(script)
sleep 2
find('#output').find('div').trigger('click')
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.