Watir-webdrive如何自动化谷歌地方自动完成

时间:2014-10-20 15:49:23

标签: ruby autocomplete google-api watir-webdriver

我们正在使用此Google API https://developers.google.com/places/documentation/autocomplete,我希望自动化测试用例以选择特定地址。

<fieldset class="control-group">
<a class="button small grey search-again" style="display: none;" href="#"> Erneut versuchen </a>
<input class="address-search" type="text" data-required="true" placeholder="Geben Sie Ihre Adresse ein" autocomplete="off">

这是需要填写的文本字段。 我试过了

@address_page.address_line1 = line1 
text_field(:address, :class => 'address-search')

但它只是打开建议列表而没有选择我在输入中给出的那个,我有这个错误

Element is not currently visible and so may not be interacted with (Selenium::WebDriver::Error::ElementNotVisibleError)
      [remote server] file:///var/folders/zg/1303qv_56kjc0r43rpb9h0b40000gp/T/webdriver-profile20141020-26884-1it1xd2/extensions/fxdriver@googlecode.com/components/command_processor.js:10816:in `DelayedCommand.prototype.checkPreconditions_'

Ant建议如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

使用Google自动填充字段时,您需要:

  1. 在文本字段中输入内容
  2. 等待显示建议列表。这是为了避免Watir在加载之前尝试与建议进行交互的可能性。
  3. 单击代表建议的div元素之一。
  4. 以下是使用Google Maps JavaScript API v3示例页面的工作示例:

    # Go to the page
    browser = Watir::Browser.new
    browser.goto('http://code.google.com/apis/maps/documentation/javascript/examples/places-autocomplete.html')
    
    # For the test page, the places autocomplete is in an iframe
    iframe = browser.div(id: 'gc-content').iframe
    
    # Get the autocomplete field
    autocomplete = iframe.text_field(id: 'pac-input')
    
    # Type something into the autocomplete field
    autocomplete.set('Aus')
    
    # Wait for the list of suggestions to be displayed
    suggestion_menu = iframe.div(class: 'pac-container')
    suggestion_menu.wait_until_present
    
    # Click one of the suggestions, in this case, we clicked the second suggestion
    suggestion_menu.div(class: 'pac-item', index: 1).click
    
    # Check that the autocomplete value is updated
    p autocomplete.value
    #=> "Australian Capital Territory, Australia"
    

    请注意,您可能需要针对特定​​实现调整脚本。例如,文本字段id可能不是'pac-input'。但是,一般概念应适用于此和许多其他自动填充字段。

相关问题