C# 如何拍摄“异常”的图像;“无消息框”;

C# 如何拍摄“异常”的图像;“无消息框”;,c#,image,coded-ui-tests,C#,Image,Coded Ui Tests,我有以下代码: 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.UItestingW

我有以下代码:

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");
    }
如何拍摄带有“无messageBox”的
异常的图像?

若发生错误,则应调用
Method2()

我不确定是否有帮助,但以下是如何引发异常并拍摄对话框的图像

[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);
        }


    }

可通过以下方式获取整个桌面(包括所需的消息框):

Image pic = UITestControl.Desktop.CaptureImage(); 

永远不要写
throw-ex。你的问题非常不清楚。基本上,我想捕获错误,所以只需检查
ex.ToString()
。不,谷歌搜索只是一个例子,我用来测试如果发生错误,代码是否移动到public void Method2()。