C# 使用反射在MSTest中获取异常消息或完成异常

C# 使用反射在MSTest中获取异常消息或完成异常,c#,selenium-webdriver,mstest,C#,Selenium Webdriver,Mstest,我有几个selenium测试,并且在我的全局异常处理块中有一些异常处理机制,用于最预期的异常,如ElementNotFoundException、ElementNotVisibleException。在下面使用WebDriverWait的代码块中,我能够处理已知的异常,但有时在我的网页中可能会出现ElementNotInteractible异常和一些其他未处理的异常 WebDriverWait wait=new WebDriverWaitdriver,TimeSpan.FromSeconds C

我有几个selenium测试,并且在我的全局异常处理块中有一些异常处理机制,用于最预期的异常,如ElementNotFoundException、ElementNotVisibleException。在下面使用WebDriverWait的代码块中,我能够处理已知的异常,但有时在我的网页中可能会出现ElementNotInteractible异常和一些其他未处理的异常

WebDriverWait wait=new WebDriverWaitdriver,TimeSpan.FromSeconds Constants.MediumWaitTime; wait.IgnoreExceptionTypes WebDriverTimeOutException类型, NoschelementException的类型, 元素的类型NotVisibleException, typeofStaleElementReferenceException, 元素类型ClickInterceptedException, 元素的类型NotSelectableException, ElementNotInteractiableException的类型; 尝试 { FunctTestCaseInput,testCaseStep; } 捕获WebDriverTimeoutException WebDriverTimeoutException { testCaseStep.ErrorMessage=$@方法中出现的异常: “{MethodBase.GetCurrentMethod.Name}”异常:“{webDriverTimeoutException.StackTrace}” WebDriverTimeoutException.InnerException是:{WebDriverTimeoutException.InnerException} 由于超时而发生异常; } 在TestCleanUp中,是否有任何方法可以捕获或侦听MSTest中未处理的异常。在c语言中,是否有任何方法可以使用一些反射或其他机制从TestExplorer抛出异常

注意:我不希望每个方法都有一些try/catch块

我能够使用EventFiringWebDriver处理selenium引发的每个异常。这解决了我问题的一部分。但也有一些从.net framework引发的异常,例如,我希望下拉列表包含一些值,并从下拉列表中选择一个值,如 IList accounts=driver.findElementsBy.XPath//a[id='accounts']; accounts.FirstOrDefault.Click。如果accounts列表没有任何webelements,则会抛出System.InvalidOperationException:序列不包含匹配的元素。我可以在单击元素之前设置if条件。但我不能把这些条件放在所有这些下降的周围

我浏览了一些在asp.net或.netcore情况下有用的链接,以记录firstexceptions或任何异常,但我找不到MSTest未处理异常的相关链接。此链接对于asp.net或.netcore非常有用
要记录每个异常:

请尝试以下方法。扩展TestMethod并用新方法装饰

public class SeleniumTestMethodAttribute : TestMethodAttribute
{
     public override TestResult[] Execute(ITestMethod testMethod)
     {
        try {
             base.Execute(testMethod);
        }
        catch (WebDriverTimeoutException webDriverTimeoutException)
        {                    
            testCaseStep.ErrorMessage = $@"""Exception Occoured in Method: 
                '{MethodBase.GetCurrentMethod().Name}' with Exception: '{webDriverTimeoutException.StackTrace}'                      
                    ""WebDriverTimeoutException.InnerException is::"" {webDriverTimeoutException.InnerException}                        
                ""Exception occured due to timeout""";
        }
    }
}
然后用

[SeleniumTestMethodAttribute]
public void TestMethodWithSelenium() {
}

试试下面的方法。扩展TestMethod并用新方法装饰

public class SeleniumTestMethodAttribute : TestMethodAttribute
{
     public override TestResult[] Execute(ITestMethod testMethod)
     {
        try {
             base.Execute(testMethod);
        }
        catch (WebDriverTimeoutException webDriverTimeoutException)
        {                    
            testCaseStep.ErrorMessage = $@"""Exception Occoured in Method: 
                '{MethodBase.GetCurrentMethod().Name}' with Exception: '{webDriverTimeoutException.StackTrace}'                      
                    ""WebDriverTimeoutException.InnerException is::"" {webDriverTimeoutException.InnerException}                        
                ""Exception occured due to timeout""";
        }
    }
}
然后用

[SeleniumTestMethodAttribute]
public void TestMethodWithSelenium() {
}

假设您通过任何神奇的方法捕获异常,并在清理中获得它们的列表。抛出此类异常的测试的预期结果是什么?测试应该失败,并且异常应该记录到报告中。测试人员说,您可以通过任何神奇的方法捕获异常,并在清理中获得异常列表。抛出此类异常的测试的预期结果是什么?测试应该失败,并且应该将异常记录到报告中如果发生异常,TestResult[]会发生什么情况?MSTest是否自动将其设置为UnitTestResult.Failed?如果发生异常,TestResult[]会发生什么情况?MSTest是否自动将其设置为UnitTestOutput。失败?