Similar HTML Tag with Coded UI

List all similar Html Tag
To list all “P” tag of a website we need to use the following code –

[TestInitialize()]
        public void MyTestInitialize()
        {
            BrowserWindow.Launch("http://demo.tanmaysarkar.com/sample_01.html");
        }


        [TestMethod]
        public void CodedUITestMethod1()
        {
            BrowserWindow Browser = new BrowserWindow();
            Browser.SearchProperties[UITestControl.PropertyNames.Name] = "Automation Sample 1:: Tanmay Sarkar";

         HtmlControl AllPtag = new HtmlControl(Browser);
            AllPtag.SearchProperties[HtmlControl.PropertyNames.TagName] = "p";
            UITestControlCollection collection = AllPtag.FindMatchingControls();
            foreach (HtmlControl each in collection)
            {
                Console.WriteLine(each.GetProperty("InnerText"));
            }
            

        }

And the output should contain all the p tag of http://demo.tanmaysarkar.com/sample_01.html site –

To get a particular p tag inner text from this collection we can modify our code like –

HtmlControl Sample = new HtmlControl(Browser);
            Sample.SearchProperties[HtmlControl.PropertyNames.TagName] = "p";
            UITestControlCollection col = AllPtag.FindMatchingControls();
            Console.WriteLine(col[3].GetProperty("InnerText"));

And the out put will be –

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *