如何使用Selenium WebDriver捕获网络流量并查找Google Analytics(分析)呼叫?

时间:2018-07-10 11:23:49

标签: selenium google-analytics automation automated-tests

我需要使用自动化功能来测试ga调用。

get(LogType.PERFORMANCE).getAll();

提供一组日志,但找不到呼叫请求的URL。
请帮助找到ga呼叫。

2 个答案:

答案 0 :(得分:0)

要使用 Python Selenium 中获取所有性能信息和网络通话,我使用$(document).scroll(function () { var y = $(this).scrollTop(); console.log(y); if (y > startPosition) { fixedContainer.addClass('active'); if (y > stopPosition) { fixedContainer.css('top', stopPosition - y); } else { fixedContainer.css('top', 0); } if(y > (stopPosition/2){ // stopPosition/2 is just example, i still dont get the formula for this condition //scroll to bottom /* add class active to next image */ }else{ /* scroll to top */ /* add class active to prev image */ } } else { fixedContainer.removeClass('active'); } }); 制作一个 JS 调用将返回性能日志:

execute_script

如果您需要除 Python 以外的解决方案,我可以更新答案。

答案 1 :(得分:0)

首先,使用以下代码配置Webdriver:

LoggingPreferences preferences = new LoggingPreferences();
preferences.enable(LogType.PERFORMANCE, Level.ALL);
ChromeOptions option = new ChromeOptions();
option.setCapability(CapabilityType.LOGGING_PREFS, preferences);
WebDriver driver = new ChromeDriver(option);

在关闭浏览器之前,使用以下代码捕获文件中的日志:

OutputStream logfile = new FileOutputStream(new File("A text file path where you can save the logs"),true);
PrintStream printlog = new PrintStream(logfile);
            LogEntries logs = driver.manage().logs().get(LogType.PERFORMANCE);
            for (LogEntry entry : logs) {
                 if(entry.toString().contains("\"type\":\"XHR\"") & entry.toString().contains("\"url\":\"https://example.com/")) {

                    printlog.append(new Date(entry.getTimestamp()) + " " + entry.toString() +" "+ System.getProperty("line.separator"));
                    printlog.append(System.getProperty("line.separator"));
                    printlog.append(System.getProperty("line.separator"));
                }
            }
            printlog.close();

此代码之后,您可以关闭驱动程序。您将在文件中获取所有通话记录。

相关问题