Coded UI Testing Extensibility Points. Writing to Text Files, Visual Studio & the Event Log.

Coded UI Testing Extensibility Points. Writing to Text Files, Visual Studio & the Event Log.

Synopsis: Writing textual information throughout the progression of an automated test library off to a file, perhaps with the variables that were being used or timestamp information etc. is of use and demanded by most testing type people at one time or another. Here’s how you do a lot of it.

So open up your Coded UI Testing Project or create a new one in the manner described in other earlier posts.

The assumption here is that you know how to work with Coded UI in General, UI Maps and a bit of C#.

So the following outputs to the Visual Studio Results window. Not massively complicated but you will have to include some “using” directives. Visual Studio will tell you which ones.

public void CodedUITestMethod1()

{

// To generate code for this test, select “Generate Code for Coded UI Test” from the shortcut menu and select one of the menu items.

// For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463

this.UIMap.ClickTitle();

//Output Text within Visual Studio.

{

Console.Error.WriteLine(“Found Title Bar and Selected.”);

TestContext.WriteLine(“Found Title Bar and Selected.”);

Trace.WriteLine(“Found Title Bar and Selected.”);

Debug.WriteLine(“Found Title Bar and Selected.”);

So, specifically the Output section should give you the following within your Test Results view.

Your browser may not support display of this image.

Useful right?

How about writing to the Event Log? So you can get a lot of this from MSDN samples but if you place the following in your coded ui test you will get numerous outputs to an event log as defined within the script.

{

string sSource;

string sLog;

string sEvent;

sSource = “Test Run”;

sLog = “Test Run Steps”;

sEvent = “There was a break in the application logic”;

if (!EventLog.SourceExists(sSource))

EventLog.CreateEventSource(sSource, sLog);

sEvent = “This is a specific error”;

EventLog.WriteEntry(sSource, sEvent,

EventLogEntryType.Error, 101);

EventLog.WriteEntry(sSource, sEvent,

EventLogEntryType.FailureAudit, 202);

sEvent = “Changing event inline”;

EventLog.WriteEntry(sSource, sEvent,

EventLogEntryType.Warning, 234);

EventLog.WriteEntry(sSource, sEvent,

EventLogEntryType.Information, 7);

EventLog.WriteEntry(sSource, sEvent,

EventLogEntryType.SuccessAudit, 10);

}

This should produce the following in the event log. Again you have to be aware of group policy etc. if you are working on a corporate network or with an account that has reduced privileges.

So you should see the following in the Event Log when you run the script.

Again useful especially for scenarios where you are using Test Agent and distributing tests to drone machines.

And last but not least the old favourite of writing out to a text file. This of course can be html, xml etc.

So the following code:

//Output to a text file.

{

TextWriter tw = new StreamWriter(“C:/test.txt”);

tw.WriteLine(DateTime.Now + ” Test Step 1 has completed.”);

tw.WriteLine(“Text written from Test Script”);

this.UIMap.ClickTitle();

tw.WriteLine(DateTime.Now + ” Test Step 2 has been appended to the log”);

tw.WriteLine(“Additional Information”);

tw.Close();

}

Will produce a text file on the C called test.txt.

If you go and open the file you will see the following.

It’s worth noting that this file is overwritten every time you run the test. So in conclusion you will see below the full coded UI test with all of these features implemented at varying points within the test itself.

using System;

using System.IO;

using System.Collections.Generic;

using System.Text.RegularExpressions;

using System.Windows.Input;

using System.Windows.Forms;

using System.Drawing;

using Microsoft.VisualStudio.TestTools.UITesting;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Microsoft.VisualStudio.TestTools.UITest.Extension;

using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;

using System.Diagnostics;
namespace Extensibility_Points

{

/// <summary>

/// Summary description for CodedUITest1

/// </summary>

[CodedUITest]

public class CodedUITest1

{

public CodedUITest1()

{

}

[TestMethod]

public void CodedUITestMethod1()

{

// To generate code for this test, select “Generate Code for Coded UI Test” from the shortcut menu and select one of the menu items.

// For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463

this.UIMap.ClickTitle();

//Output Text within Visual Studio.

{

Console.Error.WriteLine(“Found Title Bar and Selected.”);

TestContext.WriteLine(“Found Title Bar and Selected.”);

Trace.WriteLine(“Found Title Bar and Selected.”);

Debug.WriteLine(“Found Title Bar and Selected.”);

{

string sSource;

string sLog;

string sEvent;

sSource = “Test Run”;

sLog = “Test Run Steps”;

sEvent = “There was a break in the application logic”;

if (!EventLog.SourceExists(sSource))

EventLog.CreateEventSource(sSource, sLog);

sEvent = “This is a specific error”;

EventLog.WriteEntry(sSource, sEvent,

EventLogEntryType.Error, 101);

EventLog.WriteEntry(sSource, sEvent,

EventLogEntryType.FailureAudit, 202);

sEvent = “Changing event inline”;

EventLog.WriteEntry(sSource, sEvent,

EventLogEntryType.Warning, 234);

EventLog.WriteEntry(sSource, sEvent,

EventLogEntryType.Information, 7);

EventLog.WriteEntry(sSource, sEvent,

EventLogEntryType.SuccessAudit, 10);

}

}

//Output to a text file.

{

TextWriter tw = new StreamWriter(“C:/test.txt”);

tw.WriteLine(DateTime.Now + ” Test Step 1 has completed.”);

tw.WriteLine(“Text written from Test Script”);

this.UIMap.ClickTitle();

tw.WriteLine(DateTime.Now + ” Test Step 2 has been appended to the log”);

tw.WriteLine(“Additional Information”);

tw.Close();

}

}

#region Additional test attributes

// You can use the following additional attributes as you write your tests:

////Use TestInitialize to run code before running each test

//[TestInitialize()]

//public void MyTestInitialize()

//{

// // To generate code for this test, select “Generate Code for Coded UI Test” from the shortcut menu and select one of the menu items.

// // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463

//}

////Use TestCleanup to run code after each test has run

//[TestCleanup()]

//public void MyTestCleanup()

//{

// // To generate code for this test, select “Generate Code for Coded UI Test” from the shortcut menu and select one of the menu items.

// // For more information on generated code, see http://go.microsoft.com/fwlink/?LinkId=179463

//}

#endregion

/// <summary>

///Gets or sets the test context which provides

///information about and functionality for the current test run.

///</summary>

public TestContext TestContext

{

get

{

return testContextInstance;

}

set

{

testContextInstance = value;

}

}

private TestContext testContextInstance;

public UIMap UIMap

{

get

{

if ((this.map == null))

{

this.map = new UIMap();

}

return this.map;

}

}

private UIMap map;

}

}

Leave a Comment
  • Please add 5 and 1 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
Page 1 of 1 (1 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
Page 1 of 1 (1 items)