I have some how implemented the function to wait until page is loaded in intern 4 with TypeScript
. But some times it dosen't work and throws TimeOutEror
even if i catch the error in the function. Can some one please review and tell me whats wrong with it.
Heres my implementation:
export abstract class AdvalentBasePage {
remote: any;
waitTime: number = -1;
locator: any;
constructor(remote: any, locator: any) {
this.remote = remote;
this.locator =locator ;
}
abstract getPageTitleXpath(): string;
getPageTitleElement() {
return this.remote.findByXpath(this.getPageTitleXpath());
}
//this function sometimes throws timeoutError even before waitTime provided in argument
async waitUntilPageIsFullyLoaded(waitTime: number): Promise<any> {
this.waitTime = waitTime;
var self = this;
try {
await self.remote.sleep(1000).findByXpath(self.getPageTitleXpath());
} catch (e) {
console.log(e.name)
if (e.name == 'NoSuchElement') {
if (this.waitTime > 0) {
self.waitTime = self.waitTime - 1000;
await self.waitUntilPageIsFullyLoaded(self.waitTime);
}
else {
throw new Error('TimeOut Exception ')
}
}
}
}
}
There are a couple of different timeouts in play:
findBy
call will timeout if an element isn't found within this time (browser-dependent, typically 0 by default)It's not clear from your description what the specific error is, although it sounds like you may be running into a general test timeout error. You can get around that by increasing the test timeout (this.timeout = x
or this.async(x)
in a test, where x is a number of milliseconds).
It looks like you could also simplify your approach by simply setting a very large find timeout rather than creating the waitUntilPageIsFullLoaded
method. For example (assuming self
is pointing to the Test object):
// Make sure the test timeout is large enough to handle the time
// required to run all test actions.
self.timeout = 130000;
await self.remote
// Set the find timeout to something huge; this is still efficient
// because the test will continue as soon as an element is found
.setFindTimeout(120000)
.sleep(1000)
.findByXpath(self.getPageTitleXpath())