Forum Discussion

SmartBearRD's avatar
5 years ago

Using Javascript Validation to Wait (for something to load) in a Script

Using Javascript Validation to Wait in a Script

 

Some users have expressed a requirement for the script to wait for i.e. a modal, chart or report to render before proceeding further.

 

LoadNinja has a Javascript Validation Continue Waiting feature that should make the script wait until i.e. a 'spinning/loading' icon is gone before continuing.

 

This feature will wait for a target element to either disappear (or appear) before proceeding playback. In a customer's use case, they were targeting an element within a modal with querySelector'#openModal' before clicking "Buy Online". (This was a modal that popped up to generate a coupon). Their validation was attached to the step before they clicked "Buy Online" so the validation would apply before they clicked "Buy Online". The coupon would be applied. (Without this javascript the script would continue before the coupon was generated.)

 

In another use case the customer was waiting for a chart/report to fully render before proceeding.

Once the validation is invoked, the validation will pause the script for however long it needs to.

 

In a use case, the Javascript Validation would be inserted before the point where you want the script to continue. (In the screenshots, there was a report the customer was waiting to be rendered before continuing.)

 

The JavaScript used is attached below as an example. After navigating to Javascript validation click the "Advanced options" which will show the continue waiting feature. Checkmark "Continue Waiting" and have Javascript expression set to "Return true". Copy the code below into the body of your validation changing ('#openModal') to the appropriate querySelector for your issue.

 

let elem = document.querySelector('#openModal');
if (elem) {
let computedStyle = window.getComputedStyle(elem);
if (computedStyle.display === 'none') {
return true;
} else {
return false;
}
}
return true;
}

 

Often a customer has a 'spinning/loading' icon that needs to disappear before continuing. If the loading icon in the application code is '.lds-css', try using a Javascript validation with the following code:


function _wait_for_charts_to_load() {

let elem = document.querySelector('.lds-css');
if (elem) {
let computedStyle = window.getComputedStyle(elem);
if (computedStyle.display === 'none') {
return true;
} else {
return false;
}
}
return true;
}


return _wait_for_charts_to_load();


Please note that timeout delay may need to be increased for the wait time. The relevant documentation for Timeout delay is found here:

 

https://support.smartbear.com/loadninja/docs/scripts/settings.html

No RepliesBe the first to reply