ExecuteAutomation

Using TestContext to identify the state of Test method

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