1

Currently, I am using the traditional way i.e. 'Inspect Element' (or Firebug or IE Developer) for getting value of ID and Name attributes of any field present over page for which I am creating test cases. But, it is sometimes very time consuming especially when there are 100s of fields over a page/form. Sometimes, I use the record method abd then getting these attributes from the recorded test cases (but this too is a kind of work around).

Is there any quick way of getting ID and Name attributes of all fields (like TextBox, Dropdowns, CheckBox etc.) present on a web application page in one go.

  • Maybe write a code to capture elements by tag name into a list. Then traverse inside a For loop to print the attribute with the value. – ilm Feb 5 '16 at 9:14
  • Not sure if this is exactly what I am looking for but will definitely try it. – Dhiman Feb 5 '16 at 12:32
3

Since you are using CodedUI for this I would write something like this.

public static void GetAllElements(BrowserWindow bw)
{
var a = bw.GetChildren().Where(x => x.GetProperty("TagName").ToString() == "TD");
            foreach (var control in a)
            {
                Console.WriteLine(control.Name);
                Console.WriteLine(control.GetProperty("Id"));
            }
}

Also if you are interested in saving time while hand coding make as many generic methods if you can

public static void ClickHtmlButton(string buttonidentifierType, string buttonIdentifier)
{
    var button = new HtmlButton(bw);
    if buttonidentifierType == "ID"
    {
        button.SearchProperties.Add("Id", buttonIdentifier);
    }
    if buttonidentifierType == "InnerText"
    {
        button.SearchProperties.Add("InnerText", buttonIdentifier);
    }
    Mouse.Click(button);
}
| improve this answer | |
  • I tried the same but it seems not working, using the same code throws this error 'Microsoft.VisualStudio.TestTools.UITesting.UITestControlCollection' does not contain a definition for 'Where'. But you gave me the right direction so I modified your code and it worked correctly :) – Dhiman Feb 9 '16 at 14:50
  • Add the linq reference – AlexCharizamhard Feb 9 '16 at 15:41
  • I tried with that too earlier, but I made a silly mistake of adding 'using System.Xml.Linq; instead of 'using System.Linq'. Now your solution too worked fine. – Dhiman Feb 9 '16 at 16:33
0

It is not in one go, but one list per element type, using driver.FindElement(By.TagName("tag")) http://www.seleniumhq.org/docs/03_webdriver.jsp (as @ilm correctly suggested)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.