How to find all links in web page using Coded UI Test?

regerus picture regerus · May 12, 2011 · Viewed 9.1k times · Source

Can I find all links in web page using Coded UI Test Builder, or I have to make HTTP request and parse HTML?

Answer

edance picture edance · Jul 14, 2011

You could do something like this...

        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);
            }
        }