Squish tip of the week: Confirm expected object timing

Squish tip of the week: Confirm expected object timing

The idea behind this tip came from a question how one could confirm a window disappeared if not interacted with for a given period of time.

The result is quite flexible, and can extend beyond the single case.

Enjoy!

function logWhenObjectGone(obj){
     var d = new Date();
     var initialTime = d.getTime();
     var timeoutDuration = 20000;
     var marginOfError = 2000
     var objectExists = true;

     waitFor("object.exists(obj)", 20000);

     objectExists = waitFor("!object.exists(obj)", timeoutDuration);
     timeEllapsed = (new Date()).getTime() - initialTime;
     if (objectExists == true){
         test.fail("Object still present after " + String(timeEllapsed) + " miliseconds. Should timeout after " + String(timeoutDuration) + " miliseconds, plus or minus " + String(marginOfError) + " milliseconds (margin of error).")
     } else{
         if (timeEllapsed + marginOfError < timeoutDuration){
             test.fail("Object timedout too soon. Expected timeout: " + String(timeoutDuration) + " miliseconds. Actual timeout: " + String(timeEllapsed) + " miliseconds.")
         } else {
             test.pass("Object no longer present within expected timeframe of " + String(timeoutDuration) + " miliseconds, plus or minus " + String(marginOfError) + " miliseconds. Exact timeout detected: " + String(timeEllapsed) + " miliseconds.")
         }
     }
}


function main(){
    startApplication("addressbook");

    //not required, but a visual with error often provides value within the results
    enableScreenshots();

    ...

    // pass a symbolic or real name as the single parameter
    logWhenObjectGone(':Address Book - Add_Dialog');

    ...

    disableScreenshots()
}

function enableScreenshots(){
    testSettings.logScreenshotOnFail = true;
    testSettings.logScreenshotOnError = true;
    testSettings.logScreenshotOnPass = true;
}

function disableScreenshots(){
    testSettings.logScreenshotOnFail = false;
    testSettings.logScreenshotOnError = false;
    testSettings.logScreenshotOnPass = false;