【问题标题】:Getting the exception message or complete exception in MSTest using reflection使用反射在 MSTest 中获取异常消息或完整异常
【发布时间】:2019-11-28 13:01:16
【问题描述】:

我的 selenium 测试很少,并且在我的全局异常处理块中有针对最预期的异常(如 ElementNotFoundException、ElementNotVisibleException)的异常处理机制。在下面使用 WebDriverWait 的块中,我能够处理已知异常,但有时在我的网页中可能会出现 ElementNotInteractble 异常和其他一些未处理的异常。

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds ((Constants.MediumWaitTime)));

wait.IgnoreExceptionTypes(
    typeof(WebDriverTimeoutException),
    typeof(NoSuchElementException),
    typeof(ElementNotVisibleException),
    typeof(StaleElementReferenceException),
    typeof(ElementClickInterceptedException),
    typeof(ElementNotSelectableException),
    typeof(ElementNotInteractableException));
try
{
    Func(TestCaseInput, testCaseStep);
}

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""";
}

有什么方法可以在 TestCleanUp 中的 MSTest 中捕获或侦听未处理的异常。有没有办法使用 c# 中的一些反射或其他机制来获取 TestExplorer 抛出的异常

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

我能够使用 EventFiringWebDriver 处理从 selenium 引发的每个异常。这解决了我的问题的一部分。但是.net框架引发了一些例外情况,例如我希望下拉列表包含一些值并从下拉列表中选择其中一个值 IList accounts = driver.findElements(By.XPath("//a[id='accounts']")); account.FirstOrDefault().Click()。如果帐户列表没有任何 web 元素,那么它会抛出 "System.InvalidOperationException: Sequence contains no matching element" 。我可以在单击元素之前放置一个 if 条件。 if(accounts.Any()) 但我不能在所有此类下拉列表中设置此类条件。

我浏览了一些在 asp.net 或 .netcore 的情况下有用的链接来记​​录 firstexceptions 或任何异常,但我找不到指向 MSTest 未处理异常的相关链接。此链接https://stackify.com/csharp-catch-all-exceptions/ 对 asp.net 或 .netcore 很有用 记录每个异常:

【问题讨论】:

  • 假设您通过任何魔法手段捕获异常并在 CleanUp 中获取它们的列表。抛出此类异常的测试的预期结果是什么?
  • 测试应该失败并且应该将异常记录到报告中

标签: c# selenium-webdriver mstest


【解决方案1】:

尝试以下方法。扩展 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 是否自动将其设置为 UnitTestOutcome.Failed?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-14
  • 2023-03-08
  • 2013-04-08
  • 1970-01-01
  • 2011-06-08
  • 2016-04-26
  • 1970-01-01
相关资源
最近更新 更多