Forum Discussion

christian0501's avatar
christian0501
New Contributor
2 months ago

Accessing parent objects attributes

Dear community,

I have a struggle in coding a web test that I coulnd't solve for a couple of days now:

I have a combobox where the user can select items in the dropdown menu. All items have a data-test id string, so that they can be accessed.

Now I want to find out if a certain item is selected or not. The item with the data-testid doesn't have the selected attribute itself (it is of type "span") but the parent item (of type "li") has a aria-selected attribute.
So I want to access the attributes of the parent element to read the aria-selected attribute.
(the html structure I'm talking about can be seen in the attached screenshot)

The problem is that, when accessing the parent object via .Parent, then there are no attributes to this Parent object.
So how can I do this simple task of reading an attribute of the parent object?
This is really annoying so I'm hoping very much for help.

Thank you :)

4 Replies

  • In case it's needed, this is the code:

    var item = this.page.FindElement("//span[@data-testid='" + dataTestIdString + "']");

    if(item.Parent.attributes.aria_selected == true)
    { ... do stuff

    For all other attribute accesses, this code works but the "attributes" property doens't exist on the Parent

    • rraghvani's avatar
      rraghvani
      Champion Level 3

      Could you provide a screenshot of the combobox, the list of items and the selected item?

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      Hi,

      > For all other attribute accesses, this code works but the "attributes" property doens't exist on the Parent

      Are you saying that for 'item' element statement, for example, item.Parent.FullName returns some value and only item.Parent.attributes evaluates to null?

      If the above is true, then this might be a problem in TestComplete.

      If item.Parent contains only native html properties or evaluates to null, then it might be a case that the span you are looking for was excluded from objects hierarchy in TestComplete (https://support.smartbear.com/testcomplete/docs/app-testing/web/general/object-identification/tree-model-elements.html) and thus FindElement() returned native DOM element but not its TestComplete wrapper (as this happens usually).

      As a kind of workaround I may suggest:

      a) either try to use native DOM methods to get item's parent; or

      b) try to search for the li element that has required child span element and work with found li element. Consider https://copyprogramming.com/howto/how-select-a-parent-node-with-xpath as an example of possible search.

       

      Hope this will help.

       

  • Maybe try to get the "li" element directly with something like this:
    //li[@aria-selected and child::span[@data-testid='" + dataTestIdString + "']]