New to Telerik Test Studio? Download free 30-day trial

External Log File

I would like to save the test execution log to an external file on disk.

Solution

Overwrite the OnAfterTestCompleted method for individual tests in order to perform logic on Test Results. See our KB article on Executing Custom Scripts Before/After your Test Run for more information.

You can generate a custom log file by using the TestResult class. Write code in the code-behind file of your test (outside of a coded step). Here's how the placement looks:

Override

We have code samples for two file types:

Text File

public IList<AutomationStepResult> stepResults { get; set; }
public override void OnAfterTestCompleted(TestResult result)
{
    stepResults = result.StepResults;

    string passed = Convert.ToString(result.TotalPassedSteps);
    string notRunSteps = Convert.ToString(result.TotalNumberOfNotRunSteps);
    string overall = Convert.ToString(result.Result);
    string resultMessage = Convert.ToString(result.Message);

    System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\source.txt");

    file.WriteLine(resultMessage);
    file.WriteLine("Number of Passed Steps:"+ passed);
    file.WriteLine("Number of Not Run Steps: "+ notRunSteps);
    file.WriteLine("Total Test Result: "+ overall);
    file.Close();
}

Finally you'll need to Add an Assembly Reference for ArtOfTest.Common.Design. If it is already present, remove it and add it again. Add it from the *Test Studio\Bin* directory:

  • C:\Program Files (x86)\Progress\Test Studio\Bin

Excel File

  • You'll need to add a reference to Microsoft.Office.Interop.Excel.dll. If it isn't already on your machine, ensure you download the version from Microsoft that matches your Office suite.

  • You'll need to create the Excel file on disk first.

public override void OnAfterTestCompleted(TestResult result)
{
    string passed = Convert.ToString(result.TotalPassedSteps);
    string notRunSteps = Convert.ToString(result.TotalNumberOfNotRunSteps);
    string overall = Convert.ToString(result.Result);

    string resultMessage = Convert.ToString(result.Message);  
    string [] split = resultMessage.Split(new Char [] {'\n'});

    Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();           
    string myPath = "C:\\Excel.xlsx";
    Microsoft.Office.Interop.Excel.Workbook workbook = excelApp.Workbooks.Open(myPath);

    int idx = 1;
    foreach (string s in split)
       {
          excelApp.Cells[idx, 1] = s;
          idx++;
       }

    excelApp.Cells[idx + 1, 1] = ("Number of Passed Steps: " + passed);
    excelApp.Cells[idx + 2, 1] = ("Number of Not Run Steps: " + notRunSteps);
    excelApp.Cells[idx + 3, 1] = ("Total Test Result: " + overall);
    excelApp.Visible = true;

    excelApp.ActiveWorkbook.Save();
    workbook.Close(false, Type.Missing, Type.Missing);
    excelApp.Workbooks.Close();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);

    excelApp.Quit();
    GC.Collect();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
}

How to find and use Office PIA's without Visual Studio installed

1.  Make sure that during the installation of Microsoft Office .NET Programmability Support was selected.

.NET Programmability Support

2.  Then you will find the interop assemblies in the Windows Global Assembly Cache, specifically the folder: C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\

This is a hidden protected system folder which won't show up in an ordinary hard drive search. If you try to go to "C:\Windows\assembly" Windows recognizes this as a special folder and will show you the full contents of the GAC in a flattened list instead of the individual folders that make up the GAC.