【问题标题】:Watir: Need to double click on an element to open custom popupWatir:需要双击一个元素才能打开自定义弹出窗口
【发布时间】:2011-04-16 04:58:28
【问题描述】:

我是 WATIR 的新手。我面临的问题是—— 我正在测试的应用程序在页面上放置了缩略图(如 Windows 图标),我需要双击它。这样做时,将打开一个自定义弹出窗口(用 javascript 实现的 ajax 弹出窗口)。 fire_event("ondblclick") 对我不起作用。我也尝试了两次“点击”,但这也无济于事。有没有其他方法来处理这个?非常感谢您的帮助。

2010 年 7 月 6 日添加:

我解决了,但我现在有另一个问题。

下面是我能够使用“@ie.div(:class,'GridElementInlineIE').fire_event('ondblclick')”解决的 HTML


<div class="gridViewItem" style='display: inline-table;' ondblclick='openAsset("634119577077187500", "", "LIBRARY_ASSETS_TAB", "1", "A111");'
        id='GridComponent634119577077187500'>
        <table style="display: inline-table;" class="gridViewItemTable" cellpadding="0" cellspacing="0"
            onclick="highlightAsset(this, event)" projectid="" dmguid="634119577077187500"
            id="_thumb_634119577077187500" objectclass="VIDEO">
            <tr>
                <td style="padding: 10px 10px 0px 7px">
                    <img class="assetListGridImage draggableThumbnail" id="thumb_634119577077187500"
                        title="A111" alt="A111"
                        src="/images/wait.gif" dmguid="634119577077187500" projectid=""
                        objectclass="VIDEO" _onclick="highlightAsset(this, event)" />
                </td>
            </tr>
            <tr>
                <td style="padding: 0px 0px 0px 7px">

                    A111
                </td>
            </tr>
            <tr>
                <td style="padding: 0px 0px 5px 7px; min-height: 33px; max-height: 33px; height: 33px;">
                    <img alt='Not starred' name='IMAGE634119577077187500' title='Star this asset' src='/Images/star_off.png' onclick='toggleStar(event, this, "634119577077187500")' class='starGrid' />
                    <img alt='video' title='video' src='/Images/asset_type/VIDEO.png'/>
                    <img src='/images/shared.png'  title ='Shared' alt='Shared' />
                </td>

            </tr>
        </table>
    </div>

现在我需要双击这个项目(下面的代码)。但即使正在识别元素(以黄色突出显示),双击也不起作用。我正在尝试“@ie.div(:class,'gridViewItem').fire_event('ondblclick')”。我也尝试过 while 循环和单击两次选项无效。我正在使用带有 Ruby186-27_rc2 的 Watir 1.6。

           div class="GridElementInline">
                <table class="GridElementInline" style="border: solid 2px #1e606e;min-height:134px;height:134px;max-height: 134px" onclick="highlightAsset(this, event)"
                                                projectid='' folderid="2383" id="_tblBinlist2383" title = "today">
                    <tr>
                        <td style="padding: 10px 10px 0px 7px;">
                            <table id='tblBinlist2383' folderid='2383' projectId='' _onclick='highlightAsset(this, event)' ondblclick='showBinDetails("2383", "")' class='binThumbnail GridElementInline' cellpadding='0' cellspacing='0'><tr><td><img class='fourGridViewImage' src='http://stream.....' /></td><td><img class='fourGridViewImage' src='http://stream.....' /></td></tr><tr><td><img class='fourGridViewImage' src='http://stream.....' /></td><td><img class='fourGridViewImage' src='http://stream.....' /></td></tr></table>
                        </td>
                    </tr>
                <tr>
                <td colspan="2" align="center" style="padding: 10px 10px 0px 7px; font-size: 9px;white-space: nowrap;">
                <div align="left" title="today">
                    today
                </div>
                </td>
                </tr>
                </table>
            </div>

【问题讨论】:

  • 如果是公开的,请发布该页面的链接,以便我们尝试。如果不是公开的,请发布相关的 HTML。您可以询问开发人员是否应该触发另一个事件,或者是否应该在不同的元素上触发它。
  • 这不会在问题本身中得到更新,也不允许发布其余代码。无论如何,这是 HTML sn-p。这会有帮助吗?
  • 您可以编辑您的问题并发布 HTML 片段吗?

标签: ajax popup watir web-testing


【解决方案1】:

我为 Windows 中的 Watir 1.6.7 做了一个优雅的解决方案。

我去了ruby/lib/ruby/gems/1.8/gems/watir-1.6.7/lib/watir/element.rb (你的路径可能会改变,但主要是访问watir库并修改element.rb 文件)。

我添加了这些行:

def fire_event_no_wait(event)
  assert_exists
  assert_enabled
  highlight(:set)
  element = "#{self.class}.new(#{@page_container.attach_command}, :unique_number, #  {self.unique_number})"
  ruby_code = "require 'rubygems';" <<
          "require '#{File.expand_path(File.dirname(__FILE__))}/core';" <<
          "#{element}.fire_event(\"#{event}\")"
  system(spawned_click_no_wait_command(ruby_code))
  highlight(:clear)
end

这将创建一个名为 fire_event_no_wait 的方法,其行为类似于 watir 中的 click_no_wait。

希望这可以帮助任何人。

【讨论】:

    【解决方案2】:

    编辑您现在可以获得此代码here 的更好的工作版本。

    这里有一个快速且略显肮脏的解决方法,以防您真的被这个问题困扰。这确保了自动相关的硬件点击以保存点击事件所存在的问题(传统上,它在某些极端情况下存在)。

    将其粘贴在一个名为“mouseControl.rb”的文件中,然后使用 require 'mouseControl' 从您的主文件链接到它......或者您希望这样做。自从我使用它已经有一段时间了,但我相信你可以双击它。我没有包括所说的黑客,因为我碰巧知道这个文件有效(或至少有效)。您将需要'win32ole'。设置完成后,您应该可以简单地说出类似 @brower.button(:text, "boo").left_click 之类的内容来执行手动顶层硬件点击。

    应该让你渡过难关,直到你能得到更好的答案。

    require 'watir'
    
    module Watir
      class Element
        def top_edge
          assert_exists
          assert_enabled
          ole_object.getBoundingClientRect.top.to_i
        end
    
        def top_edge_absolute
          top_edge + page_container.document.parentWindow.screenTop.to_i
        end
    
        def left_edge
          assert_exists
          assert_enabled
          ole_object.getBoundingClientRect.left.to_i
        end
    
        def left_edge_absolute
          left_edge + page_container.document.parentWindow.screenLeft.to_i
        end
    
        def left_click
          x = left_edge_absolute + 1
          y = top_edge_absolute + 1
          #puts "x: #{x}, y: #{y}"
          WindowsInput.move_mouse(x, y)
          WindowsInput.left_click
        end
    
        def right_click
          x = left_edge_absolute
          y = top_edge_absolute
          #puts "x: #{x}, y: #{y}"
          WindowsInput.move_mouse(x, y)
          WindowsInput.right_click
        end
      end
    end 
    
    
    module WindowsInput
      # Windows API functions
      SetCursorPos = Win32API.new('user32','SetCursorPos', 'II', 'I')
      SendInput = Win32API.new('user32','SendInput', 'IPI', 'I') 
      # Windows API constants
      INPUT_MOUSE = 0 
      MOUSEEVENTF_LEFTDOWN = 0x0002 
      MOUSEEVENTF_LEFTUP = 0x0004 
      MOUSEEVENTF_RIGHTDOWN = 0x0008 
      MOUSEEVENTF_RIGHTUP = 0x0010 
    
      module_function
    
      def send_input(inputs) 
        n = inputs.size 
        ptr = inputs.collect {|i| i.to_s}.join # flatten arrays into single string 
        SendInput.call(n, ptr, inputs[0].size) 
      end
    
      def create_mouse_input(mouse_flag) 
        mi = Array.new(7, 0) 
        mi[0] = INPUT_MOUSE 
        mi[4] = mouse_flag 
        mi.pack('LLLLLLL') # Pack array into a binary sequence usable to SendInput 
      end 
    
      def move_mouse(x, y)
        SetCursorPos.call(x, y)
      end
    
      def left_click
        leftdown = create_mouse_input(MOUSEEVENTF_LEFTDOWN) 
        leftup = create_mouse_input(MOUSEEVENTF_LEFTUP) 
        send_input( [leftdown, leftup] )
      end
    
      def right_click
        rightdown = create_mouse_input(MOUSEEVENTF_RIGHTDOWN) 
        rightup = create_mouse_input(MOUSEEVENTF_RIGHTUP) 
        send_input( [rightdown, rightup] )
      end
    end
    

    【讨论】:

    • 谢谢你,kinofrost,但我们已经用这个试过了,没有效果。
    • 所以你可以将其识别为要点击的元素,但是硬件双击对它没有影响?我的意思是写一个新方法,def double_click,包含两行“left_click”,直接从标识的元素调用它没有效果?如果没有,那么我完全被卡住了。我可以建议尝试 AutoIT 解决方法,但老实说,如果硬件点击 IDd 元素不起作用,您可能需要找到另一种与之交互的方式。如果我能看到您编写的单击它的代码,它可能会有所帮助。大概你正在本地机器上的 IE 窗口上进行测试...
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签