Monday, January 5, 2015

Extract all web links Text from Website using Coded UI (C#)





Hi guys in this post we are to see "How to Print all the Links Text from Webpage" using Coded UI

In this example :


  1. Opening browser and navigating to http://automationplace.blogspot.in
     BrowserWindow bw = BrowserWindow.Launch("www.automationplace.blogspot.com");

   2.  Assigning the Webcontrol to UITestControl

     UITestControl links = new UITestControl(bw);
   links.TechnologyName = "web";
   links.SearchProperties.Add("Controltype", "Hyperlink");

   3. Loop all the links.

     foreach(UITestControl alllinks in links.FindMatchingControls())
   {
     Console.WriteLine("All links " + alllinks.GetProperty("Innertext"));
   }



Source Code :


using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;


namespace CodedUIDemo2
{
    /// <summary>
    /// Summary description for CodedUITest1
    /// </summary>
    [CodedUITest]
    public class CodedUITest1
    {
        public CodedUITest1()
        {
        }

        [TestMethod]
        public void Printlinks()
        {
            BrowserWindow bw = BrowserWindow.Launch("www.automationplace.blogspot.com");
            UITestControl links = new UITestControl(bw);
            links.TechnologyName = "web";
            links.SearchProperties.Add("Controltype", "Hyperlink");

            foreach(UITestControl alllinks in links.FindMatchingControls())
            {
                Console.WriteLine("All links " + alllinks.GetProperty("Innertext"));
            }
        }

    }
}


Run the above code.

After that, Click on Output from TextExplorer


You will see  all the hyperlink present in webpage.











Hope you Like this post.











2 comments:

  1. hi prashant

    1>Is thier any logic to wait for perticuler element untill it appears?
    2>give me some verification scripts like assertTrue,assertEqules like that

    ReplyDelete