Forum Discussion

arybalka's avatar
arybalka
Occasional Contributor
7 years ago
Solved

How to/improve performance of my function/JavaScript

Hi every one,

I am using very often my function to fill text fields, dropdowns or calendar/date select fields. It is working properly but I am not really happy with performance.

Any advise how to make it faster?

function ClickInputTextField(tag, propertyName, propertyValue, userInput)
{
var page = Sys.Browser().Page("*");
this.tag = tag;
this.propertyName = propertyName;
this.propertyValue = propertyValue;
this.userInput = userInput;
var obj = page.NativeWebObject.Find(propertyName, propertyValue, tag);

var counter = 0;
while(!obj.Exists)//because some times my object is not yet displayed
{
aqUtils.Delay(1000);
Log.Message("Delay for not found object with propertyValue " + propertyValue + " was applied");
counter++;
var obj = page.NativeWebObject.Find(propertyName, propertyValue, tag);
//obj.Click();
if(counter==7){break};
}
obj.Click();
obj.SetText("");//because some times that field is prepopulated
obj.Keys(userInput);// It can be dropdown, so I am typing the string(or begining of the string) to select
aqUtils.Delay(300);//when I don't use that hardcoded <wait> it fails some times for dropdowns
page.Keys("[Enter]"); to apply my select - if it is dropdown or date in calendar
}

 

 

  • The problem, I believe, is in your use of the "Find" method on the NativeWebObject of the page. This is searching the entire page for the object that you want.  Instead of searching the whole page, there are other objects like Panel or Table or Cell within the page that are child objects in the page.  The further down the hierarchy you go so that you're close to the desired object, the faster the Find method will run (less objects to search through).  This would be my first step in improving performance... improve your Find to search closer to the desired object.

8 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    The problem, I believe, is in your use of the "Find" method on the NativeWebObject of the page. This is searching the entire page for the object that you want.  Instead of searching the whole page, there are other objects like Panel or Table or Cell within the page that are child objects in the page.  The further down the hierarchy you go so that you're close to the desired object, the faster the Find method will run (less objects to search through).  This would be my first step in improving performance... improve your Find to search closer to the desired object.

    • arybalka's avatar
      arybalka
      Occasional Contributor

      Thanks, I tried to use object spy to find(or define an object) and the function that is suggested by TestComplet to this object and it is really faster. But in that case I have a huge line of code just to click. I understand that I can map that object and use my allias, but is there any way to avoid mapping?

      I am not really strong, so if you can give me one line of code with generic example (how can I find an object <objects like Panel or Table or Cell> and reference to tutorial - it will be perfect. 

      Thanks.

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Without using mapping, you're going to have to build out your own object model to utilize find/findChild methods to search for and identify objects.  Those methods would then return objects that you can then pass into your "click" function.  This is going to take some programming.  This is one advantage that Mapping has over this methodology in that you don't need to be a "good" programmer to utilize the NameMapping feature.  You can map and organize objects and use them directly via Aliases within your test code without having to build identification methodology.

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    One small question in addition to what was recommended by Robert:

    > page.NativeWebObject.Find(...)

    Any real reason to use .NativeWebObject ?

    This is: a) not portable (you will have to use .NativeChromeObject for Chrome etc.) and b) presumably works slower than just page.Find(...).

    • arybalka's avatar
      arybalka
      Occasional Contributor

      Hi Alex,

      Good Question

      <Any real reason to use .NativeWebObject ?> - No, it is the first way that I learned and I am still using it. I understand that there are disadvantages that I see now, and will be more later, that is way I am looking for alternative.

      About <page.Find(..)> - I don't understand how to use it. Can you give me a line(or reference to the example) of code how to find "Username" at http://support.smartbear.com/samples/testcomplete10/weborders/

       

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Here's the online documentation for the Find method. https://support.smartbear.com/testcomplete/docs/reference/test-objects/members/common-for-all/find-method.html

         

        In the meantime, here's the requested code example.

         

        function findExample(){
            var textboxUserName;
            textboxUserName = Aliases.browser.pageWebOrdersLogin.Find(['ObjectType','ObjectIdentifier'],['Textbox','username'],3,true);
            if (textboxUserName.Exists){
                textboxUserName.Keys('test');
            }
        } 

        Note that I've mapped the page in my Aliases.  Technically speaking I could map this one level further since I don't want to seach the whole page, I just want to search the components in the login panel.  so, I map the panel as panelLogin... in which case the above code alters to this:


        function findExample(){
            var textboxUserName;
            textboxUserName = Aliases.browser.pageWebOrdersLogin.panelLogin.Find(['ObjectType','ObjectIdentifier'],['Textbox','username'],1,true);
            if (textboxUserName.Exists){
                textboxUserName.Keys('test');
            }
        } 

        So, this will find the object as a child of panelLogin which is a much smaller scope of objects to search.  But, honestly, is redundant because the UserName object will ALWAYS be the same identifications... so, I'd prefer, in this case, to map the UserName field.  The properties used in the mapping are the same properties and values indicated in the Find method...  So, my code simplifies even further.

         

        function findExample(){
            var varUserName;
            varUserName= Aliases.browser.pageWebOrdersLogin.panelLogin.WaitAliasChild('textboxUserName', 5000);
            if (varUserName.Exists){
                varUserName.Keys('test');
            }
        } 

        There's no need, here, to "find" anything, just simply to check if the object as mapped exists.  If it does, the code continues.  All the "finding" is taking place within the TestComplete core functionality and, because this is compiled code and not interpreted script code, it will ALWAYS operate faster.