0

I have the following code:

public void Method1()
{
    try
    {
        this.UIMap.abc(TestContext.DataRow["xxx"].ToString());
        this.UIMap.xyz(TestContext.DataRow["zzz"].ToString());
    }
    catch (Exception ex)
    {
        Image pic = this.UIMap.UItestingWindow.CaptureImage();
        pic.Save(@"C:\result.bmp");
        TestContext.AddResultFile(@"C:\result.bmp");
    }

How can I take an image of an Exception with "No messageBox"? If an error occurs, Method2() should be called.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 3
    **Never** write `throw ex;`. http://stackoverflow.com/a/2999314/34397 – SLaks Feb 23 '14 at 19:19
  • 2
    Your question is extremely unclear. – SLaks Feb 23 '14 at 19:20
  • basicly, I want to capture the error – user3288584 Feb 23 '14 at 19:22
  • So just check `ex.ToString()`. – SLaks Feb 23 '14 at 19:22
  • No, google search is just an example I used to test if code moves to the public void Method2() if error occur. – user3288584 Feb 23 '14 at 19:26
  • forget the google part!!! I want to take the image of the error "catch (Exception ex)" and save it on C:\. But the code is not taking image of error. – user3288584 Feb 23 '14 at 19:31
  • Image pic = this.UIMap.UItestingWindow.CaptureImage(); is only taking picture of Window with no error displayed. allthough, when I use MessageBox.Show(ex.Message); right before Image pic = this.UIMap.UItestingWindow.CaptureImage(); it does show me the error. – user3288584 Feb 23 '14 at 19:33
  • Your question is unclear, what is it you want to save? What happens when you try to save? And what do you want to happen instead? Please [edit the question](http://stackoverflow.com/posts/21973229/edit) to add more details in. – Scott Chamberlain Feb 23 '14 at 20:23
  • The exception will only catch errors in your own program, not in the Internet Explorer process. If you want to automate web browser testing, have a look at ( http://msdn.microsoft.com/en-us/library/dd286726.aspx ). This is an intermediate topic. – Iain Ballard Feb 23 '14 at 20:29
  • I wanted to an image of error, but for some reason the image is not capturing the error. It is only capturing the (application) image with no error display. – user3288584 Feb 23 '14 at 20:32
  • Please explain what you mean by "Image of the error". – Scott Chamberlain Feb 23 '14 at 20:59
  • As far as the program is concerned, no error has occurred. You have captured it with the `catch` statement. – Iain Ballard Feb 23 '14 at 21:00
  • Are you sure your code even gets into the `catch` block? What does it see for `ex`? – John Saunders Feb 23 '14 at 21:13

2 Answers2

0

I'm not sure I'm helping, but here's how you can throw an exception and take an image of the the dialog box.

[TestMethod]
    public void CodedUITestMethod1()
    {
        WinWindow window = new WinWindow();
        window.SearchProperties[WinWindow.PropertyNames.Name] = "Error Window";
        WinButton okButton = new WinButton(window);
        okButton.SearchProperties[WinButton.PropertyNames.Name] = "OK";

        try
        {
            throw new Exception("Coded UI is throwing an exception.  No idea about the Internet explorer state.");
        }
        catch(Exception ex)
        {
            Task.Run(() =>
                {
                    MessageBox.Show(ex.Message, "Error Window");
                }).Start();
            throw new Exception("An unexpected exception occurred in Coded UI",ex);
        }
        finally
        {
            Image pic = window.CaptureImage();
            pic.Save(@"C:\result.bmp");
            Mouse.Click(okButton);
        }


    }
jeff
  • 162
  • 8
0

The whole desktop including the wanted message box can be obtained with:

Image pic = UITestControl.Desktop.CaptureImage(); 
AdrianHHH
  • 13,492
  • 16
  • 50
  • 87