获取 chrome 的控制台日志 [英] Get chrome's console log

查看:53
本文介绍了获取 chrome 的控制台日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个自动化测试,所以我必须知道chrome控制台出现的错误.

I want to build an automation testing, so I have to know the errors that appear in the console of chrome.

是否可以选择获取出现在控制台中的错误行?

there is an option to get the error lines that appear in the console?

为了看到控制台:在页面的某处右键单击,点击检查元素",然后转到控制台".

In order to see the console: right click somewhere in the page, click "inspect element" and then go to "console".

推荐答案

我不懂 C# 但这里有 Java 代码,希望你能把它翻译成 C#

I don't know C# but here's Java code that does the job, I hope you can translate it to C#

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ChromeConsoleLogging {
    private WebDriver driver;


    @BeforeMethod
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "c:\path\to\chromedriver.exe");        
        DesiredCapabilities caps = DesiredCapabilities.chrome();
        LoggingPreferences logPrefs = new LoggingPreferences();
        logPrefs.enable(LogType.BROWSER, Level.ALL);
        caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
        driver = new ChromeDriver(caps);
    }

    @AfterMethod
    public void tearDown() {
        driver.quit();
    }

    public void analyzeLog() {
        LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
        for (LogEntry entry : logEntries) {
            System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());
            //do something useful with the data
        }
    }

    @Test
    public void testMethod() {
        driver.get("http://mypage.com");
        //do something on page
        analyzeLog();
    }
}

注意上面代码中的setUp方法.我们使用 LoggingPreferences 对象来启用日志记录.有几种类型的日志,但如果您想跟踪控制台错误,那么 LogType.BROWSER 是您应该使用的一种.然后我们将该对象传递给 DesiredCapabilities 并进一步传递给 ChromeDriver 构造函数,瞧——我们有一个启用了日志记录的 ChromeDriver 实例.

Pay attention to setUp method in above code. We use LoggingPreferences object to enable logging. There are a few types of logs, but if you want to track console errors then LogType.BROWSER is the one that you should use. Then we pass that object to DesiredCapabilities and further to ChromeDriver constructor and voila - we have an instance of ChromeDriver with logging enabled.

在页面上执行一些操作后,我们调用analyzeLog() 方法.在这里,我们只是提取日志并遍历其条目.您可以在此处放置断言或进行任何其他您想要的报告.

After performing some actions on page we call analyzeLog() method. Here we simply extract the log and iterate through its entries. Here you can put assertions or do any other reporting you want.

我的灵感来自 Michael Klepikov 的这段代码,它解释了如何从 ChromeDriver 中提取性能日志.

My inspiration was this code by Michael Klepikov that explains how to extract performance logs from ChromeDriver.

这篇关于获取 chrome 的控制台日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆