2

I am new to automation and still in learning phase. We are building an automation framework using Selenium with Java for a web app that is being newly developed. We do not have IDs for most elements in this web app right now. I am trying to identify web elements for which having unique ID is important so that I can request the developers to add IDs for these web elements.

I was thinking that it's okay not to have IDs for non-functional web elements like page headings, static content on page, etc. Among functional web elements, I think we need unique IDs for these elements without a doubt: 1. Text Fields, 2. Buttons, 3. Check-boxes, 4. Drop-downs, 5. Hyperlinks, 6. Lists.

However, I am not sure about few web elements like: 1. Hover over elements (e.g. drop down menu appears, tool tip appears, details screen appears), 2. Dynamic Tables (I guess it would be good to have unique IDs for column headings, other cells need not have IDs).

I am trying to find a way that will let us build test scripts with more accuracy and low maintenance, at the same time I do not want to over burden our developers with the task of creating unique IDs for each and every element. Please let me know whether unique IDs should be created for web elements like hover over elements, dynamic tables and any other web element that I have missed mentioning here. Thanks!

  • Deepti, Can you accept any answer which helps you the most. So this question will no longer remain in Active queue. #BestPractice – NarendraC Aug 16 '16 at 7:03
3

Completely agree with Yu. I would like to add few more points:

  • When no ID available then you can go for Xpath and Css selectors. More strong solution I will suggest to use:

    1. Use Relative xpath:

Doesn't need to start from the root node. It starts with Double forward slash(//)

  1. Use combination of locators:

You can use combination of Name, linkText, className, partialLinkText, css etc to find elements more accurate

  1. Avoid Absolute XPath:

Disadvantage is, if any thing goes wrong or some other tag added in between, then this path will no longer works

2

xpath and CSS identifiers are sometimes harder to maintain, but also add some test functionality. A broken CSS identifier may happen because an element moved when it shouldn't have, after all. If a button moves out of a containing group, the page might still function, but the CSS applied to the button could be different. If your application isn't undergoing current or frequent redesign, these identifiers should be fairly stable.

When I had to have new identifiers (ID or class) on a new element, I used the following rules:

  • Identifiers for testing started with qa-
  • Never ever ever use the qa- identifiers outside the test scripts.
  • If a developer needed to address elements with a qa- identifier, they should replace that tag with something meaningful to the functionality/style they were adding, and let me know of the change. That way I could add/remove/change qa- identifiers without worrying about breaking anything. If they forgot to tell me of the change, tests would break, but it would be a pretty quick fix to identify the problem and adjust the tests.

We weren't doing continuous integration; if you are, you might need to adjust the rules around changing the identifiers to not break the build process.

1

Unique IDs is the most efficient way to uniquely identify a web element.

But it will take some effort to come up with a naming convention about how to name an element and developers need to stick to this convention.

It is definitely worth a while to assign all interactive web elements with unique IDs, such as buttons, check boxes and input boxes.

In case for an element that has no ID, you can easily locate it via Xpath and Css selector.

  • Thanks for your reply! What I got to know till now is that Xpath and Css selector might change with changes in UI and that using unique ID is the best approach as it accurate and does not change with UI changes. I have requested dev team to add IDs for web elements and they have agreed to add IDs for interactive elements like text fields, buttons, etc. But as this is a tedious task for them, they wanted to know if I want IDs for dynamic web tables and hover over elements too. Can you suggest if it is important to have IDs for these elements as well? – Deepti Deshpande Aug 11 '16 at 6:33
  • @DeeptiDeshpande, that depends on how often you are planning to interact with them. If those elements are part of you test plan, do assign unique IDs to them. P.S. What you said in your second comment is spot on. – Yu Zhang Aug 11 '16 at 6:36
  • 1
    I will suggest to ask ID for hover elements. It helps lot while automating other things too. Even when you might not need to interact with those as in test case you are automating but pre-requisite scenarios this can be helpful. Not that needed for dynamic web table, I feel – NarendraC Aug 11 '16 at 6:43
  • Thanks @NarendraC for your inputs. I have one question on this - what if hover element is available in a dynamic web table? We have a table in our web app that has hover elements. For example, if I hover over a username available in this table, a screen appears that displays user details. Adding IDs for each cell of a dynamic table might not be feasible for developers, right? – Deepti Deshpande Aug 11 '16 at 6:51
  • yes - exactly. You are getting things quickly. Doesn't seems new in automation ;) Btw, So I'll suggest ask ID for only hover over elements. That would also be beneficial to automate scenarios related to table too. Clear @DeeptiDeshpande? – NarendraC Aug 11 '16 at 7:06

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.