编码UI:如何重试右键单击上下文菜单以检查启用的选项

本文关键字:菜单 上下文 单击 检查 选项 启用 右键 UI 何重试 重试 编码 | 更新日期: 2023-09-27 17:54:27

我试图在我的一个编码UI测试中自动化一个过程,我右键单击表格中的一个单元格,如果"粘贴"按钮启用,我想选择它。

然而,这个按钮只有在有东西要"粘贴"时才启用,但情况并非总是如此。所以我想右键点击并检查'粘贴'是否启用。如果没有,等10秒再试一次,可能会重复10次,然后失败。到目前为止,我想出的最好的一个过程,我右键单击单元格,如果粘贴按钮未启用,等待10秒,再试一次。

Mouse.Click(Cell1, MouseButtons.Right, ModifierKeys.None, new Point(125, 10));
while (!PasteButton.Enabled)
    {
    System.Threading.Thread.Sleep(10000);
    Mouse.Click(Cell1, MouseButtons.Right, ModifierKeys.None, new Point(125, 10));
    if (PasteButton.Enabled)
    {
        Mouse.Click(Cell1, new Point(54, 12));
    }
}

我知道类似

的东西
PasteButton.WaitForControlPropertyEqual(PasteButton.Value, "Paste", 60000);

但我不能右键点击,等待按钮成为启用,因为它不自动刷新。所以我需要继续检查右键点击上下文菜单

编码UI:如何重试右键单击上下文菜单以检查启用的选项

如果你没有采取任何行动,而不是右键单击,如何进入剪贴板供你粘贴?如果你有一种测试方法,你可以改进你的检查,但除此之外,你所做的似乎是正确的。我想稍微修改一下代码。

var rightClickCell = () => Mouse.Click(Cell1, MouseButtons.Right, ModifierKeys.None, new Point(125, 10));
rightClickCell();
while (!PasteButton.Enabled);
{
    System.Threading.Thread.Sleep(10000);
    rightClickCell();
}
if (PasteButton.Enabled)
{
    // PROBABLY DO NOT WHAT TO DO THIS
    // instead, find the context menu and click the item
    // whose name/text is "Paste"
    Mouse.Click(Cell1, new Point(54, 12));
}

更好,也许更像这样:

var rightClickCell = () => Mouse.Click(Cell1, MouseButtons.Right, ModifierKeys.None, new Point(125, 10));
// pseudo code
var contextMenu = new WinContextMenu(Cell1);
var pasteMenuItem = new WinMenuItem(contextMenu);
pasteMenuItem.SearchProperties.Add("Name", "Paste");
rightClickCell();
while(!pasteMenuItem.TryFind() || !pasteMenuItem.Enabled)
{
   rightClickCell();
   WaitForControlEnabled(pasteMenuItem, 10 * 1000);
}
if(pasteMenuItem.Enabled)
{
   Mouse.Click(pasteMenuItem);
}

谢谢@MPavlak,您的解决方案为我指明了正确的方向。我不得不对您的伪代码进行一些调整,以使其适用于我的应用程序。

Visual Studio坚持我声明var rightClickCell =()为rightClickCell = new Action(() =>等等…

WinContextMenu实际上是一个WinList, WinMenuItem在我们的应用程序中是一个Winbutton。

我使用了SearchProperties[WinButton.PropertyNames]。名称]而不是SearchProperties。添加,因为它与我在应用程序中发现其他对象的方式一致。

我必须在while循环中的主单元格上添加Click,因为右键单击单元格中的同一位置实际上并没有刷新菜单。在rightClickCell中,我可以将new Point(125,10)更改为new Point(x,y),并可能在每次通过while循环时减少x值,从而将右点移动到不同的位置。但这是更多的代码行,其中左键单击单元格,然后右键单击产生相同的结果。

没有找到WaitForControlEnabled,所以导致了一个异常,所以我使用了Thread.Sleep。

var rightClickCell = new Action(() => Mouse.Click(cell1, MouseButtons.Right, ModifierKeys.None, new Point(125, 10)));
var contextMenu = new WinList(uIContextMenu);
var pasteMenuItem = new WinButton(contextMenu);
pasteMenuItem.SearchProperties[WinButton.PropertyNames.Name] = "Paste";
rightClickCell();
while (!pasteMenuItem.TryFind() || !pasteMenuItem.Enabled)
{
    Mouse.Click(cell1, new Point(10, 10));
    rightClickCell();
    Thread.Sleep(5000);
}
if (pasteMenuItem.Enabled)
{
    pasteMenuItem.DrawHighlight();
    Mouse.Click(pasteMenuItem, new Point(10, 10));
}

如果我尝试用x,y值定义rightClickCell,我会得到错误:删除"Action不接受0个参数"。

var rightClickCell = new Action(() => Mouse.Click(cell1, MouseButtons.Right, ModifierKeys.None, new Point(int x, int y)));