Write Text Log For Coded Ui Testing in VSTS

How to : Write Text Log For Coded Ui Testing in VSTS

Test Logs are nothing but Results during the Test script run:

Suppose we have a specific scenario of logging into an application ( SignInGlobal in the below discussed case ). In general if we have a successfull Log In we need to maintain a log report that the Scenario has been executed successfully and that the functionality is working fine within the application. Contrarily speaking, in case the scenario/functionality is not working as per the expectation we need the same to be maintained in the log file, so that we can figure out from the Log files after the complete run as to which particular module has failed :

The definition of the below two function calls are available in the UIMap.cs partial class file :

1. SignInGlobal
2. WriteLogs : It is taking two parameters One being the TestC ontext and the other the status message as Failure/Pass.

                this.UIMap.WriteLogs(TestContext, "Logging Into Aplication Event Success");
            catch (PlaybackFailureException e)
                this.UIMap.WriteLogs(TestContext, "Logging Into Aplication Event Failure");

In UiMap.cs Partial class file keep the below function body defined :

 public void WriteLogs(TestContext varTestcontext, string Status)
            string strAppPath = varTestcontext.DataRow["AppPath"].ToString();
            FileInfo f = new FileInfo(strAppPath + "Results.txt");
            StreamWriter w = f.AppendText();
            w.WriteLine("Module : " + Status + " TimeStamp : " + DateTime.Now.ToString());

So this particular function namely WriteLogs will append the current status message in the same file .

This is how we can successfully maintain a Log file for the series of events that are happening during the course of script run.


  1. Hi,
    This is the one I am looking for a long in Coded UI, its really good. But while running my solution, i am getting the object reference error in the below line:
    string strAppPath = varTestcontext.DataRow["AppPath"].ToString();
    Can you help me to fix this issue.

  2. http://csesupport.blogspot.com/2010/10/coded-ui-automation-testing-walkthrough_31.html

    Please go through the below link for gaining insights on how to do data driving of the codedui class file.

    Once you have done this data driving take care of two things.
    One is that copy the address of the directory where you want the result file to be stored.
    once the address is copied paste it in a column named Apppath in the excel sheet.
    RFest all is self explanatroy. I hope that will help you greatly. If you still face issues le me know on it.

  3. Hi
    This error happens 'couse varTestcontext is a datasource that you should fill with the correct information, AppPath is a column inside this datasource, you can use a csv file with a column named appPath, add a row with the correct path, assign the csv file to code ui test method as datasource and it work fine.
    Let me know if you need more information

  4. Ya Chris your comprehension of the post is absolutely right

  5. So... should I quit looking for a way to log to Visual Studio's Test Results? I was hoping to log an error message, or even informational messages, but allow the test to finish and then use Assert.Fail at the end if one or more methods had errors.

    Your post is of course a doable option if what I want to do is not possible.

  6. You can already do that, just put the corresponding method in try catch block, in case the method fails its expected outcome, exception would be caught and you can log the error or information message and the rest of the code sequence would keep runing as you wish to.