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

2024-04-30 05:17:51 发布

您现在位置:Python中文网/ 问答频道 /正文

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

除了功能测试之外,我们还使用基于nodejs^{}库的^{}来检查性能回归。因为,"Performance is a feature"

使用protractor-perf我们可以在进行浏览器操作时测量和断言不同的性能特征,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); 
};

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

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


Tags: the应用程序forbyselenium浏览器element性能
3条回答

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

您需要做的是为Selenium正在执行的每个操作设置一个计时器(start/stop)。例如:单击一个按钮并将其记录到一个文件中以供以后使用。

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

Selenium或Webdriver(Selenium 2.0)提供了这一功能。因此,需要进行一些自定义编码才能正常工作。

通过收集chrome performance logs并对其进行分析,有可能更接近what ^{} is doing

get performance logs,请通过调整loggingPrefs所需的功能来打开performance日志:

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中有一个例子可用,但是,目前,我在Python中没有实现它。

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

enter image description here

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

这真的很有意义,多亏了维韦克辛格的指点。


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

不建议通过Selenium进行性能测试,因为它没有针对工作进行优化。硒team将其列为最差实践之一:

It may seem ideal to performance test in the context of the user but a suite of WebDriver tests are subjected to many points of external and internal fragility which are beyond your control; for example browser startup speed, speed of HTTP servers, response of third party servers that host JavaScript or CSS, and the instrumentation penalty of the WebDriver implementation itself. Variation at these points will cause variation in your results. It is difficult to separate the difference between the performance of your website and the performance of external resources, and it is also hard to tell what the performance penalty is for using WebDriver in the browser, especially if you are injecting scripts.

相关问题 更多 >