Search code examples
visual-studiocoded-ui-tests

Coded UI - How to Capture Image After a Mouse Click Event


What is the best option of capturing image after each mouse click in recorded session of Coded UI?

For example, in UIMap.Designer.Cs file I have entered image capturing lines:

Mouse.Click(uIOutagesandSafetyHyperlink, new Point(62, 2));
Image MSOPic1 = UITestControl.Desktop.CaptureImage();
MSOPic1.Save(@"C:\Automation\TestAutomation\web\Recordings\FullSite1\Screenshots\MSO\HomePage_" + DateTime.Now.ToString("ddMMyyyyHHmmss") + ".png");

The above code takes a screenshot but it does not take the current browser's screenshot, just captures another screen where the current browser window does not exist. How can I capture screenshot of the active browser where the test is taking place?

One more question: Does Visual Studio provide an alternative way to capture images during playback? Like through configuration?


Solution

  • One more question: Does Visual Studio provide an alternative way to capture images during playback? Like through configuration?

    By default coded ui test should take a screenshot with each action it does. You can find these screenshots in the html outputs for your tests.

    See: https://msdn.microsoft.com/en-us/library/jj159363.aspx

    If in any case it is not enabled for your tests you have to set: Playback.PlaybackSettings.LoggerOverrideState = HtmlLoggerState.AllActionSnapshot;

    If you want to have this setting in all tests and for all actions call it in the [TestInitialize()] method of each test you want to apply it to.

    Like so:

        [TestInitialize()]
        public void MyTestInitialize()
        {
            Playback.PlaybackSettings.LoggerOverrideState = HtmlLoggerState.AllActionSnapshot;
        }