0

我试图Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.HtmlDiv作为一个参数传递,所以我需要初始化并在一行中找到我正在寻找的特定控件(如果可能的话),但我正在努力解决这个问题。正常初始化是这样完成的......

HtmlDiv htmlDiv = new HtmlDiv(htmlDocument) // where htmlDocument is of Microsoft.VisualStudio.TestTools.UITesting.UITestControl
htmlDiv.SearchProperties[HtmlDiv.PropertyNames.Id] = "header";

我正在尝试做这样的事情......

myFunction(new HtmlDiv(htmlDocument) {
    SearchProperties[HtmlDiv.PropertyNames.Id] = "header"
});

我收到错误cannot resolve symbol 'SearchProperties'Cannot initialize type 'Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.HtmlDiv' with a collection initializer because it does not implement 'System.Collections.IEnumerable'.

有没有办法使这项工作?

4

1 回答 1

0

我想不出办法来做到这一点,所以我在我的 HtmlDocument 包装类中创建了一个方法,该方法将返回找到的 UITestControl。

public class UiHtmlDocument : HtmlDocument {
    public UiHtmlDocument(UITestControl searchLimitContainer) : base(searchLimitContainer) {}

    public T searchHtmlElementByAttributeValues<T>(Dictionary<string,string> propertyExpressions) where T : UITestControl, new() {
        var uiTestControl = Activator.CreateInstance(typeof(T), this);
        foreach (var propertyExpression in propertyExpressions) {
            ((T)uiTestControl).SearchProperties[propertyExpression.Key] = propertyExpression.Value;
        }
        return (T)uiTestControl;
    }
}

并像这样使用...

myFunction(
    browserWindow.htmlDocument.searchHtmlElementByAttributeValues<HtmlDiv>(
        new Dictionary<string, string> { 
            { HtmlDiv.PropertyNames.Class, "footer" } 
        }
    )
);
于 2012-05-16T13:11:32.367 回答