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 and 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:

2 Comments

  1. Eswar 6 years ago

    Hello Amanda,

    Can this be done in Squish Web based version? Using python script.

    • Amanda Burma 6 years ago

      Yes, writing to a file can be done in any of the Squish supported scripting languages. Python’s File object takes care of all the functionality you need to do this: https://docs.python.org/2.4/lib/bltin-file-objects.html

      The Squish Coco integration is only for application types support by Squish Coco, which are C, C++, C# and Tcl.

Leave a reply

Your email address will not be published. Required fields are marked *

*