我希望能够在测试运行期间从浏览器控制台访问所有 JS 警告和错误。禁用“-e”(跳过 JS 错误)标志后,测试会在第一个错误处停止,因此显然它正在寻找它们。启用此标志后,我希望能够查看测试运行期间触发了哪些错误(最好是警告)。
我尝试过使用 ClientFunction 和 window.onerror 方法。我还尝试了 -r 'reports' 标志——我只看到 TestCafe 错误,而不是测试页面中的 JS 错误。我尝试过“访问控制台消息”https://devexpress.github.io/testcafe/documentation/test-api/accessing-console-messages.html https://devexpress.github.io/testcafe/documentation/test-api/accessing-console-messages.html但这仅提供 console.log 等抛出的消息(“请注意,此方法仅返回通过 console.error、console.warn、console.log 和 console.info 方法发布的消息。浏览器输出的消息(例如当页面上发生未处理的异常)将不会返回。”)
const installErrorHandler = ClientFunction(() => {
window.onerror = error => {
console.log("ERROR::::::");
console.log(error);
};
});
在 Clojure 空间中,当使用 Etaoin webdriver 实现(在 Chrome 或 Phantom.js 上)时,在任何时候只需执行
(get-logs driver)
returns
{:level :warning,
:message "1,2,3,4 anonymous (:1)",
:timestamp 1511449388366,
:source nil,
:datetime #inst "2017-11-23T15:03:08.366-00:00"}
....
....
包括任何“未处理的异常”。
我可以不在 TestCafe 中执行此操作吗?
也许您的 window.onerror 示例不起作用,因为它稍后会发生错误。我建议您将错误处理程序提取到单独的 *.js 文件中,并使用新的 testcafe 功能注入它:将脚本注入测试页面 https://devexpress.github.io/testcafe/documentation/using-testcafe/common-concepts/inject-scripts-into-tested-pages.html.
看下面的例子:
要注入的脚本(log-errors.js):
window.unhandledErrors = [];
window.onerror = (message, source, lineno, colno, error) => {
window.unhandledErrors.push({ message, source, lineno, colno, error });
}
test:
import { Selector } from 'testcafe';
fixture('Log errors')
.page('http://example.com')
.afterEach(async t => {
const errors = await t.eval(() => window.unhandledErrors);
console.log(errors);
});
test('test', async t => {
//...
});
通过以下命令运行测试:
testcafe chrome test.js -e --cs=./scripts/log-errors.js
请注意,脚本注入功能自 1.4.0 版本起可用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)