How to take screenshots of a failed test with Telerik Testing Framework


This article is about taking a screenshot with Telerik Test Studio Free Framework. If you want to know how to write tests with Telerik Test Studio Free Framework refer my Previous posts

Lets Start,

First you need a test clean up method, If you're using a VS test Project given from Telerik testing framework its already on your VS test template like in the below screenshot [Figure 1], else you can create a new test method.

Figure 1
If you're creating a new Cleanup test method, follow the below syntax

 // Use TestCleanup to run code after each test has run  
     [TestCleanup()]  
     public void MyTestCleanup()  
     {  
       //  
       // Place any additional cleanup here  
       //  
       #region WebAii CleanUp  
       // Shuts down WebAii manager and closes all browsers currently running  
       // after each test. This call is ignored if recycleBrowser is set  
       this.CleanUp();  
       #endregion  
     }  

This is copied from the Telerik Free test template


Now lets move on to the Topic


The screenshot should be only taken if the test is failed, So Test Cleanup is the best place to palace out code but there is no point of taking screenshots of a passed test. So how do we know whether the test is passed?

You can use thew below code to check that, and you can place this inside an IF condition

 TestContext.CurrentTestOutcome == UnitTestOutcome.Failed  


now we know that our test is failed

To take the screenshot you can use "System.Drawing.Image" reference, use the referance as it is in the code or make sure you don't import Microsoft silver light libraries

Eg:
using ArtOfTest.WebAii.Silverlight;
using ArtOfTest.WebAii.Silverlight.UI;

If you have used this in your code, usually Telerik imports them implicitly, it will take the reference of Silverlight Image class. so to be in the safe side use "System.Drawing.Image"

so following is the code to take the screenshot


 if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed)  
       {  
         System.Drawing.Image img = myManager.ActiveBrowser.Capture();   
         string filename = string.Format("{0}_{1}.jpg", DateTime.Now.ToString("yyyyMMdd_HHmmsss"), TestContext.TestName);  
         img.Save(@"C:\Images\Errors\" + filename, System.Drawing.Imaging.ImageFormat.Jpeg);  
       }  


You can change the output directory to any folder I have used "C:\Images\Errors\" as per my convenience.

So that's it :)

Happy testing!!!



Comments

Popular posts from this blog

Drag and Drop with Telerik Testing Framework

Sheduling tests - Running Tests From the Command line | Telerik Testing Framework

Using page object model with Telerik Framework