Using Test Context as a static variable
One of the problems I ran into when trying to capture a screen shot. I wanted to capture a screen shot only when an error happened. I wanted the screen shot to be called as part of Test Clean Up or Class Clean Up, I also wanted to add a screen shot right when I logged in, to make sure the screen looked normal at the beginning of the class.
If you are using your initialize and clean up attributes correctly, you probably created a class file that contains all of the logic for starting and ending a test. In this class file if you add the Test Context Initialize sequence:
private TestContext m_testContext;javascript:void(0)
public TestContext TestContext
{
get { return m_testContext; }
set { m_testContext = value; }
}
Then add this to the Assembly Initialize:
Screenshot ss = Driver.GetScreenshot()
String fileNames = Environment.GetEnvironmentVariable("TEMP") + "\\TempFile" + testName + DateTime.Now.ToString("MMddHHmmss") + ".Png");
ss.SaveAsFile(fileNames, ImageFormat.Png);
TestContext.AddResultFile();
The Problem
You will get an error on TestContext.AddResultFile(): "An object reference required for the non-static field, method, or property"
Which poses as a big bummer. So does this mean I can't use Test Context for uploading a file on Assembly initialize? No.
Solution
So here's what I did.
First create a tool class if you haven't already that keeps all of the supporting framework methods for your automation. This class should be a static class. In this static class create a two methods 1. Get Screen Shot, 2. Save Screen Shot. The first method will capture the screen shots, and save the file names in a static collection. The second will then use Text Context to go through and upload each of the files contained in the collection and add to the result file. The first method I will call every time I need a screen shot, the second method I will call only on Clean Up attribute at the end to add the files to the result file.
Step 1: Add Static Test Context to Base File
If you have a file that you use for all of your static variables like Driver, Website address, or db connection. Add these lines to the file.
private static TestContext testContextInstance;
public static TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
This will create a static accessible Test Context.
Step 2: Initialize Test Context
On [AssemblyInitialize] include TestContext testContext in the call, then assign testContext to the Static Test Context in the class.
for example:
[AssemblyInitialize]
public static void SetUp(TestContext testContext)
{
TestContext = testContext;
}
Step 3: Create Method in Tool
The following code will create a screen shot using the Web Driver Framework, save the file in a TEMP directory, and Store the full file name in a static string collection.
First: Create a static collection for the filename:
private static List
Filenames = new List();
Second: Create a method to capture screen shot:
public static void GetScreenShot()
{
string testName = Base.TestContext.TestName;
Screenshot ss = Base.Driver.GetScreenshot();
Filenames.Add( Environment.GetEnvironmentVariable("TEMP")+"\\TempFile"+ testName + DateTime.Now.ToString("MMddHHmmss") + ".Png");
ss.SaveAsFile(Filenames[(Filenames.Count-1)], ImageFormat.Png);
}
(Note you will need references: System.Drawing.Imaging; OpenQA.Selenium; System.Collection.Generic; System.Collection.ObjectModel; )
(Additional Note: Notice that we are accessing the static TestContext in the base class to the get the TestName, and include it as part of the file name. )
Step 4: Add Screen Shots to Result FileNow we have to create one more tool method that will add the file to the result set. This method will go through each of the full file names in the static file name collection, and save each file, then delete the temp file.
public static void SaveScreenShot()
{
foreach(string filename in Filenames)
{
Base.TestContext.AddResultFile(filename);
File.Delete(filename);
}
Filenames.Clear();
}
Step 5: Call methodsIn the TestCleanup add the following:
[TestCleanup]
public void CleanUp()
{
Tool.SaveScreenShot();
}
Next Add the following to any place you want to grab a screen shot
For Example here I am getting a screen shot before I sign out of a test.
[TestCleanup]
public void CleanUp()
{
Tool.GetScreenShot();
Tool.SaveScreenShot();
_nav.SignOut();
}