Tuesday, March 27, 2012

Capturing Screen Shot and Uploading to Result File in Microsoft Unit Testing Framework (MUTF)

One of the great benefits to Microsoft Unit Test Framework, is it allows you to attach files to the be delivered with the result file. This could be a log file, a system file, or in this example a screen shot.

To create the screen shot I am using the tool built directly into WebDriver.

The code is:

Screenshot ss = Base.Driver.GetScreenshot();
ss.SaveAsFile("TempFileLocation", ImageFormat.Png);

This is the code I used, notice I added the GetEnvironmentVariable to grab the Temp directory of the computer, and I added the Date and Time to the file name to create a unique instance of the file. You will want to keep the filename in a variable so you can reuse later on. (

Screenshot ss = Base.Driver.GetScreenshot();
String Filename = Environment.GetEnvironmentVariable("TEMP")+"\\TempFile"+ testName + DateTime.Now.ToString("MMddHHmmss") + ".Png");
ss.SaveAsFile(Filenames[(Filenames.Count-1)], ImageFormat.Png);


To include the file in the result set you will need to instantiate TestContext, then call:

TestContext.AddResultFile("locationAndNameOfFile");

In the example above I would use the code

TestContext.AddResultFile(Filename);

This will add the file to the result set.

No comments:

Post a Comment