What is the best efficient way to collect data from a table using a protractor?

What is the best efficient way to collect data from a table using a protractor.

I am collecting the data shown in the code below and it takes 20-30 seconds for 10 lines.

The buildStr counter is designed to create an object for each row, 8 is the number of columns.

    row = {};
    gridRows = [];

    element.all(by.css('#contenttableGrid div[role="gridcell"] div')).each(function(element){
        element.getText().then(function(text){
            row[headerName[buildStr]] = text;
            buildStr++;

            if(buildStr === 8){
                 buildStr = 0;
                 gridRows[rowCounter] = row;
                 rowCounter++;
                 row = {};
            }
        });
    });

      

+3


source to share


1 answer


One way to speed it up what I see is to fetch all the data directly on the page by injecting a script on it. This can be done using browser.executeScript()

( docs ). In your example, Protractor has to make a request to the browser, when you call getText()

, the call number calls = the number of cells in your table. But using it browser.executeScript()

, it will make one call and do whatever is in the browser, which can be very fast. Then you can simply return that data to the test BOM and use it via Promises.

var headerName = {};
// I assume that this variable holds names for headers
// you can pass data from test spec to injected script as arguments (below)

// promise will be resolved with the value you return from `executeScript`
var promise = browser.executeScript(function (headerName) {

    // all the stuff inside this function happens on your page under test
    // it is not a Protractor environment

    var buildStr = 0;
    var rowCounter = 0;
    var row = {};
    var gridRows = [];

    var cells = document.querySelectorAll('#contenttableGrid div[role="gridcell"] div');
    for (var i = 0, l = cells.length; i < l; i++) {
        var text = cells[i].textContent;

        // `headerName` object is passed as an argument from test spec
        row[headerName[buildStr]] = text;
        buildStr++;

        if (buildStr === 8) {
            buildStr = 0;
            gridRows[rowCounter] = row;
            rowCounter++;
            row = {};
        }
    }

    // return all collected data back to test spec
    return gridRows;

}, headerName); // pass helper object from test spec to injectable function

promise.then(function (gridData) {

    console.log(gridData); // result of computations

});

      



Be sure to read the docs for browser.executeScript()

if you want to use it because it has a lot of special points.

+1


source







All Articles