我想用 Selenium WebDriver + C# 收集控制台中出现的所有控制台错误消息.我只想要控制台错误,例如
I want to collect all the console error messages that appear in the console with Selenium WebDriver + C #. I just want console errors like
控制台错误
按照以下步骤收集浏览器日志,然后输出.
Follow these steps to collect browser logs and then output them.
1 - 创建一个收集错误日志的函数
此函数返回浏览器错误列表.像这样:
1 - Create a function for collecting error logs
This function return a list of browser errors. like this:
private List<string> GetBrowserError()
{
ILogs logs = this.Driver.Manage().Logs;
var logEntries = logs.GetLog(LogType.Browser); // LogType: Browser, Server, Driver, Client and Profiler
List<string> errorLogs = logEntries.Where(x => x.Level == LogLevel.Severe).Select(x => x.Message).ToList();
return errorLogs;
}
2 - 将日志添加到 TestContext
像这样:
private void AddBorwserLogs()
{
string errors = "
*** Errors ***
";
List<string> errorLogs = this.GetBrowserError();
if (errorLogs.Count != 0)
{
foreach (var logEntry in errorLogs)
{
errors = errors + $"{logEntry}
";
}
// Add errors to TestContext
TestContext.WriteLine($"{errors}
Number of browser errors is: {errorLogs.Count}");
}
}
3 - 函数调用
在测试拆卸前调用 AddBorwserLogs()
函数.像这样:
[TestCleanup]
public void TeardownTest()
{
this.AddBorwserLogs();
this.Driver.Close();
this.Driver.Quit();
}
别忘了初始化 WebDriver
像这样:
public IWebDriver Driver;
public TestContext TestContext { get; set; }
[TestInitialize]
public void SetupTest()
{
ChromeOptions options = new ChromeOptions();
options.AddArguments("ignore-certificate-errors");
this.Driver = new ChromeDriver(options);
this.Driver.Manage().Timeouts().PageLoad = new TimeSpan(0, 0, 70);
var url = "https://www.selenium.dev/documentation/";
this.Driver.Url = url;
}
这篇关于如何使用 Selenium WebDriver + C# 获取浏览器控制台错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!