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

时间:2014-12-22 03:31:51

标签: python selenium selenium-webdriver protractor performance-testing

我们正在使用protractor来测试内部AngularJS应用程序。

除了功能测试之外,我们还会在protractor-perf的帮助下检查性能回归,该browser-perf基于nodejs "Performance is a feature"库。因为,for example

使用protractor-perf,我们可以在制作浏览器操作时测量和断言不同的性能特征{{3}}:

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); 
};

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

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

3 个答案:

答案 0 :(得分:17)

通过收集what browser-perf is doing并分析它们,有可能接近chrome performance logs

get performance logs,请通过调整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()

此时,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"
  },
  ..
]

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

最初建议during the Google Test Automation Conference的一个选项是将日志提交到webpagetest.org。在java 可用here中有一个示例,但是,目前,我没有运气在Python中实现它。

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

enter image description here

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

这要归功于Vivek Singh的指点评论。


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

答案 1 :(得分:4)

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

你想要做的是为Selenium正在执行的每个动作设置一个计时器(开始/停止)。例如:单击按钮并将其记录到文件中以供以后使用。

使用Selenium可以创建性能基线,并从此开始将每个连续结果与基线进行比较。这将为您提供可用于进一步分析的统计信息。

Selenium和Webdriver(Selenium 2.0)开箱即用。因此,需要进行一些自定义编码才能实现。

答案 2 :(得分:1)

不建议通过Selenium进行性能测试,因为它并未针对工作进行优化。硒team已将其列为<最>最坏的习惯之一:

  

对于用户而言,性能测试似乎是理想的选择,但是WebDriver测试套件会受到许多外部和内部脆弱性的影响,这是您无法控制的。例如浏览器的启动速度,HTTP服务器的速度,托管JavaScript或CSS的第三方服务器的响应以及WebDriver实现本身的检测损失。这些点的变化会导致结果变化。很难区分网站的性能和外部资源的性能之间的差异,也很难说出在浏览器中使用WebDriver对性能的影响,尤其是在注入脚本的情况下。

相关问题