ssharma
ssharma

Reputation: 941

Protractor loggingPrefs, how to start and stop recording performance logs after certain test step and stop it once its done

I am using loggingPrefs in my test to log all network call/ http request made during the test, I want to know if its possible to log performance logs after certain test step and stop it once that step is done. so that I can validate that certain Http request have been made after the step is executed.

For e.g. :

//Start Recording Performance after user is logged in
//and validate certain HTTP request is made
//Stop Recording Performance 
//Log out from application - I do not want to record performance for Logout step

Upvotes: 0

Views: 626

Answers (1)

Infern0
Infern0

Reputation: 2814

this should do the job. Adapt the code to your needs. Do not forget to attach the browser

capabilities:

'goog:chromeOptions': {
    'perfLoggingPrefs': {
        'enableNetwork': true,
        'enablePage': false
    }
},
loggingPrefs: {
 'performance': 'ALL'
}

Example spec:

import { browser } from 'protractor';

describe('dummy test', () => {
    it('should do something', async () => {
        const startTime = Date.now();

        await browser.waitForAngularEnabled(false);
        await browser.get('https://google.com');

        await browser.sleep(5000);

        const endTime = Date.now();

        await collectPerformLog(startTime, endTime);
    });
});

export async function collectPerformLog(timeFrom, timeTo) {
    await browser.manage().logs().get('performance').then((browserLog) => {

        const fs = require('fs-extra');
        const filePath = '/performance/performance.log';
        fs.ensureDirSync('/performance/');
        let filteredData = browserLog
            .filter(data => timeFrom < data.timestamp && timeTo > data.timestamp);
        fs.appendFile(filePath, JSON.stringify(filteredData));

    }).catch((e) => {
        console.log('Error collection performance logs.', e);
    });
}

Upvotes: 1

Related Questions