Forum Discussion

ashutosh01's avatar
ashutosh01
Contributor
2 years ago

How to create custom script for dynamic wait for desktop and web

Hi,

I see there are Different waits in selenium for web and desktop but i want to use one custom method for having smart wait like we have in selenium.

can someone give a sample of custom function which i can call for desktop and web differently.

    • ashutosh01's avatar
      ashutosh01
      Contributor

      to many places i had to put wait so looking for custom function.

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        You're still going to have to put that custom function in just as many places.

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Not being very familiar with Selenium, what do you mean by "different waits"?  Waits for what?  

    In TestComplete there are a variety of ways of waiting for an object, page, component, etc., all built in to the various parts of the tool.  If you can describe a bit more as to what you're looking for in the way of examples, scenarios, someone can probably give you at least a start f what you're looking for.  Most likely, such a thing will utilize one or more of those existing waits in TestComplete.

    • ashutosh01's avatar
      ashutosh01
      Contributor

      I am looking to put dynamic wait in multiple places until an object is let's say visible or enabled to perform actions.

      Suppose wait time is 10sec but if object loaded in 3sec then it will move on not wait further 7 sec

       

      and I want to have a custom function where I just pass the object and time, then call this function from wherever i needed for whichever object.

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Almost all the built in Wait functions that rraghvani linked to already do that.  WaitAliasChild, WaitChild, WaitProcess, etc, all operate as a "timeout" where you set a value (10 seconds in your example) but it only waits as long as necessary.

        I'd start looking there, rather than building an extra custom function.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    If in your scipt, you want to wait for 10 objects to appear, then you'll use one of appropriate WaitNNN Methods, 10 times for each object.

     

    Or you could spend time, writing your own custom function, then call it 10 times for each object.

  • eykxas's avatar
    eykxas
    Regular Contributor

    I have created this kind of method. I've called it "fluentWait". (I use javascript)

    this method wait dynamically for an object to be exists in the object tree. (100ms interval, up to 10sec)

    function fluentWait(){
        let stopTime = GetTickCount() + 10000; //wait 10s at max
    
        do {
            var obj = Aliases.your.main.container.WaitNamedChild(MappedChildName, 100);
        }
        while(! obj.Exists && GetTickCount() < stopTime);
    }

     

    • tristaanogre's avatar
      tristaanogre
      Esteemed Contributor

      That works.  However, two things:

      1) I'd make the stop time be calculated based upon an input parameter.  Instead of hardcoding 10 seconds, make that the input parameter for the user to enter the number of milliseconds to wait.

      2) MappedChildName needs to be an input parameter as well.  Somehow, you need to indicate the name that you want to wait for.
      3) The problem is that the named child, in the indicated code, must be an immediate child of the your.main.container object.  What if it's not?  Somehow you need to indicate the parent that you want to search under
      4) WaitNamedChild cannot take alias names, only mapped names.  If you want to search based upon Alias, you should use WaitAliasChild but then, again, you need to designate the parent.

      Here's how I'd re-write it:

      function fluentWait(parentObject, childName, waitTime){
          let stopTime = GetTickCount() + waitTime; //wait 10s at max
      
          do {
              var obj = parentObject.WaitAliasChild(childName, 100);
          }
          while(! obj.Exists && GetTickCount() < stopTime);
      }
  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Using WaitNamedChild Method achieves the same - "Use the WaitNamedChild method to delay the test execution until the specified mapped child object appears in the child list of the given mapped object or the specified time limit is reached". I.e.

    Aliases.your.main.container.WaitNamedChild(MappedChildName, 10000);

     

     

  • eykxas's avatar
    eykxas
    Regular Contributor

    But you have no control on the timing interval (only timeout), and you can't use as is in keyword test.

  • eykxas's avatar
    eykxas
    Regular Contributor

    My real method is not like this. I tried here to simplify it. (I haven't tested this code)

    my real fluentWait call two others method, "waitPageFullyLoaded" and "waitSpinner". Both use the tick count loop, but with an object via xpath not mapped name. (my entire project use xpath).