I have posted a snippet of code to assist Watir users that might need to
right-click on an element on the screen. I would appreciate any
feedback you could give me about the method that I am using.
Ideally there should be a way to do this without resorting to AutoIt but
I couldn't find a way. If anyone knows how to use some windows API
method to click a mouse coordinate on a screen and/or how to figure out
the offset between an IE window an it's client area (as reported by
ole_object.getBoundingClientRect) I would really appreciate it.
http://wiki.openqa.org/display/WTR/Right+Click+an+Element
Thanks for your consideration,
Alan
ie.button(:id , 'watever').fire_event('onContextMenu')
and then use autoit to send the key presses to select the menu item
Paul
Here is what I've tried:
$ie = Watir::IE.attach(:url, /SessionLog/)
$autoit = WIN32OLE.new('AutoItX3.Control')
$autoit.AutoItSetOption("WinTitleMatchMode", 2)
$autoit.AutoItSetOption("MouseCoordMode", 2)
window_title = "SessionLog?"
$autoit.WinActivate(window_title)
myobject = $ie.link(:text => 'Request', :index => 1)
myobject.focus
myobject.fire_event('onContextmenu')
$autoit.Send "{DOWN}{DOWN}{DOWN}{ENTER}"
Thanks for your help,
Alan
Paul
That would be nice. Reading the explanation for this makes my head
hurt, any help in deciphering would be nice.
Alan
Mouse_event = Win32API.new('user32','mouse_event','LLLLL','V')
MOUSEEVENTF_RIGHTDOWN = 0x0008
MOUSEEVENTF_RIGHTUP = 0x0010
Mouse_event.call(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
Mouse_event.call(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
----
And here's a similar script that uses SendInput instead. For the parts that get a little cryptic, I tried adding some comments:
----
require 'Win32API'
SendInput = Win32API.new("user32", "SendInput", 'IPI', 'I')
INPUT_MOUSE = 0
MOUSEEVENTF_LEFTDOWN = 0x0002
MOUSEEVENTF_LEFTUP = 0x0004
MOUSEEVENTF_RIGHTDOWN = 0x0008
MOUSEEVENTF_RIGHTUP = 0x0010
# Takes an array of packed input structures and passes them on to SendInput.
# Taken from the Ruby dl2 SendInput example in Takaaki Tateishi's dlcookbook:
# http://www.koders.com/ruby/fid5ECCEACCE986D7D1452DB90275B010C51F8EDD33.aspx
def send_input(inputs)
n = inputs.size
ptr = inputs.collect {|i| i.to_s}.join # flatten arrays into a single string
SendInput.call(n, ptr, inputs[0].size)
end
# Takes a mouse input bit flag, and returns a packed string containing the flag.
# SendInput will accept the string as an INPUT structure.
#
# The list of flags is here:
# http://msdn.microsoft.com/en-us/library/ms646273(VS.85).aspx
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
rightdown = create_mouse_input(MOUSEEVENTF_RIGHTDOWN)
rightup = create_mouse_input(MOUSEEVENTF_RIGHTUP)
p send_input([rightdown, rightup])
----
Bill –
I downloaded your example and it runs fine on my computer. However, when I use it in conjunction with my program I get the following error:
>ruby right-click_BillAgee.rb
right-click_BillAgee.rb:22:in `method_missing': unknown property or method `parentWindow' (WIN32OLERuntimeError)
HRESULT error code:0x80020006
Unknown name. from right-click_BillAgee.rb:22:in `left_edge_absolute'
from right-click_BillAgee.rb:26:in `right_click'
from right-click_BillAgee.rb:75:in `main'
from right-click_BillAgee.rb:82
>Exit code: 1
The only thing I did was access my page from your program, I have posted the relevant modification below. The main difference is that my link is in a table, see the code below:
# Open google index page, and send a right click to the logo image
ie = Watir::IE.attach(:url, /SessionLog?/)
ie.table(:index, 1).link(:text => 'Request', :index => 1).right_click
# Then, bring up the properties menu (works with IE6, at least)
ie.send_keys("{DOWN}{DOWN}{DOWN}{ENTER}")
Maybe container.document.parentWindow doesn’t work inside a table?
Alan
From: watir-...@googlegroups.com [mailto:watir-...@googlegroups.com] On Behalf Of Bill Agee
Sent: Friday, June 20, 2008 11:52 AM
To: watir-...@googlegroups.com
Subject: [wtr-general] Re: Right Click an Element
<br
Bill –
This worked for me. I will update the wiki accordingly.
Thanks again!
Alan