使用量角器测试验证页面上的信标(https请求)

时间:2015-06-12 03:04:56

标签: javascript protractor

我正在编写一个量角器测试来验证请求是否在页面加载时触发/出现。有没有办法通过量角器测试来捕获请求?

通过监视网络选项卡(开发工具)中的请求调用,可以看到在页面加载时触发。希望自动化这种情况。

另一方面,我尝试在页面上找到该链接,但由于其内部是iframe内容(#iframe_id> html> head>脚本> src =“https ...”),因此它不是可以访问(可以访问吗?)。

感谢任何建议和帮助。

1 个答案:

答案 0 :(得分:1)

根据来自@Kirill的回答,我创建了这个可在量角器“ it”测试中使用的片段:

import { browser } from 'protractor';
import { MyPage } from './pages/myPage.po';

describe('Test my Page', () => 
{
  let page: MyPage;

  beforeAll(() => {
    page = new MyPage();
  });

  it('should display my page and print generated resource traffic', () => {
    page.navigateTo();

    page.clickSomeThingToGenerateResourceCalls();
    page.clickSomeThingelseToGenerateResourceCalls();

    browser.driver.executeScript(function()
    {
      return window.performance.getEntriesByType("resource"); // returns an array of PerformanceResourceTiming objects
    }
    ).then(function (requests)
    {
      console.log(requests);
    });
  });
});

文档链接:

performance.getEntriesByType('resource')

PerformanceResourceTiming object


如果您想过滤requests,可以通过以下方式进行过滤:

browser.driver.executeScript(function()
{
  return window.performance.getEntriesByType("resource"); // returns an array of PerformanceResourceTiming objects
}
).then(function (requests)
{
  for (let index = 0; index < requests.length; index++)
  {
    const element = requests[index];
    if(element.name.indexOf('mydomain') !== -1)
    {
      console.log(element);
    }
  }
});