Search code examples
sencha-testsencha-test-2.1

Extract grid's records into an object


Say I have a grid and want to extract its records.

let record = {};
grid.rowAt(rowIndex) 
    .cellAt(cellIndex)
    .focus()
    .get('innerText')
    .and(function (future) {
        record.columnName = future.data.innerText;
    });

How can I figure out how many columns and rows a grid has?

Based on the column's index how can I get the name of the respective header?


Solution

  • The following examples should help demonstrate how to get the row count, column count, get a column's title, and get the record object from a row in an Ext JS grid using Sencha Test:

    describe('Grid', function() {
        beforeAll(function() {
            ST.grid('array-grid')
                .rowAt(0);      // Wait for data to be in the grid
        });
    
        it('should check the total number of rows and columns', function() {
            ST.grid('array-grid')
                .execute(function(grid) {
                    var values = {
                        recordCount: grid.getStore().getCount(),    // Get the Store count (same as row count)
                        columnCount: grid.query('gridcolumn:visible').length    // Get the number of visible columns
                    };
    
                    return values;
                })
                .and(function(future) {
                    var result = future.data.executeResult;
    
                    expect(result.recordCount).toEqual(100);
                    expect(result.columnCount).toEqual(6);
                });
        });
    
        it('should get the column title for the fifth column in the grid', function() {
            var columnTitle;
    
            ST.component('array-grid gridcolumn:visible:nth-child(5)')
                .get('text')
                .and(function(future) {
                    // Do something with the column title
                    columnTitle = future.data.text;
    
                    expect(columnTitle).toEqual('Last Updated');
                });
        });
    
        it('should get the record for the third grid row and check some of its values', function() {
            ST.grid('array-grid')
                .rowAt(2)
                .getRecord()
                .and(function(future) {
                    var record = future.data.record;
    
                    expect(record.name).toEqual('Dabvine');
                    expect(record.price).toEqual(29.94);
                });
        });
    });