Squish tip of the week: How to Write Test Data to a File in 3 Simple Steps

Squish tip of the week: How to Write Test Data to a File in 3 Simple Steps

Test results have value beyond basic reports; Why not share the data?

Write test-related information (or pretty much anything) to an external file:

Write to a file in 3 simple steps
function main(){
   var file = File.open("C:\\path\\to\\file.txt", "a");
   file.write("Pass:" + String(test.resultCount("passes"))
            + "Fail:" + String(test.resultCount("fails")));
   file.close();
}


Example writing to another application’s measurement file

The example below demonstrates how to write information from a test to a Squish Coco report file, which Squish Coco then imports as part of its coverage measurements.

Not only does the user have data from the automated GUI test, but Squish Coco also reveals what code the test covered and related analysis about the test (assumes Squish Coco installed and configured in advance)

tst_sampleTest.js
source(findFile("scripts","squishCocoLogging.js"))

function main(){
    startApplication("addressbook");
    execution = getExecutionPath();
    logTestNameToCocoReport(squishinfo.testCase,
                            execution);

    try{
       // body of script
       }
       catch(e){
           test.fail('An unexpected error occurred',
                      e.message)
       }
       finally{
           logTestResultsToCocoReport(test, execution)
       }
}


squishCocoLogging.js: does all the external file logging work:
function getExecutionPath(){
    var currentAUT = currentApplicationContext();
    var execution = currentAUT.cwd + "\\" +
                    currentAUT.name + ".exe.csexe"
    return execution;
}

function logTestNameToCocoReport(currTestCase, execution){
    var testExecutionName =
        currTestCase.substr(currTestCase
        .lastIndexOf('\\') + 1);

    var file = File.open(execution, "a");
    file.write("*" + testExecutionName + "\n");
    file.close();
}

function logTestResultsToCocoReport(testInfo, execution){

    var currentAUT = currentApplicationContext();

    // wait until AUT shuts down
    while (currentAUT.isRunning)
      snooze(5);

    // collect test result summary and status
    var positive = testInfo.resultCount("passes");
    var negative = testInfo.resultCount("fails")
                     + testInfo.resultCount("errors")
                     + testInfo.resultCount("fatals");

    var msg = "TEST RESULTS - Passed: " + positive + " | "
               + "Failed/Errored/Fatal: " + negative;

    var status = negative == 0 ? "PASSED" : "FAILED";

    // output results & status to Coco report file
    var file = File.open(execution, "a");
    file.write("<html><body>" + msg + "</body></html>\n");
    file.write("!" + status + "\n")
    file.close();
}

Read more in the KB article and other Squish resources below: