如何自定义 Selenium 测试错误/失败消息 [英] How to customize Selenium test error/fail message
问题描述
我正在使用 VS2012,我想自定义我的 selenium 测试.
I'm using VS2012 and i want to customize my selenium tests.
例如,当测试失败时,我想显示文本页面加载时间过长-无法登录+原始消息
,而不是只显示:OpenQA.Selenium.NoSuchElementException:无法找到 id == loginElementID
的元素.是否可以?UI 测试时如何以及何时使用断言?这将使我的测试更易于理解和提供信息.
For example, when test fails, i want to show the text The page loaded too long - unable to login + original message
instead of showing only this: OpenQA.Selenium.NoSuchElementException: Unable to find element with id == loginElementID
. Is it possible? How and when to use Assertions when UI testing?
That would make my tests more understandable and informative.
var wait = new WebDriverWait(driver, new TimeSpan(0, 0, 0, 8));
wait.Message = "Page Loaded too long";
或者如何在测试失败时显示wait.Message?
Or how to show wait.Message when test fails ?
推荐答案
根据您要实现的目标,至少有两种方法(可能更多).
Depending on what you are trying to achieve there are at least two ways of doing it (and probably more).
使用 try..catch 语句.捕获 NoSuchElementException 并用您自己的消息抛出新的异常.一种方法是用您自己的类包装 WebDriver,然后用 try..catch 包装每个方法(findBy、get).
Use try..catch statement. Catch NoSuchElementException and throw new one with your own message. One way of doing it would be wrapping WebDriver with your own class and then wrapping each method (findBy, get) with try..catch.
使用 EventFiringWebDriver 和 WebDriverEventListener 并在 onError 方法中实现正确的登录.
Use EventFiringWebDriver with WebDriverEventListener and implement proper logging in onError method.
这篇关于如何自定义 Selenium 测试错误/失败消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!