Dem*_*nes 1 javascript python selenium google-chrome
我有一个大型应用程序,正在使用Headless Chrome,Selenium和Python测试每个模块。我想遍历每个模块,并获取在该特定模块内产生的所有JS控制台错误。
但是,由于每个模块都位于不同的测试用例中,并且每个用例都在单独的会话中执行,因此脚本首先必须登录每个测试。登录过程本身会产生许多错误,这些错误会显示在控制台中。测试每个模块时,我不希望无关的登录错误出现在日志中。
基本上,清除当前日志中的所有内容->转到模块并执行某些操作->获取已添加到控制台的日志。
这不可能吗?我尝试这样做,driver.execute_script("console.clear()")但是控制台中的消息并未删除,并且在执行某些操作并打印日志后仍显示与登录有关的消息。
日志记录API尚未成为官方Webdriver规范的一部分。
实际上,要求为 2级规范进行定义。在2017年中,仅Chromedriver对该命令进行了未记录的非标准实现。
在源代码中没有清除日志的方法的痕迹:
Webdriver.get_log()Command名称RemoteConnection返回的(原始)数据结构是一个字典,如下所示:
{
u'source': u'console-api',
u'message': u'http://localhost:7071/console.html 8:9 "error"',
u'timestamp': 1499611688822,
u'level': u'SEVERE'
}
Run Code Online (Sandbox Code Playgroud)
它包含一个可以记住的时间戳,以便以后的调用get_log()可以过滤出新的时间戳。
正面
class WebdriverLogFacade(object):
last_timestamp = 0
def __init__(self, webdriver):
self._webdriver = webdriver
def get_log(self):
last_timestamp = self.last_timestamp
entries = self._webdriver.get_log("browser")
filtered = []
for entry in entries:
# check the logged timestamp against the
# stored timestamp
if entry["timestamp"] > self.last_timestamp:
filtered.append(entry)
# save the last timestamp only if newer
# in this set of logs
if entry["timestamp"] > last_timestamp:
last_timestamp = entry["timestamp"]
# store the very last timestamp
self.last_timestamp = last_timestamp
return filtered
Run Code Online (Sandbox Code Playgroud)
用法
log_facade = WebdriverLogFacade(driver)
logs = log_facade.get_log()
# more logs will be generated
logs = log_facade.get_log()
# newest log returned only
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1523 次 |
| 最近记录: |