如何初始化HtmlDiv

时间:2012-05-15 15:35:44

标签: c# initialization coded-ui-tests

我正在尝试传递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'

有没有办法让这项工作?

1 个答案:

答案 0 :(得分: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" } 
        }
    )
);