Verify Beacon(https Request) On The Page Using A Protractor Test
I am writing a protractor test to verify that a request is firing/present on the page load. Is there any way to capture the request by a protractor test? Manually by monitoring the
Solution 1:
You can use
driver.executeScript(window.performance.getEntriesByType("resource")).then(function (requests) {
console.log(requests);
})
to get an array of all http requests (including xmlhttprequests, css image requests, iframes and etc,) on your page and filter this array as you want.
Solution 2:
Based on the answer from @Kirill I created this snipped usable within a protractor "it" test:
import { browser } from'protractor';
import { MyPage } from'./pages/myPage.po';
describe('Test my Page', () =>
{
letpage: MyPage;
beforeAll(() => {
page = newMyPage();
});
it('should display my page and print generated resource traffic', () => {
page.navigateTo();
page.clickSomeThingToGenerateResourceCalls();
page.clickSomeThingelseToGenerateResourceCalls();
browser.driver.executeScript(function()
{
returnwindow.performance.getEntriesByType("resource"); // returns an array of PerformanceResourceTiming objects
}
).then(function (requests)
{
console.log(requests);
});
});
});
Documentation links:
performance.getEntriesByType('resource')
PerformanceResourceTiming object
In case you want to filter the requests
you could do so by:
browser.driver.executeScript(function()
{
returnwindow.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);
}
}
});
Post a Comment for "Verify Beacon(https Request) On The Page Using A Protractor Test"