通过 selenium 测试浏览器性能 [英] Browser performance tests through selenium

查看:35
本文介绍了通过 selenium 测试浏览器性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用 protractor 来测试内部 AngularJS 应用程序.

除了功能测试之外,我们还借助 protractor-perf 检查性能回归它基于 nodejs browser-perf 库.因为,性能是一种特性".

使用 protractor-perf,我们可以在进行浏览器操作时测量和断言不同的性能特征,例如:

browser.get('http://www.angularjs.org');perf.start();//开始测量指标element(by.model('todoText')).sendKeys('写一个量角器测试');element(by.css('[value="add"]')).click();perf.stop();//停止测量指标if (perf.isEnabled) {//是否启用了性能测量?//检查性能回归,就像检查功能回归一样期望(perf.getStats('meanFrameTime')).toBeLessThan(60);};

<小时>

现在,对于另一个内部应用程序,我们有一组用 Python 编写的基于 selenium 的测试.

是否可以使用 selenium-python 检查性能回归,还是应该使用 protractor 重写测试以便能够编写浏览器性能测试?

解决方案

有可能更接近 browser-perf 正在做什么,通过收集chrome 性能日志 并对其进行分析.

获取性能日志,请开启performance 通过调整 loggingPrefs 所需的功能来记录:

from selenium import webdriver从 selenium.webdriver.common.desired_capabilities 导入 DesiredCapabilitiescaps = DesiredCapabilities.CHROMEcaps['loggingPrefs'] = {'performance': 'ALL'}驱动程序 = webdriver.Chrome(desired_capabilities=caps)driver.get('https://stackoverflow.com')logs = [json.loads(log['message'])['message'] for log in driver.get_log('performance')]with open('devtools.json', 'wb') as f:json.dump(日志,f)驱动程序关闭()

此时,devtools.json 文件将包含一堆跟踪记录:

<预><代码>[{参数":{时间戳":1419571233.19293,"frameId": "16639.1","requestId": "16639.1","loaderId": "16639.2","type": "文档",回复": {"mimeType": "文本/纯文本",状态":200,fromServiceWorker":假,编码数据长度":-1,标题":{"Access-Control-Allow-Origin": "*","Content-Type": "text/plain;charset=US-ASCII"},"url": "数据:,","statusText": "好的",连接 ID":0,连接重用":假,fromDiskCache":假}},方法":Network.responseReceived"},{参数":{时间戳":1419571233.19294,编码数据长度":0,请求 ID":16639.1"},方法":网络加载完成"},..]

现在的问题是,如何处理它.

在 Google 测试自动化会议期间最初建议的一个选项是提交登录到 webpagetest.org.此处有一个java示例,但是,目前,我没有运气在 Python 中实现它.

理论上,webpagetest.org 生成的 UI 报告如下所示:

他们还提供了 JSON/XML 和其他可以进一步分析的格式的指标.

这真的很重要,感谢 Vivek Singh 的指点.

<小时>

browser-perf 还使用日志记录功能来获取跟踪日志并分析数据.

We are using protractor for testing internal AngularJS applications.

Besides functional tests, we check for performance regressions with the help of protractor-perf which is based on nodejs browser-perf library. Because, "Performance is a feature".

With protractor-perf we can measure and assert different performance characteristics while making browser actions, for example:

browser.get('http://www.angularjs.org');

perf.start(); // Start measuring the metrics
element(by.model('todoText')).sendKeys('write a protractor test');
element(by.css('[value="add"]')).click();
perf.stop(); // Stop measuring the metrics 

if (perf.isEnabled) { // Is perf measuring enabled ?
    // Check for perf regressions, just like you check for functional regressions
    expect(perf.getStats('meanFrameTime')).toBeLessThan(60); 
};


Now, for an another internal application we have a set of selenium-based tests written in Python.

Is it possible to check for performance regressions with selenium-python, or should I rewrite the tests using protractor to be able to write browser performance tests?

解决方案

There is a possibility to get closer to what browser-perf is doing by collecting the chrome performance logs and analyzing them.

To get performance logs, turn on performance logs by tweaking loggingPrefs desired capability:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

caps = DesiredCapabilities.CHROME
caps['loggingPrefs'] = {'performance': 'ALL'}
driver = webdriver.Chrome(desired_capabilities=caps)

driver.get('https://stackoverflow.com')

logs = [json.loads(log['message'])['message'] for log in driver.get_log('performance')]

with open('devtools.json', 'wb') as f:
    json.dump(logs, f)

driver.close()

At this point, devtools.json file would contain a bunch of trace records:

[
  {
    "params": {
      "timestamp": 1419571233.19293,
      "frameId": "16639.1",
      "requestId": "16639.1",
      "loaderId": "16639.2",
      "type": "Document",
      "response": {
        "mimeType": "text/plain",
        "status": 200,
        "fromServiceWorker": false,
        "encodedDataLength": -1,
        "headers": {
          "Access-Control-Allow-Origin": "*",
          "Content-Type": "text/plain;charset=US-ASCII"
        },
        "url": "data:,",
        "statusText": "OK",
        "connectionId": 0,
        "connectionReused": false,
        "fromDiskCache": false
      }
    },
    "method": "Network.responseReceived"
  },
  {
    "params": {
      "timestamp": 1419571233.19294,
      "encodedDataLength": 0,
      "requestId": "16639.1"
    },
    "method": "Network.loadingFinished"
  },
  ..
]

Now, the question is, what to do with it.

One option that was initially suggested during the Google Test Automation Conference is to submit the logs to webpagetest.org. There is an example in java available here, but, at the moment, I had no luck implementing it in Python.

In theory, the UI report generated by webpagetest.org would look like this:

They also provide the metrics in JSON/XML and other formats that can be further analyzed.

This is really something, thanks to Vivek Singh for the pointing comment.


browser-perf also uses the logging functionality to pick up the tracing logs, and analyzes the data.

这篇关于通过 selenium 测试浏览器性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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