Ever wondered how to override the location associated with test results?
A function which (temporarily) enforces that test results created by Squish functions, for example test.compare, will have their report entry rewritten such that the location of the result entry is no longer the place where text.compare was called but rather any of the ancestors frames of the current function.
This can be very useful to solve problems with code duplications or where comparisons and verifications are consolidated into separate functions.
The test.fixateResultContext function adjusts the call stack frame which is used when generating test result entries. By default, it will rewrite the location of all result entries such that they seem to stem from the parent stack frame.
Triggered by the call to test.compare the following test result entry will be logged with a location pointing into the main() function, not into the windowcheck() function:
def windowcheck(): test.fixateResultContext(1) waitFor("object.exists(':Address Book - Unnamed_Window')", 20000) test.compare(findObject(":Address Book - Unnamed_Window")["class"], "Addressbook.MainWindow") test.restoreResultContext() def main(): startApplication("Addressbook") windowcheck() mouseClick(waitForObject(":New_ToolbarItem")) windowcheck() mouseClick(waitForObject(":Add_ToolbarItem")) type(waitForObject(":Address Book - Add.Forename:_Edit"), "Peter") type(waitForObject(":Address Book - Add.Surname:_Edit"), "Lustig") type(waitForObject(":Address Book - Add.Email:_Edit"), "123") type(waitForObject(":Address Book - Add.Phone:_Edit"), "email@example.com") windowcheck() clickButton(waitForObject(":Address Book - Add.OK_Button")) windowcheck() mouseClick(waitForObject(":Remove_ToolbarItem")) clickButton(waitForObject(":Address Book - Delete.Yes_Button")) windowcheck()
Note how the test.compare function is wrapped in a test.fixateResultContext(1) and test.restoreResultContext() pair. It means that the results generated by test.compare are reported at the location where windowcheck() is called in main().
Example test result (click image to enlarge):
By passing an optional argument (ancestorFrameIndex) “1” you can pass other values to reference other ancestor stack frames, e.g. “2” would reference the grand-parent stack framer (the caller of the caller).
To avoid misleading test reports always call test.restoreResultContext after calling test.fixateResultContext!
As calls can also be nested, i.e. multiple subsequent calls to fixateResultContext it is required that restoreResultContext gets called multiple times as well.