Ask a Question

setInterval is not defined: Suggestion for best wait method during page loads

SOLVED
nish_b
Contributor

setInterval is not defined: Suggestion for best wait method during page loads

Hi,

 

ISSUE:  ReferenceError: setInterval is not defined

 

DESCRIPTION: I am trying to write a pure JavaScript function for the webpage to fully load before proceeding with my test script. I am using setInterval() to poll until the DOM Load event is fired

 

BACKGROUND: I have already used page.Wait(); for waiting during page load. But still the webelements are not found after the wait. I also have tried a polling function to check whether the element exists.

 

CODE SNIPPETS:

1) Pure JavaScript function for DOM:

      let webpage = Sys.Browser().Page(this.openBrowserWindowURL()).contentDocument; //Get current webpage document

waitForDOMToLoadFullyLoad(webpage){
//The basic check 
if(webpage.readyState === 'complete') {
  // Polling further until load event fires - which means all icons are fully loaded
  let pageisloaded=0;
  let interval = setInterval(function() {
     window.addEventListener('load', (event) => {
     pageisloaded=1;
    })
   if(pageisloaded) {
      Log.Message("DOM Load event is completed!");
      clearInterval(interval);
    }
  }, 100);
}

    }

 

2) POLLING FUNCTION:

isElementPresent(webElement,timeout,pollingTime,frequency) {
let flag=false;
aqUtils.Delay(timeout);
for(let i=0;i<frequency;i++){
  if(webElement.Exists){
    Log.Message("Element exists");
    flag=true;
    break;
  }
 else {
    Log.Message("Waiting for element at iteration: "+i);
    webElement.WaitItem(index,pollingTime);
    }
  }
 return flag;

}


QUESTIONS: 

1. Please suggest which is better option? Code Snippet 1 or 2? Code Snippet 2 is not giving satisfying results. It works sometimes, sometimes it doesn't work.

2. Why am I getting error: setInterval is not defined. Is it not supported in TestComplete?

11 REPLIES 11

The code snippet for getOpenBrowserPage() is below. Well, not sure, maybe it could be possible that the  DOM might be still building the tree. So I will try the Page.Wait before finding xpath.

 

openBrowserWindowURL(){
   var Url=Sys.Browser().Page("*").URL;
   //Log.Message("URL is: "+Url)
   return Url;
 }
 
 getOpenBrowserPage(){
   var page=Sys.Browser().Page(this.openBrowserWindowURL());
   return page;
 }

 

 

Hi @AlexKaras ,

 

Looks like adding the Page.Wait helped! Thank you very much for your help.

 

For the people searching for a solution below is the line of code which solved the issue:

 

for(let i=0;i<frequency;i++)
{
  Sys.Browser().Page("*").Wait(); /*This has resolved the issue as it gets the latest DOM after the delay and hence element is found*/
  Log.Message("Searching for webelement");
  webObj=this.getOpenBrowserPage().FindChildByXPath(xpath);

....

 

 

cancel
Showing results for 
Search instead for 
Did you mean: