Forum Discussion

amathews's avatar
amathews
Occasional Contributor
14 years ago

Script error "Unable to find object" on object that is defined but not used.

In TestComplete programming with Jscript, I'm trying to create an "object repository" script with page elements defined as variables, but when I do that and run a different script in the same project the script will "wait" for the all the defined objects, even though I haven't used them yet.  Why is it doing this and how do I make it stop?

Example:

function test()

{

Log.Message("okay");

}

var participantEdit_participantUrl = Sys.Process("iexplore").Page("*").Panel("container").Panel("pageCell").Panel("pageContent").Panel("content").Form("ParticipantSaveForm").Fieldset(1).Panel(5).Textbox("participantUrl");



I run test() but the script starts "Waiting for Panel("container")" and throws an error after the timeout.
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    If the object is not present in the system memory at the time you run the test, you'll get that error.



    You're not going to be able to create a list of objects in memory in this fashion when the objects don't exist yet in system memory.  That is what NameMapping and Aliases are for.   
  • amathews's avatar
    amathews
    Occasional Contributor
    Well, that's kind of bites.  I say this because I wrote a script that does FindAll for every relevant type of element on the page and creates code for each element definition with pageName_elementName = page.ect.ect.elementDefinition. 



    Okay, so how about this: Is there any way perhaps to create aliases for the NameMapping in a Script step programatically rather than having to go in to set-up the page element names manually?
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Currently, Aliases cannot be defined and created in code.  You'd have to use the NameMapping tool to map out your components ahead of time.



    It's not that you cannot create an object repository in code, just that you'd have to wrap each item in a function to reference it, like below.



    function participantEdit_participantUrl ()

    {

    return Sys.Process("iexplore").Page("*").Panel("container").Panel("pageCell").Panel("pageContent").Panel("content").Form("ParticipantSaveForm").Fieldset(1).Panel(5).Textbox("participantUrl");

    }




    The point is that the object would then resolve upon calling the function.  The code you posted earlier, the variables outside of the function need to resolve first... if you wrap them in functions like above, then you can do all your definitions in code and call them at will.



    I'd suggest though that you liberally use code like FindChild, WaitChild, etc., in your code so that the code does the appropriate waiting until an object exists before it tries to get a reference to it.



    A different tactic would be that, instead of creating objects like this or using name mapping, to liberally use FindChild and FindAllChildren within the tests themselves, specifically create wrapper routines that, given a parent object, you'd find any child object kind of like this



    function GetMyObject(ParentObj, PropertyArray, ValuesArray)

    {

    return ParentObj.FindChild(PropertyArray, ValuesArray, 10, true)

    }
  • amathews's avatar
    amathews
    Occasional Contributor
    Thanks!  The function thing worked.  Though it would be nice for there to be some way to set Aliases dynamically in scripts (especially for those of us coming from a framework like Watir who are used to setting up EVERYTHING in scripts), something like:

    Aliases.add("alias_variable_name", "page.ect.ect.elementDefinition");

  • amathews's avatar
    amathews
    Occasional Contributor
    Here's the Object Repository creation code I wrote (below). It puts the object functions into c:\testfile.txt







    function FindPageObjsAndCreateObjRepository()

    {

    //ObjRep();

    var fso, tf;

    fso = new ActiveXObject("Scripting.FileSystemObject");

    tf = fso.CreateTextFile("c:\\testfile.txt", true);

    var pageObjNames = new Array();

    var pageObjNamesIndex = 0;



    //Page

    page = Sys.Process("IEXPLORE").Page("*");

    pageURL = page.ObjectIdentifier;

      //find page name from Title bar in browser tab

    pageTitle = Sys.Process("iexplore").IEFrame(0).CommandBar.TabBand.TabButton("*").Name.split("\"")[1];

    pageTitleElements = pageTitle.split(" ");

    for(var i = 0; i < pageTitleElements.length; i++)

    {

     if(i == 0)

      pageTitle = pageTitleElements.toLowerCase();

     else

      pageTitle = pageTitle + pageTitleElements;  

    }

    pageTitle = pageTitle.toString().replace(/\W/g, '');

    //Optional: specify page title alias

    pageTitle = "rmLogin";

    tf.writeLine("/*");

    tf.writeLine("\t" + pageTitle + " page objects");

    tf.writeLine("*/");

    tf.writeLine("//var pageURL = \"" + pageURL + "\"\;");

    tf.writeLine("function " + pageTitle + "(){return " + page.FullName.replace(pageURL, "*") + "\;}");



    //Textboxes

    tf.writeLine("//");

    tf.writeLine("//Textboxes");

    tf.writeLine("//");

    pageObjs = page.FindAll("ObjectType", "*Textbox*", 9999);

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

     for(var i = 0; i < pageObjs.length; i++)

     {

      //If page objects have the same name, add an index number for each subsequent entry

      var objNum = 1;

      var pageObjName, newPageObjName;   

      pageObjName = pageObjs.ObjectIdentifier;

      newPageObjName = pageObjName;

      pageObjNames[pageObjNamesIndex] = pageObjs.ObjectIdentifier;

      for(var z = 0; z < pageObjNames.length; z++)

      {

        if((z != i) && newPageObjName == pageObjNames)

          {

           objNum++;

           newPageObjName = pageObjName + objNum;

           pageObjNames = newPageObjName;

          }    

      }

      pageObjName = newPageObjName.toString().replace(/\W/g, '');

        //if object name is just a number, add object type to the name to make it unique

      if(!isNaN(pageObjName))

        pageObjName = "textbox" + pageObjName;

     

     

       tf.writeLine("function " + pageTitle + "_" + pageObjName.replace(/\s+/g, '') + "(){return " + pageObjs.FullName.replace(pageURL, "*") + "\;}");

       pageObjNamesIndex++;

     }

    pageObjs = page.FindAll("ObjectType", "Password*", 9999);

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

     for(var i = 0; i < pageObjs.length; i++)

     {

       tf.writeLine("function " + pageTitle + "_" + pageObjs.ObjectIdentifier.toString().replace(/\W/g, '') + "(){return " + pageObjs.FullName.replace(pageURL, "*") + "\;}");

     }



    //Textareas

    tf.writeLine("//");

    tf.writeLine("//Textareas");

    tf.writeLine("//");

    pageObjs = page.FindAll("ObjectType", "*Textarea*", 9999);

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



     for(var i = 0; i < pageObjs.length; i++)

     {

      //If page objects have the same name, add an index number for each subsequent entry

      var objNum = 1;

      var pageObjName, newPageObjName;   

      pageObjName = pageObjs.ObjectIdentifier;

      newPageObjName = pageObjName;

      pageObjNames[pageObjNamesIndex] = pageObjs.ObjectIdentifier;

      for(var z = 0; z < pageObjNames.length; z++)

      {

        if((z != i) && newPageObjName == pageObjNames)

          {

           objNum++;

           newPageObjName = pageObjName + objNum;

           pageObjNames = newPageObjName;

          }  

      }

      pageObjName = newPageObjName.toString().replace(/\W/g, '');

        //if object name is just a number, add object type to the name to make it unique

      if(!isNaN(pageObjName))

        pageObjName = "textarea" + pageObjName;

     

     

       tf.writeLine("function " + pageTitle + "_" + pageObjName.replace(/\s+/g, '') + "(){return " + pageObjs.FullName.replace(pageURL, "*") + "\;}");

       pageObjNamesIndex++;

     }



    //Drop down selection boxes

    tf.writeLine("//");

    tf.writeLine("//Drop down selection boxes");

    tf.writeLine("//");

    pageObjs = page.FindAll("ObjectType", "*Select*", 9999);

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



     for(var i = 0; i < pageObjs.length; i++)

     {

      //If page objects have the same name, add an index number for each subsequent entry

      var objNum = 1;

      var pageObjName, newPageObjName;   

      pageObjName = pageObjs.ObjectIdentifier;

      newPageObjName = pageObjName;

      pageObjNames[pageObjNamesIndex] = pageObjs.ObjectIdentifier;

      for(var z = 0; z < pageObjNames.length; z++)

      {

        if((z != i) && newPageObjName == pageObjNames)

          {

           objNum++;

           newPageObjName = pageObjName + objNum;

           pageObjNames = newPageObjName;

          }  

      }

      pageObjName = newPageObjName.toString().replace(/\W/g, '');

        //if object name is just a number, add object type to the name to make it unique

      if(!isNaN(pageObjName))

        pageObjName = "dropDownBox" + pageObjName;

     

     

       tf.writeLine("function " + pageTitle + "_" + pageObjName.replace(/\s+/g, '') + "(){return " + pageObjs.FullName.replace(pageURL, "*") + "\;}");

       pageObjNamesIndex++;

     }

     

    //Links

    tf.writeLine("//");

    tf.writeLine("//Links");

    tf.writeLine("//");

    pageObjs = page.FindAll("ObjectType", "*Link*", 9999);

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



     for(var i = 0; i < pageObjs.length; i++)

     {

      //If page objects have the same name, add an index number for each subsequent entry

      var objNum = 1;

      var pageObjName, newPageObjName;   

      pageObjName = pageObjs.innerText;

      newPageObjName = pageObjName;

      pageObjNames = pageObjs.innerText;

      for(var z = 0; z < pageObjNames.length; z++)

      {

        if((z != i) && newPageObjName == pageObjNames)

          {

           objNum++;

           newPageObjName = pageObjName + objNum;

           pageObjNames = newPageObjName;

          }  

      }

      pageObjName = newPageObjName.toString().replace(/\W/g, '');

        //if object name is just a number, add object type to the name to make it unique

      if(!isNaN(pageObjName))

        pageObjName = "link" + pageObjName;

     

     

       tf.writeLine("function " + pageTitle + "_" + pageObjName.replace(/\s+/g, '') + "(){return " + pageObjs.FullName.replace(pageURL, "*") + "\;}");

       pageObjNamesIndex++;

     }

     

    //Buttons

    tf.writeLine("//");

    tf.writeLine("//Buttons");

    tf.writeLine("//");

    pageObjs = page.FindAll("ObjectType", "*Button*", 9999);

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



     for(var i = 0; i < pageObjs.length; i++)

     {

      //If page objects have the same name, add an index number for each subsequent entry

      var objNum = 1;

      var pageObjName, newPageObjName;   

      pageObjName = pageObjs.ObjectIdentifier;

      newPageObjName = pageObjName;

      pageObjNames[pageObjNamesIndex] = pageObjs.ObjectIdentifier;

      for(var z = 0; z < pageObjNames.length; z++)

      {

        if((z != i) && newPageObjName == pageObjNames)

          {

           objNum++;

           newPageObjName = pageObjName + objNum;

           pageObjNames = newPageObjName;

          }  

      }

      pageObjName = newPageObjName.toString().replace(/\W/g, '');

        //if object name is just a number, add object type to the name to make it unique

      if(!isNaN(pageObjName))

        pageObjName = "button" + pageObjName;

     

       tf.writeLine("function " + pageTitle + "_" + pageObjName.replace(/\s+/g, '') + "(){return " + pageObjs.FullName.replace(pageURL, "*").replace(pageObjs.Name, pageObjs.Name.replace(/\s+/g, "*")) + "\;}");

       pageObjNamesIndex++;

     }

     

    //Tables

    tf.writeLine("//");

    tf.writeLine("//Tables");

    tf.writeLine("//");

    pageObjs = page.FindAll("ObjectType", "*Table*", 9999);

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



     for(var i = 0; i < pageObjs.length; i++)

     {

      //If page objects have the same name, add an index number for each subsequent entry

      var objNum = 1;

      var pageObjName, newPageObjName;   

      pageObjName = pageObjs.ObjectIdentifier;

      newPageObjName = pageObjName;

      pageObjNames[pageObjNamesIndex] = pageObjs.ObjectIdentifier;

      for(var z = 0; z < pageObjNames.length; z++)

      {

        if((z != i) && newPageObjName == pageObjNames)

          {

           objNum++;

           newPageObjName = pageObjName + objNum;

           pageObjNames = newPageObjName;

          }  

      }

      pageObjName = newPageObjName.toString().replace(/\W/g, '');

        //if object name is just a number, add object type to the name to make it unique

      if(!isNaN(pageObjName))

        pageObjName = "table" + pageObjName;

        

       tf.writeLine("function " + pageTitle + "_" + pageObjName.replace(/\s+/g, '') + "(){return " + pageObjs.FullName.replace(pageURL, "*") + "\;}");

       pageObjNamesIndex++;

     }

     

     tf.close();



    }

  • amathews's avatar
    amathews
    Occasional Contributor
    And what's the flipping code to make stuff appear in a Code box?  I tried <JScript> ... </JScript> and [JScript] ... [/JScript] to no avail...
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    use the word code in the square brackets... and then to indicate the script language do code=JScript.











    This uses