Forum Discussion

pkudrys's avatar
pkudrys
Contributor
9 days ago

how to get Object.Name without triggering element search

Hi guys, 

I know that this topic may sound a bit weird, but I have a very good reasons for it :) Here is my situation...

I have a mapped element, containing an xpath, which I need to pass to a function as a parameter. The xpath alone, may not find an element! This is a critical point of my problem! The xpath is supposed to be used as a parameter in FindElements method. And because FindElements parameter requires xpath, I first need to extract the xpath from the mapped element. 

BTW, of course, I know that I can pass the xpath directly as a parameter. But I want all my xpaths stored in Name Mapping and not here or there in keyword/script modules. Therefore, I want to pass either mapped object or string value, representing the mapped/alias object. 

The problem is, that if the mapped element is used directly, as a parameter of function, during runtime, it always triggers the element search. But as I mentioned above, the xpath alone cannot find the element! 

So I decided to assign the mapped element via string constant (to avoid the search of element) and then convert the string back to object using Eval function. Eval correctly converts the string to object, but then, after calling Name property, it again tries to search the mapped element in UI, which, of course fails, not to mention takes long time.

So, is there a way, how to get the xpath of a mapped element, without triggering element search? Thanks. 

3 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    You might be over complicating things here.

    The Name Mapping repository stores all the objects of your tested applications. This is then used to instruct TC on how to locate the object in your app. It's assumed that these objects exists, and you have various methods that can be used to check their existence.

    If you call e.g. NameMapping.Sys.browser.pageCssButtons.buttonCssButton, TC will verify each object i.e. browser, pageCssButtons and buttonCssButton exists based on the property values given.

    Name Mapping can not be used as a "variable", it's used internally by TC.

    Assuming object NameMapping.Sys.browser.pageCssButtons exists, you can check if the object button exists e.g.

    var btn = NameMapping.Sys.browser.pageCssButtons.WaitNamedChild("buttonCssButton", 3000);
    if (btn.Exists) {
      // Performs testing actions over button
      // ...
    } else {
    }

     

  • Hi,

    Thanks for the reply. Well, the things are a bit more complicated, than I described in my initial post :) I just did not want to make the post "overcomplicated" to understand. My problem is only a small part of much bigger form filling method, which fills/saves form inputs from/to CSV file. 

    Here is what I need to achieve:
    How to get xpath from alias object | SmartBear Community

    Basically, I have two xpaths. One pointing to the "main" form element, containing multiple inputs and their labels. The second one, points to individual labels. I'm using mainForm.FindElements(labelsXpath), which returns list of labels, which I'm then looping and filling individual inputs, belonging to each label. 

    Because the second xpath is somewhat complicated and it works ONLY if used as a parameter of FindElements, I don't want to store the xpath in a plain variable or action parameter. I want to use the Name Mapping/Aliases, which I consider to be the only proper way, how to store and use xpaths. Using xpaths here or there in variables and modules makes them unmaintainable. 

    The problem is, that whenever the Name Mapping/Alias object is accessed, TC tests if it's available in UI. But as mentioned above, some of my xpaths are usable ONLY if used via FindElements or similar methods. Therefore, I tried to address the Name Mapping/Alias object as string and convert it back to object, which works great. What does not work, is the Name property, which automatically searches for the element availability.

    All I need is some way, how to get the xpath from a stored Name Mapping/Alias element, even if the element is not available in UI. Thanks.

    • rraghvani's avatar
      rraghvani
      Champion Level 3

      I understand your requirements, but it's not possible.

      All the methods and properties show in this dialog, is based on the object itself, and not the name mapping storage.

      It may have worked for Ranorex, but do not expect the same from TC.

      Saying that, you could read the XML file NameMapping.tcNM, search the for node and attribute of you object, to get the desired value of the XPath.