Thursday, September 21Be an Automation Engineer

Get performance logs of a web page using protractor

Selenium has the ability to get the resource load times of a web page. Resources means the data that is getting loaded into the webpage from backend http calls.

We can fetch the request and responses of all http calls which are being made as part of page rendering. Selenium is providing access to http calls in the form of logs.

There are different levels of logs available in selenium/Protractor. Please click here to know more about logs.

We can set the logging preferences for above log levels in configuration file. To know more about setting logging preferences click here.

Now, lets practice a small example program to fetch performance log using protractor.

First of all we need to enable performance logs in config file.

exports.config = {
    capabilities: {
        'directConnect': true,
        'browserName': 'chrome',
        'loggingPrefs': {
            'performance': 'ALL'
        }
    },
    framework: 'jasmine',
    specs: ['./specs/GetPerformanceLogs_spec.js'],
    jasmineNodeOpts: {
        defaultTimeoutInterval: 999999
    },
    onPrepare: function () {
        browser.manage().timeouts().implicitlyWait(5000);
        browser.manage().timeouts().setScriptTimeout(60000);
    }
};

Observe that we have added loggingPres under capabilities in the above config file.
'loggingPrefs': {
'performance': 'ALL'
}

We can set different logging levels like ‘SERVER’,’INFO’, ‘WARNING’ etc.. based on our requirements.

Now, let’s see how we can fetch performance logs in the spec.

describe('Capture perfomance logs', function () {
    beforeEach(function () {
        browser.get('http://angularjs.org/')
    });
    it('Get performance data of angularjs.org webpage', function () {
        browser.manage().logs().get('performance').then(function (browserLog) {
            if (browserLog.length > 0)
                JSON.parse(JSON.stringify(browserLog)).forEach(function (browserLog) {
                    console.log('log: ' + browserLog.message);
                });
        });
    });
});

Observe that we are fetching performance logs in the above program using browser.manage().logs().get('performance') statement. It returns JSON data.

Note: Performance logs can only be accessed from chrome browser. Remaining browsers(firefox, opera, safari etc..) which are supported by protractor are not providing performance logs.

%d bloggers like this: