Site Loader
Auckland, New Zealand
We have already discussed about TestContext in previous posts while trying to fetch data from csv file in data driven testing with Coded UI Testing. TestContext class provides the information and functionality for the current test run. (As like ScenarioContext in Specflow) We can leverage the TestContext class to get the state of test method as shown

Here is the complete video with explanation. Here is the complete code
[CodedUITest]
public class CodedUITest1
{
public CodedUITest1()
{
}

[TestInitialize]
public void Initialize()
{
    //BrowserWindow.Launch("http://executeautomation.com/demosite/Login.html");
    Console.WriteLine("Initializing the Test");

}

[TestMethod]
public void Login()
{
    Console.WriteLine("Login");
}

[TestMethod]
public void Settings()
{
    Console.WriteLine("Setting");
    throw new Exception();
}

[TestMethod]
public void Logout()
{
    Console.WriteLine("Logout");
    throw new Exception();
}

[TestMethod]
public void Admin()
{
    Console.WriteLine("Admin");
}

[TestCleanup]
public void MethodClean()
{
    Console.WriteLine("Test Cleanup");
    if(TestContext.CurrentTestOutcome == UnitTestOutcome.Error)
    {
        Image image = UITestControl.Desktop.CaptureImage();
        image.save(@"D:\captured.jpeg",ImageFormat.jpeg);
        image.dispose();
    }
}
}


Thanks for watching the video and reading the post !!! Please leave your comments and let me know if there is anything to be improved in the post !!! Thanks, Karthik KK

Post Author: Karthik kk

6 Replies to “Using TestContext to identify the state of Test method”

  1. Hi Karthik,

    Thank you for the detailed posts on coded ui. I have been following your post for almost everything related to Coded UI.
    I am stuck at one issue related to TestContext. I want to run my coded ui tests from different application (WPF application project created under the same solution). My test cases are data driven – taking data from CSV files.
    I am using the below given code to access test method from the wpf app:
    Playback.Initialize();
    TestClass obj = new TestClass();
    Playback.Cleanup();

    However, when the control goes to testmethod and it tries to access the “TestContext”, i get an error that TestContext is NULL.
    Could you please give some inputs on this as to how can this be resolved and how can i access/initialize the testContext.

    Thanks.

  2. It seems that if a test is timeout, it won’t execute its TestCleanup method….Is there anyway to force a timeout test to do its cleanup at the end ? Thanks

  3. Hi Karthik,

    I would like to create some codes under the TestCleanup method where the result of a test run is emailed to a certain person. We already have some codes that would handle that. My question is this – can we know what test case id is the test method link in MTM? We would like to include it in the emai – the Testname, TestId, TestResult to name a few.

    Not so good in coding and also new in using CodedUI. Any idea would be helpful.
    Thanks!

  4. Hi Karthik,

    I want to automate a test case in which a migration is triggered in one web application and then it takes some time which we don’t know that how much it would be.
    Once the migration is triggered successfully we have to test some scenarios in another web application.

    Can you suggest a good approach to write coded ui script for such scenarios?

    Thanks!

Leave a Reply to akina Cancel reply

Your email address will not be published. Required fields are marked *