Hello,
I've modified the ClickMenuItem function to illustrate how to click items of a WPF unbound ContextMenu using their names.
function ClickMenuItem(menu, path, parentWindow, proc)
{
var i, items, item, found, x, y, point;
items = path.split("|");
// Find the menu item to click
found = false;
for (i = 0; i < menu.Items.Count; i++)
{
item = menu.Items.Item(i);
if (item.Header.OleValue == items[0])
{
found = true;
break;
}
}
if (!found)
{
Log.Error("The '" + items[0] + "' menu item was not found");
return false;
}
// Calculate the menu item's screen coordinates and click the found point
var appDom = proc.AppDomain(proc.ProcessName + ".exe");
var pnt = appDom.dotNET.System_Windows.Rect.Empty.Location;
pnt.X = item.ActualWidth/2;
pnt.Y = item.ActualHeight/2;
var locationFromWindow = item.TranslatePoint(pnt, parentWindow);
var locationFromScreen = item.PointToScreen(pnt);
x = locationFromScreen.X;
y = locationFromScreen.Y;
Sys.Desktop.MouseDown(VK_LBUTTON, x, y);
Sys.Desktop.MouseUp(VK_LBUTTON, x, y);
Delay(250);
// Proceed to the next item in the path
if (items.length > 1)
if (!ClickMenuItem(item.DropDown, items.slice(1).join("|")))
return false;
return true;
}
Usage:
var proc;
var tabControl;
var contextMenu;
var mainWnd;
// The process of the tested application
proc = Sys.Process("WpfApplication1");
// The window that owns the context menu
mainWnd = proc.WPFObject("HwndSource: MainWindow", "MainWindow").WPFObject("MainWindow", "MainWindow", 1);
...
ClickMenuItem(contextMenu, "Paste", mainWnd, proc);