locked
How to capture screenshot if an error accurs RRS feed

  • Question

  • Hi,

    I want to capture screenshot if uitestcoded failed. Visual Studio capture screenshot automaticly but not failed page, just first page.

    Thank you.

    Friday, January 20, 2012 3:42 PM

Answers

  • Yes.

    Assuming you are using MsTest, and have a TestContext property in your test class, in your [TestCleanup] method you can do this:

    if(TestContext.UnitTestOutcome != Passed)

    {

    // Grab a screenshot here

    }

    Grey Ham


    Blog: http://www.havecomputerwillcode.com/blog
    • Marked as answer by EsP_Unholy Monday, January 23, 2012 7:22 AM
    Friday, January 20, 2012 11:49 PM
  • Thanks Grey Ham. :) It works fine :)

     

                if (this.TestContext.CurrentTestOutcome != UnitTestOutcome.Passed)
                {
                    Random random = new Random();
                    int rnd = random.Next(0, 1000000);

                    Image pic = UITestControl.Desktop.CaptureImage();
                    pic.Save(@"C:\Users\enespekkaya\Desktop\"+ rnd +".jpeg");
                }

     

     

    • Marked as answer by EsP_Unholy Monday, January 23, 2012 7:22 AM
    Monday, January 23, 2012 7:22 AM

All replies

  • For any object you have a method: CaptureImage(). You can use it to capture a desktop, browser/object screen shot.
    Please mark this post as answer if this answers your question. Silviu.
    • Proposed as answer by TejasJ Friday, January 20, 2012 5:07 PM
    Friday, January 20, 2012 4:16 PM
  • Use the CaptureImage() method on failure of any assertion or whenever an exception is encountered.
    Friday, January 20, 2012 5:09 PM
  • Thanks for answers.

    I Know CaptureImage() functions. But I knew that this function always capture screenshot whether failed or not fail. I Solved using try catch but is there any other solution?

     

    Thanks.

    Friday, January 20, 2012 8:15 PM
  • Yes.

    Assuming you are using MsTest, and have a TestContext property in your test class, in your [TestCleanup] method you can do this:

    if(TestContext.UnitTestOutcome != Passed)

    {

    // Grab a screenshot here

    }

    Grey Ham


    Blog: http://www.havecomputerwillcode.com/blog
    • Marked as answer by EsP_Unholy Monday, January 23, 2012 7:22 AM
    Friday, January 20, 2012 11:49 PM
  • Thanks Grey Ham. :) It works fine :)

     

                if (this.TestContext.CurrentTestOutcome != UnitTestOutcome.Passed)
                {
                    Random random = new Random();
                    int rnd = random.Next(0, 1000000);

                    Image pic = UITestControl.Desktop.CaptureImage();
                    pic.Save(@"C:\Users\enespekkaya\Desktop\"+ rnd +".jpeg");
                }

     

     

    • Marked as answer by EsP_Unholy Monday, January 23, 2012 7:22 AM
    Monday, January 23, 2012 7:22 AM