如何使用Coded UI Test查找网页中的所有链接?

时间:2011-05-11 23:20:35

标签: c# visual-studio-2010 coded-ui-tests

我可以使用Coded UI Test Builder查找网页中的所有链接,还是必须发出HTTP请求并解析HTML?

1 个答案:

答案 0 :(得分:8)

你可以这样做......

        BrowserWindow bw = BrowserWindow.Launch(new Uri("http://www.google.com"));
        bw.WaitForControlReady();
        UITestControl document = bw.CurrentDocumentWindow;
        HtmlControl control = new HtmlControl(document);
        control.SearchProperties.Add(HtmlControl.PropertyNames.ClassName, "HtmlHyperlink");
        UITestControlCollection controlcollection = control.FindMatchingControls();
        List<string> names = new List<string>();
        foreach (UITestControl x in controlcollection)
        {
            if (x is HtmlHyperlink)
            {
                HtmlHyperlink s = (HtmlHyperlink)x;
                names.Add(s.Href);
            }
        }