Forum Discussion

se7ent7's avatar
se7ent7
Occasional Contributor
14 years ago

Using custom properties to find web objects/elements

Hi there, I have
been finding objects using the Id string (we use a FindChildByID()
method), but this is becoming hard to maintain recently since our app
generates IDs more dynamically these days.



We added a "qaid"
(quality assurance ID) property, which can be seen with F12 (Tools) in
IE or Firebug in Firefox, but not by TestComplete's Object Spy.



How do I get TestComplete to see that property?



Here is an example of how an element looks when using F12 in Internet Explorer:



<span
id="ctl00_ContentPlaceHolderMain_Folders_RepeaterFolders_ctl03_CollapsiblePanelFolder"
_behaviors="[object Object]" CollapsiblePanelBehavior="[object Object]"
qaid="Site Photograph">



So I want to be able to say:

var SitePhotographPanel = WebUtils.FindChildByQAID(this.pageObj, "Site Photograph");



A colleage suggested using EvaluateXPath, so I tried this:

var contentPanel = this.pageObj.EvaluateXPath("//*[@qaid=’Site Photograph’]");



No luck  Please help

6 Replies

  • Hi Gerhardt,



     >>

    We added a "qaid" (quality assurance ID) property, which can be seen with F12 (Tools) in IE or Firebug in Firefox, but not by TestComplete's Object Spy.

    <<

    Yes, custom attributes aren't displayed in the Object Spy, neither they are displayed on the root level of DOM property inspectors in Firebug and Chrome Developer Tools. This is because they aren't DOM properties. In the Object Spy, as well as in Firebug and Chrome DOM inspectors, you can access custom attributes through the attributes property.



    However, in TestComplete scripts, custom attributes can be accessed directly using the dot syntax:

    contentPanel.qaid




    You can also use custom attributes when searching for objects using the Page.FindChild, Page.NativeWebObject.Find and Page.EvaluateXPath methods, in the same way you would use standard DOM properties like id or innerText.



    For example, you can locate your SPAN element by the qaid value in one the following ways:

    var contentPanel = this.pageObj.FindChild("qaid", "Site Photograph", 100 /* search depth */);

    var contentPanel = this.pageObj.NativeWebObject.Find("qaid", "Site Photograph", "span");


    or, using EvaluateXPath:

    ");

    arr = new VBArray(arr).toArray();

    var contentPanel = arr[0];






    >>

    A colleage suggested using EvaluateXPath, so I tried this:

    var contentPanel = this.pageObj.EvaluateXPath("//*[@qaid=’Site Photograph’]");



    No luck

    <<

    The EvaluateXPath method returns an array of found objects, even if there's a single object. Also, it's a Variant (VBScript-format) array, so you also need to convert it to a JScript array, like in my example above. Please see the EvaluateXPath method description for details.



    I hope this answers your questions!
  • se7ent7's avatar
    se7ent7
    Occasional Contributor
    Hi Helen, thanks very much for the reply!



    I have tried both Page.FindChild()  (been using this for many months to find objects by ID), and Page.NativeWebObject.Find(), but none of them find the the object using the custom "qaid" attribute.



    EvaluateXPath does find the object, but (after conversion to the JScript array), the object does not expose any attributes / properties. 



    In the Locals window, it's simply an [Object] and you can't expand it (see attatchment, i called the object "folder" here).



    I can actually find the button I need within the panel / object by using it with FindChild(). So the solution is useable in this case.. 



    But having it as a closed object like this is very limiting.  How can I find (or auto cast?) the object in a way that it still exposes it's content? 



    (And why would FindChild and NativeWebObject.Find be failing to find anything?)



    thanks!
  • Hi Gerhardt,

    >>

    I have tried both Page.FindChild()  (been using this for many months to find objects by ID), and Page.NativeWebObject.Find(), but none of them find the the object using the custom "qaid" attribute.

    <<

    Are you using Google Chrome? I've run some tests and found out that FindChild and NativeWebObject.Find fail to find an object by custom attributes in Chrome indeed, although this works fine in Internet Explorer and Firefox. I've forwarded this to our developers to look into. Thanks for catching this!





    >>

    EvaluateXPath does find the object, but (after conversion to the JScript array), the object does not expose any attributes / properties. 


    In the Locals window, it's simply an [Object] and you can't expand it (see attatchment, i called the object "folder" here).

    <<

    I'm afraid, I can't reproduce this issue. Could you please post here the exact complete script you're using and the URL of your web page? If you can't post them on a public forum, you can open a support ticket and send them privately to our Support Team.

    Does this issue happen only with Chrome or with other browsers as well?
  • tanviagg's avatar
    tanviagg
    New Contributor
    Hi there, the above was of great help. I wanted to ask after finding the object how are we suppose to use the actions like Settext etc. As the intellisense is not showing those options and even if I type it I shows an error. for eg. ojectpath.FindChild.idStr("").SetText("");

  • Hi Tanvi,


     


    Even if the FindChild method returns the first found object matching the search criteria, you'd better check whether the object is found in the object tree indeed via the Exists property. For example:


    function Test()


    {


      var PropArray, ValuesArray, p, w;


      


      // Creates arrays of property names and values


      PropArray = new Array("WndCaption", "Visible");


      ValuesArray = new Array("Font st&yle:", true);


     


      // Searches for the window


      p = Sys.Process("Notepad");


      w = p.FindChild(PropArray, ValuesArray, 5);


     


      // Processes the search results


      if (w.Exists)


        Log.Message(w.FullName);


      else


        Log.Error("The object was not found.");


    }



  • dmohapl's avatar
    dmohapl
    Occasional Contributor
    Hello



    I just noticed that the suggested approach for custom properties works for Firefox but not for Internet Explorer.

    I am using TestComplete 10.20 ; ie 10



    Thanks

    Dana