Ruby Watir,Dijit Widget,从“选择列表”中设置值

时间:2013-11-14 03:30:32

标签: ruby set watir dojo selectlist

我希望能够点击(查看可用的选项/帐户列表,似乎一开始就是隐藏的),然后设置一个dijit小部件“select_list”到某个值。

我可以使用

找到对象
@@ie.element(:css, "#accountSwitcherSelect.dijitDownArrowButton").flash
#works

但不能使用点击或设置:

@@ie.element(:css, "#accountSwitcherSelect.dijitDownArrowButton").click
#no errors, but doesn't do anything

@@ie.element(:css, "#accountSwitcherSelect.dijitSelectLabel").set("Account2")    
#NoMethodError: undefined method `set' for <Watir::HTMLElement:0x4d6af60>

@@ie.element(:css, "#accountSwitcherSelect.dijitSelectLabel").send_keys("Account3")
#NoMethodError: undefined method `send_keys' for <Watir::HTMLElement:0x4d6af60>

过去网站是使用普通的html实现的,并且有一个select_list,所以

@@ie.select_list(:name, "link").set(/Account1/i)

工作得很好。

以下是该网站背后的当前HTML:

<table id="accountSwitcherSelect" class="dijit dijitReset dijitInline dijitLeft dijitDownArrowButton dijitSelectFixedWidth dijitValidationTextBoxFixedWidth dijitSelect dijitValidationTextBox" lang="en-US" cellspacing="0" cellpadding="0" aria-haspopup="true" role="listbox" data-dojo-attach-point="_buttonNode,tableNode,focusNode" style="-moz-user-select: none; width: 207px;" tabindex="0" widgetid="accountSwitcherSelect" aria-expanded="false" aria-invalid="false">
  <tbody role="presentation">
    <tr role="presentation">
      <td class="dijitReset dijitStretch dijitButtonContents" role="presentation">
        <div class="dijitReset dijitInputField dijitButtonText" role="presentation" data-dojo-attach-point="containerNode,_popupStateNode" popupactive="true">
          <span class="dijitReset dijitInline dijitSelectLabel dijitValidationTextBoxLabel " role="option">Account1</span>
        </div>
        <div class="dijitReset dijitValidationContainer">
      </td>
      <td class="dijitReset dijitRight dijitButtonNode dijitArrowButton dijitDownArrowButton dijitArrowButtonContainer" role="presentation" data-dojo-attach-point="titleNode">
        <input class="dijitReset dijitInputField dijitArrowButtonInner" type="text" role="presentation" readonly="readonly" tabindex="-1" value="? ">
      </td>

1 个答案:

答案 0 :(得分:0)

假设“选择列表”与digit.form.Select documentation page上的类似,您需要:

  1. 单击下拉箭头(输入元素)
  2. 单击包含所需文本的单元格(td元素)
  3. 在您的情况下,它看起来像:

    @@ie.input(:class => 'dijitArrowButtonInner').click
    @@ie.td(:class => 'dijitMenuItemLabel', :text => /Account1/i).click
    

    这是一个工作示例(虽然它依赖于仍在线提供的dijit css文件)。

    require 'watir-webdriver'
    
    browser = Watir::Browser.new
    browser.goto "data:text/html,#{DATA.read}"
    browser.input(:class => 'dijitArrowButtonInner').click
    browser.td(:class => 'dijitMenuItemLabel', :text => 'Arizona').click
    sleep(10)  # Just so that you can see the dropdown has changed
    browser.close
    
    __END__
    
    <html>
        <head>
            <link rel="stylesheet" href="https://dojotoolkit.org/reference-guide/1.9/_static/js/dijit/themes/claro/claro.css">
            <script>dojoConfig = {async: true, parseOnLoad: true}</script>
            <script src="https://dojotoolkit.org/reference-guide/1.9/_static/js/dojo/dojo.js"></script>
            <script>require(["dojo/parser", "dijit/form/Select"]);</script>
        </head>
        <body class="claro">
            <div name="select3" value="AL" data-dojo-type="dijit/form/Select">
                <span value="AL"><b>Alabama</b></span>
                <span value="AZ"><i>Arizona</i></span>
            </div>
        </body>
    </html>
    

    <强>的Watir古典

    上述解决方案适用于watir-webdriver,但不适用于watir-classic。单击下拉列表(实际上是表格)不会触发足够的事件来打开下拉列表。作为一种解决方法,似乎您可以使用send_keys来触发正确的事件。

    假设browser对具有上述html的页面打开,以下内容将起作用:

    browser.table(:id => 'dijit_form_Select_0').send_keys :enter
    browser.td(:class => 'dijitMenuItemLabel', :text => 'Arizona').click
    

    请注意,此代码中的表格是选定的选项(关闭下拉列表时)。