通过selenium进行浏览器性能测试

ale*_*cxe 20 python selenium performance-testing selenium-webdriver protractor

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

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

随着protractor-perf我们可以测量并同时使浏览器的行为,主张不同的性能特点,例如:

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); 
};
Run Code Online (Sandbox Code Playgroud)

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

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

ale*_*cxe 17

有一种可能性,更贴近什么browser-perf是做通过收集铬性能日志和分析它们.

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

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()
Run Code Online (Sandbox Code Playgroud)

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

[
  {
    "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"
  },
  ..
]
Run Code Online (Sandbox Code Playgroud)

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

最初在Google测试自动化会议期间建议的一个选项是将日志提交到webpagetest.org.有一个例子在Java中可用在这里,不过,此刻,我没有运气Python实现它.

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

在此输入图像描述

它们还提供JSON/XML和其他可以进一步分析的格式的度量标准.

这真的是一件事,感谢Vivek Singh的指点评论.


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


Pau*_*aul 5

可以使用Selenium进行性能回归测试。但是,您可能已经注意到。Selenium的核心本质是它模仿用户行为。这意味着Selenium仅在用户能够执行相同操作时才执行该操作(例如,单击按钮)。还考虑到某些代码,甚至能够运行Selenium脚本所需的解决方法(即,艰苦的等待,各种检查和自定义代码)。这意味着与传统的性能测试相比,使用Selenium进行性能测试的“定义”将稍有不同。

您将要做的是为Selenium所执行的每个动作都有一个计时器(开始/停止)。例如:单击一个按钮,并将其记录到文件中以备后用。

使用Selenium,您可以创建性能基准,然后将每个连续的结果与基准进行比较。这将为您提供统计数据,然后您可以将其用于进一步的分析。

Selenium或Webdriver(Selenium 2.0)均附带此功能。因此,需要进行一些自定义编码才能使其正常工作。