我是WATIR的新手。我面临的问题是 - 我正在测试的应用程序在页面上放置了缩略图(如Windows图标),我需要双击它。在这样做时,将打开一个自定义弹出窗口(在javascript中实现的ajax弹出窗口)。 fire_event(“ondblclick”)对我不起作用。我也试过两次“点击”,但这也没有帮助。有没有其他办法处理这个?非常感谢您的帮助。
2010年7月6日增加:
我解决了,但我现在有另一个问题。
以下是我能够使用“@ ie.div(:class,'GridElementInlineIE')解决的HTML。”fire_event('ondblclick')“
<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>
答案 0 :(得分: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类似。
希望这可以帮助任何人。
答案 1 :(得分:0)
编辑您现在可以获得此代码here的更好的工作版本。
这是一个快速而略显肮脏的解决方法,以防您真正陷入困境。这确保了自动相对硬件点击以保存click事件所具有的问题(并且传统上,它在某些极端情况下具有它们)。
将其粘贴在名为“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