ATS : Attach a file to the current testcase

Introduction


It is possible to attach a file to the current test case. This way you can check the file content from within your testcase in Test Explorer.

Imagine while testing your server you want at some moment to have its log file. Now you can attach this file to the testcase, so while reviewing the testcase data you can also see the content of that log file.



Usage

All you need to do is to run code like the following:

import com.axway.ats.log.AtsDbLoggerUtilities;

boolean uploadSuccess = new AtsDbLoggerUtilities().attachFileToCurrentTest( "C:\\logFiles\\log.txt", "TestExplorer-4.0.5", 8080 );
if (! uploadSsuccess) {
  // throw new Exception("message") to fail test 
  // or just log warning if not important
}

where

  • the first argument is the absolute file location on the local file system;
  • the second argument is the name of the TestExplorer application context e.g. for http://192.168.1.100:8080/TestExplorer-4.0.5/ the TestExplorer's context name is " TestExplorer-4.0.5 ". The Test Explorer hostname is automatically resolved from your log4j.xml where you have already defined for DB logging settings;
  • an optional third argument is a port at which Test Explorer is running. If not specified, the default value used is 80.



Here is how it might look like in Test Explorer:

As you can see TestExplorer has an additional Attachments panel.

If you click on a button with the name of your file, its content is displayed. This is possible for text files.

No matter if the file is text or binary, you can always download the file by clicking its link so can examine it with an appropriate viewer on your PC.



File size considerations

Currently, the max file size supported is 10MB.

If your file is larger, we recommend that you either get only the last N lines or take just the new lines. In order to limit the file size of uploaded files, you may compress them.

Get just the last N lines from the log file


 FileSystemOperations op = new FileSystemOperations();

 // getting the last 100 lines from the desired file
 String[] fileLastLines = op.getLastLinesFromFile( "C:\\logFiles\\log.txt", 100 );

 // combining all array elements into one String
 StringBuilder fileContent = new StringBuilder();
 for( String line : fileLastLines ) {
    fileContent.append( line );
    fileContent.append( "\n" );
 }

 // creating the file, that will contain the last 100 lines
 File logChunk = new File( "C:\\logFiles\\logChunk.txt" );
 // make the file to be deleted, after the operation ends
 logChunk.deleteOnExit();
 
 // Save the content into a new file on the system. There are many ways to do this. Talk with us if need assistance
 // ...
 // ...

 // and now attaching the new smaller file
 boolean isAttachedOk = new AtsDbLoggerUtilities().attachFileToCurrentTest( logChunk, "TestExplorer-4.0.5" );
 // if needed, you can check whether the file was attached successfully


Get just the new lines from a text file( assuming this is some typical growing log file)


        String originalFile = "C:\\logFiles\\log.txt";
        FileSystemOperations op = new FileSystemOperations();

        // in the beginning of the test read all current file lines
        FileMatchInfo fileLastLines = op.findNewTextInFile( originalFile, ".*",true);
        StringBuilder fileContent = new StringBuilder();
      
	  	// Here the test is running. The log is growing and at some point you want to get the new lines only
        // ...
        // ...

        // read the new lines since the last read
        fileLastLines = op.findNewTextInFile( originalFile, ".*", true);        
        
        // combining all array elements into one String
        for( String line : fileLastLines.lines) {
            fileContent.append( line );
            fileContent.append( "\n" );
        }
        
        File logChunk = new File( "C:\\logChunk.txt" );
        // Save the content into a new file on the system. There are many ways to do this. Talk with us if need assistance
        // ...
        // ...

        // and now attaching the new file which contains the new lines only
        boolean isAttachedOk = new AtsDbLoggerUtilities().attachFileToCurrentTest( logChunk, "TestExplorer-4.0.5" );
        // if needed, you can check whether the file was attached successfully and either throw exception or just log warning



Attaching a file to a specific testcase

By default, the file will be attached to the currently running testcase.

However, there might be cases when you want to attach the file after the test has already completed, so there isn't a current testcase. In such case you can get the ID of the last executed testcase and give it to the attach method:

// get the the ID of the last executed testcase as it is in the Test Explorer DB
int lastExecutedTestcaseId = ActiveDbAppender.getCurrentInstance().getLastExecutedTestCaseId();

// now call the attach method
boolean isAttachedOK = new AtsDbLoggerUtilities().attachFileToTestcase( lastExecutedTestcaseId, "C:\\logFiles\\log.txt", "TestExplorer-4.0.5" );




Back to parent page

Go to Table of Contents