【发布时间】: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