在编码UI中执行循环中的方法时,对象识别失败

本文关键字:对象 方法 识别 失败 编码 UI 执行 循环 | 更新日期: 2023-09-27 18:11:20

我创建了一个通用方法,可用于在我们的AUT中搜索记录。

现在我有一个场景,我需要多次运行我的代码。所以我创建了一个循环并尝试执行。第一次迭代没有任何问题,页面被关闭。重新打开页面并加载所有控件,但在第二次迭代中对象识别失败。

初始化htmlledit对象,如下所示:

HtmlEdit medit = new HtmlEdit(objSearchPage);     
medit.SearchProperties.Add("Name", "1$SearchText", PropertyExpressionOperator.Contains);     
medit.SearchProperties[HtmlEdit.PropertyNames.TagName] = "INPUT";     
medit.SearchProperties[HtmlEdit.PropertyNames.ControlType] = "Edit";

在第二次迭代中尝试在'medit'框上输入文本时播放失败。第一次迭代成功:medit.Text = searchItem;

在编码UI中执行循环中的方法时,对象识别失败

如果您的浏览器窗口在迭代期间打开和关闭,则需要在循环中包含初始化代码或使用AlwaysSearch设置。

foreach(var thing in thingsToDo)
{
    HtmlEdit medit = new HtmlEdit(objSearchPage);     
    medit.SearchProperties.Add("Name", "1$SearchText", PropertyExpressionOperator.Contains);     
    medit.SearchProperties[HtmlEdit.PropertyNames.TagName] = "INPUT";     
    medit.SearchProperties[HtmlEdit.PropertyNames.ControlType] = "Edit";
    // use medit now and it will work
}

HtmlEdit medit = new HtmlEdit(objSearchPage);     
medit.SearchProperties.Add("Name", "1$SearchText", PropertyExpressionOperator.Contains);     
medit.SearchProperties[HtmlEdit.PropertyNames.TagName] = "INPUT";     
medit.SearchProperties[HtmlEdit.PropertyNames.ControlType] = "Edit";
// I'm not sure this will work because the browser window is different
medit.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);

@MPavlak离钱很近。我认为我们可能忘记的是你还必须重新初始化你的父控件(objSearchPage)。在搜索medit之前,请确保它存在并找到。

foreach(var thing in thingsToDo)
{
    var objSearchPage = new UITestControl(); //you can also just reinitialize here if it's been previously declared.
    objSearchPage.SearchProperties.Add("yourPropertyHere");
    HtmlEdit medit = new HtmlEdit(objSearchPage);     
    medit.SearchProperties.Add("Name", "1$SearchText", PropertyExpressionOperator.Contains);     
    medit.SearchProperties[HtmlEdit.PropertyNames.TagName] = "INPUT";     
    medit.SearchProperties[HtmlEdit.PropertyNames.ControlType] = "Edit";
}