kpmck / cypress-ag-grid

Cypress plugin for interacting with ag grid
30 stars 17 forks source link

getAgGridData fails with "Cannot read properties of undefined (reading 'nodeValue') " #14

Closed adbacker closed 2 years ago

adbacker commented 2 years ago

when attempting to parse an ag-grid formatted as attached, getAgGridData fails with "Cannot read properties of undefined (reading 'nodeValue') "

(attached is a sanitized/minimized example page from the app I'm testing)

    // debugger statement is just there as a stop point to validate gridData is populated \n
    // after I figured out where to make the change in agGridInteractions.js
    cy.get(agGridSelector).getAgGridData()
        .then( gridData => {
          debugger
          cy.log("grid data: \n" + gridData);
          debugger
        })

node_modules/cypress-ag-grid/src/agGrid/agGridInteractions.js:14:1 12 | function sortElementsByAttributeValue(attribute) { 13 | return (a, b) => { 14 | const contentA = parseInt(a.attributes[attribute].nodeValue, 10).valueOf(); | ^ 15 | const contentB = parseInt(b.attributes[attribute].nodeValue, 10).valueOf(); 16 | return contentA < contentB ? -1 : contentA > contentB ? 1 : 0; 17 | };

With a null check at the start of sortElementsByAttributeValue the grid parses successfully.

I'm sure there's a better/more maintainable approach, but this is what I used to validate my hunch:

  function sortElementsByAttributeValue(attribute) {
    return(a,b) => {
    if (null == a.attributes[attribute] || null == b.attributes[attribute]) {
      return 0;
    }  
    const contentA = parseInt(a.attributes[attribute].nodeValue, 10).valueOf();
    const contentB = parseInt(b.attributes[attribute].nodeValue, 10).valueOf();
    // ..etc..

I wasn't able to trace back far enough to figure out root cause. My first suspicion would be the empty header in the first and second columns of the grid...

app.zip

(THANK YOU for creating this plugin ... I nearly wept with joy when finding it. It saved me I-don't-know-how-much work to implement something similar that wouldn't have worked as well. :)

kpmck commented 2 years ago

Hi there @adbacker, thank you for filing this, and I'm really glad this plugin has been useful for you! I will take a look at this issue shortly and will reply back ASAP.

kpmck commented 2 years ago

Where it was failing was the assumption is the aria-colindex attribute existed on the actual .ag-cell element. This is used to ensure that the column data and headers are properly aligned, and we have another aria-rowindex used to ensure the data is captured in the exact order as it appears in the grid.

In your implementation, the aria-colindex value was on a child element of .ag-cell šŸ˜± But easy enough fix! I've applied it in the below PR and will be cutting a new version with this fix momentarily. šŸ˜„

https://github.com/kpmck/cypress-ag-grid/pull/15