gpt4 book ai didi

c# - 如果未找到 UI 元素,如何使测试失败?

转载 作者:行者123 更新时间:2023-11-30 17:08:44 25 4
gpt4 key购买 nike

我使用一种搜索 UI 元素的方法:

public static bool findtopuielm(string uiitemname)
{
bool res = false;
try
{
AutomationElement desktopelem = AutomationElement.RootElement;
if (desktopelem != null)
{
Condition condition = new PropertyCondition(AutomationElement.NameProperty, uiitemname);
AutomationElement appElement = desktopelem.FindFirst(TreeScope.Descendants, condition);
if (appElement != null)
{
res = true;
}

}
return res;
}
catch (Win32Exception)
{
// To do: error handling
return false;
}
}

此方法由另一个等待元素出现在桌面上的方法调用。

public static void waittopuielm(string appname, int retries = 1000, int retrytimeout = 1000)
{
for (int i = 1; i <= retries; i++)
{
if (findtopuielm(appname))

break;
Thread.Sleep(retrytimeout);

}
}

例如,当我调用最后一个函数时:

waittopuielm("测试");

即使未找到该元素,它也始终返回 true,在这种情况下我希望测试失败。欢迎提出任何建议。

最佳答案

看起来您的 waittopuielem 方法返回了 void - 您是要发布类似此版本的内容,它会返回 bool 值吗?

    public static bool waittopuielm(string appname, int retries = 1000, int retrytimeout = 1000)
{
bool foundMatch = false;
for (int i = 1; i <= retries; i++)
{
if (findtopuielm(appname))
{
foundMatch = true;
break;
}
else
{
Console.WriteLine("No match found, sleeping...");
}
Thread.Sleep(retrytimeout);
}
return foundMatch;
}

除此之外,您的代码对我来说似乎可以正常工作。

一个建议:在您的 findtopuielm 方法中,将桌面元素搜索中的 TreeScope 值从 TreeScope.Descendants 更改为 TreeScope.Children:

AutomationElement appElement = desktopelem.FindFirst(TreeScope.Children, condition);

TreeScope.Descendants 进行的递归搜索可能比您想要的多 - 将搜索桌面元素的所有子元素,以及这些元素的每个子元素(即按钮、编辑控件等)。

因此,在搜索相对常见的字符串时,找到错误元素的可能性很高,除非您将 NameProperty PropertyCondition 与 AndCondition 中的其他属性组合以缩小搜索范围。

关于c# - 如果未找到 UI 元素,如何使测试失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13565101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com