我有一些用 TruClient 脚本编写的自动化测试,我想验证在应用程序 JS 控制台中打印的内容。
有什么方法可以从 javascript 访问它吗?
像 console.getText()
之类的东西?
我想验证控制台中是否出现了一些信息。
例如,我该怎么做?
console.log("Hi");
//Now I want to check if it was printed correctly in the browser
if(console.getText() == "Hi")
{
//then test passed
}
我需要这个,因为我们正在设计新的浏览器。
最佳答案
您可以通过重新定义它们来拦截对 console
的调用:
// Save original console methods
var originalConsole = {
log: console.log,
warn: console.warn,
error: console.error
}
var consoleHistory = [];
console.log = function() {
// Save inputs given to console.log()
consoleHistory.push(arguments);
// Make call to original console
originalConsole.log.apply(window.console, arguments);
}
// Repeat for warn and error, if needed.
Eloquent JavaScript uses this technique在其 code sandbox 中评估 JS 时显示错误.
关于javascript - 有什么方法可以读取在 JS 中以编程方式打印到控制台的内容吗? (供测试用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56521886/