【问题标题】:Object Identification failed in Coded UI, while executing a method in a loop编码 UI 中的对象识别失败,同时在循环中执行方法
【发布时间】:2016-10-04 09:10:03
【问题描述】:

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

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

初始化 HtmlEdit 对象如下:

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.Text = searchItem;

【问题讨论】:

  • 没有您的代码是否会出现问题?我问是因为有关代码的问题需要您发布代码。如果问题是关于编码 UI 并且您的代码无关紧要,那么请明确说明。
  • 初始化HtmlEdit对象如下,HtmlEdit medit = new HtmlEdit(objSearchPage); medit.SearchProperties.Add("名称", "1$SearchText", PropertyExpressionOperator.Contains); medit.SearchProperties[HtmlEdit.PropertyNames.TagName] = "INPUT"; medit.SearchProperties[HtmlEdit.PropertyNames.ControlType] = "编辑";在第二次迭代期间尝试在“medit”框中输入文本时播放失败。第一次迭代就成功了——medit.Text = searchItem;

标签: c# coded-ui-tests


【解决方案1】:

如果您的浏览器窗口在迭代期间打开和关闭,您需要在循环中包含初始化代码或使用 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);

【讨论】:

    【解决方案2】:

    @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";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-28
      相关资源
      最近更新 更多