在UITestControl而不是BrowserWindow范围内搜索时,CodedUI无法识别HtmlControl [英] CodedUI not recognizing HtmlControl when searched within scope of UITestControl instead of BrowserWindow

查看:74
本文介绍了在UITestControl而不是BrowserWindow范围内搜索时,CodedUI无法识别HtmlControl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用于 Page 的页面对象模型中,我们在构造函数中初始化 Container ,并定义 HtmlEdit 称为 MyTextBox 以及使用此文本框进行搜索的方法。

In our Page Object Model for Page we initialize a Container in the constructor and we define a HtmlEdit called MyTextBox and a method that uses this text box to search.

public class Page 
{
    private readonly UITestControl _container;
    private HtmlEdit _myTextBox;
    private const string MyTextBoxId = "MyTextBoxHtmlId";

    protected Page()
    { }

    protected Page(Process process)
    {
        CurrentBrowser = BrowserWindow.FromProcess(process);
        _container = CurrentBrowser;
    }

    public UITestControl Container
    {
       get { return _container; }
    }

    protected HtmlEdit MyTextBox
    {
        get
        {
            if (_myTextBox == null)
            {
                _myTextBox = Container.FindHtmlEditById(MyTextBoxId);
            }

            return _myTextBox;
        }
    }

    public Page SearchMethod(string accountId)
    {
         MyTextBox.CopyPastedText = accountId;

         // Do the search

         return this;
    }
}

这里我们要使用 UITestControl 作为容器,以便我们可以在页面的特定区域内进行搜索。 getter中的 FindHtmlEditById 扩展方法按其html ID查找元素,如下所示:

Here we want to use a UITestControl as container so that we can search within a specific area of the page. The FindHtmlEditById extension method in the getter finds the element by its html id as follows:

public static class Extensions 
{
    public static HtmlEdit FindHtmlEditById(this UITestControl control, string id)
    {
        return new HtmlEdit(control).FindById(id);
    }

    public static TUIControl FindById<TUIControl>(this TUIControl control, string id) 
         where TUIControl : HtmlControl 
    {
        control.SearchProperties.Add(HtmlControl.PropertyNames.Id, id, 
            PropertyExpressionOperator.Contains);

        return control;
    }
}

所以 FindHtmlEditById 容器范围内搜索具有特定ID的元素。

So FindHtmlEditById searches for an element with a certain id within the scope of the Container.

执行时代码和执行将文本粘贴到 MyTextBox 中,我们将收到以下错误:

When we execute the code and executions arrives at pasting text into MyTextBox we get the following error:


MyTextBox.CopyPastedText ='MyTextBox.CopyPastedText'引发了类型'System.NotSupportedException'的异常

MyTextBox.CopyPastedText = 'MyTextBox.CopyPastedText' threw an exception of type 'System.NotSupportedException'

此外,<$ c $ MyTextBox 的c> ControlType 是 Window ,如下所示:

Furthermore, the ControlType of MyTextBox is Window as can be seen here:

容器的类型页面中的c $ c>字段更改为 BrowserWindow ,如下所示:

private readonly BrowserWindow _container;

public BrowserWindow Container
{
    get { return _container; }
}

MyTextBox 是正确识别为 HtmlEdit

BrowserWindow 继承自 UITestControl 。为什么然后需要将 Container 指定为 BrowserWindow 才能正常工作?为什么它不能用作 UITestControl

BrowserWindow inherits from UITestControl. Why then does the Container needs to be specified as a BrowserWindow to work? Why does it not work as a UITestControl?

推荐答案

可以提供吗?带有repro的拉链,这样我可以近距离看吗?从屏幕截图中可以明显看出,在一种情况下,您会获得一个Window控件,而不是要搜索的HtmlEdit,但这是我无法给出结论的原因。 CurrentBrowser的代码中可能存在错误,因为这似乎是不正确的构造,但也可能仅是粘贴代码中的缺陷。我复制了代码,并尝试使用bing主页作为复制文字的页面来进行复制,这就像一种魅力。因此,我怀疑搜索结果与您目标网站中的内容有所不同。可能导致此问题的一件事是您在搜索中使用的contains运算符,因为这可能会导致多个匹配项,具体取决于您要进行搜索的页面。当它返回多个控件时,您的调用也将失败,但是看着您遇到的麻烦,您没有得到多个控件,而是一个带有Windows类编辑器的窗口,它看上去更像是WinEdit控件,而不是HtmlEdit控件。

Can you provide a zip with a repro so I can have a closer look? It is obvious from the screenshots that in the one case you get a Window control back in stead of the HtmlEdit you are searching for, but as the reason why I can not give a conclusive answer. There might be a bug in your code with the CurrentBrowser since that seems to be a construct that is not correct, but also can be a flaw in the pasted code only. I copied the code and tried to reproduce using the bing homepage as the page to past the text in, and that works like a charm. So I suspect the search results in something different in your target website. One thing that might be causing this is the contains operator you use on the search, since this could potentially result in multiple matches depending on the page you are conducting the search on. When it returns multiple controls your call wil also fail, but looking at the whatch you shoed you did not get multiple controls but a window with a windows class edit, which much more looks like a WinEdit control then a HtmlEdit control.

这篇关于在UITestControl而不是BrowserWindow范围内搜索时,CodedUI无法识别HtmlControl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆