18 January 2026:
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.
Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.
How can I run a function on every page load?
mattkruse
Members
I am testing a large app, and I want to check for certain conditions on EVERY page load. For example, make sure that the header is right, that there are no error messages shown, etc. I do not want to customize every recorded script for these common checks.
Is there some hook I can use to run a custom function on page load?
I've been messing with the source in concat.js and I did manage to hook in a call to my custom function, but that function (wrapped in <browser> tags) does not have access to calls like _log() and _fail(), so if I do detect a problem, I can't do anything about it. The code I added was to the bottom of Sahi.prototype.onWindowLoad:
if (typeof onPageLoad=="function") {
onPageLoad();
}
Any ideas? Is something like this already supported, but I just can't find it?
Matt Kruse
Is there some hook I can use to run a custom function on page load?
I've been messing with the source in concat.js and I did manage to hook in a call to my custom function, but that function (wrapped in <browser> tags) does not have access to calls like _log() and _fail(), so if I do detect a problem, I can't do anything about it. The code I added was to the bottom of Sahi.prototype.onWindowLoad:
if (typeof onPageLoad=="function") {
onPageLoad();
}
Any ideas? Is something like this already supported, but I just can't find it?
Matt Kruse
Best Answer
-
Okay, I got a solution working for my needs. Here is what I did:
1. In concat.js:
Sahi.prototype.onWindowLoad = function (e){
...
// Added by Matt to allow running function on every page load
if (typeof onPageLoad=="function") {
onPageLoad();
}
}
2. In common.sah (which I include in every test script):
<browser>
function onPageLoad() {
if(_exists(_div("error_message_display"))) {
_log(_getText(_div("error_message_display")),"failure");
_sahi.stopPlaying();
}
}
</browser>
It works!
Any thoughts?