How to write a custom log in LTAF June release?

Published Wednesday, June 10, 2009 3:35 PM

If you want to write a custom log while running your test, the June 2009 release of LTAF now provides an easier way to set that up. The DriverPage gives access to a TestcaseExecutor object that exposes several lifecycle events

Here is a code snippet that shows how to write your own log (MyCustomLog.txt) by attaching to the proper events to gather information while the tests are executing.

private StringBuilder _log;
private string _logPath;

protected override void RunTestCases()
{
    // if user checked the Write Log To Disk checkbox
    if (this.WriteLogToDiskCheckBox.Checked)
    {
        // attach event handlers to write my custom log
        this.TestcaseExecutor.TestcaseExecuted += 
            new EventHandler<TestcaseExecutionEventArgs>(TestcaseExecutor_TestcaseExecuted);
        
        this.TestcaseExecutor.TestRunFinished += 
            new EventHandler<TestRunFinishedEventArgs>(TestcaseExecutor_TestRunFinished);

        this.TestcaseExecutor.TestRunStarted += new EventHandler(TestcaseExecutor_TestRunStarted);

        _logPath = System.IO.Path.Combine(Server.MapPath(""), "MyCustomLog.txt");
        _log = new StringBuilder();
    }
    
    base.RunTestCases();
}

void TestcaseExecutor_TestRunStarted(object sender, EventArgs e)
{
    _log.AppendLine("Test Run Started.");
}

void TestcaseExecutor_TestcaseExecuted(object sender, TestcaseExecutionEventArgs e)
{
    _log.AppendLine(String.Format(
        "Finished running test '{0}'. Status='{1}'", 
        e.WebTestName, 
        e.Passed));
}

void TestcaseExecutor_TestRunFinished(object sender, TestRunFinishedEventArgs e)
{   
    _log.AppendLine("Test Run Finished.");
    
    // make sure that you have write access to the test folder
    System.IO.File.WriteAllText(_logPath, _log.ToString());
}

The "MyCustomLog.txt" file will be written into the same folder where the DriverPage.aspx exists.

Federico Silva Armas
SDET, ASP.NET QA Team

by farmas
Filed under: ,

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)