Protractor:通过视觉回归或响应代码检测图像加载

Protractor: Detect Image Load through Visual Regression or Response Code

我无法让 Protractor 检查图像 link 是否损坏。

我制作了一个本地 html 站点,但没有输入正确的路径。

我正在尝试使用状态代码,并且我已经将 xpath 硬编码到损坏的图像。

browser.waitForAngularEnabled(假);

it('should find all images', function () {
  browser.get('file:///C:/Users/sdasgupta/Documents/PROTRACTOR_E2E_TESTING/TestSite.html');
  var request = require('request');
  var assert = require('assert');
  element.all(by.tagName('a')).then(function(link) {
     var url = element(by.xpath('/html/body/h1/p/img'));
     if(url) {
         request(url, function (error, response, body) {
             assert.equal(response.statusCode, 200, 'Status code is not OK');
         });
      }
  });
});

有更快的方法吗?我的命令行给我这个错误:

特别是这些行:

 Message:
    Failed: Cannot read property 'statusCode' of undefined
  Stack:
    TypeError: Cannot read property 'statusCode' of undefined
        at Request._callback (C:\Users\sdasgupta\protractor_Image\spec.js:11:36)
        at self.callback (C:\Users\sdasgupta\node_modules\request\request.js:186:22)

注意:我最终需要一个检查所有 hyperlinks 的测试。

要验证图像是否损坏,您可以评估 naturalHeight 属性:

var imagesBrokenCount = browser.executeScript(`
  var elms = document.querySelectorAll("img");
  return [].filter.call(elms, e => e.offsetHeight > 1 && e.naturalHeight <= 1).length;
  `);

expect(imagesBrokenCount).toEqual(0);