Search code examples
javascriptnode.jsnightwatch.js

Is there a way to iterate and compare dom elements in nodejs?


enter image description hereI'm trying to get two values from a site I'm automating. I iterate through an array of elements and try to compare the value of the dropdown to what is on the page to make sure they equal one another. One of the values can only be accessed by .getValue(). The other is accessed by .getText(). I would like to save the result.value of these callback functions and compare the results.

I've tried to console.log both of these values and I get them back, but I can't return anything from this callback function. I also can't seem to save it's value in a variable and return that either. I've tried looking to do it in plain javascript with document.getElementById() but that works for client-side javascript, not serverside like nodejs. Just trying to compare two values together

for (let i = 1; i <= 20; i++) {
  browser
    .element('css selector', `mat-nav-list > a:nth-child(${i})`,
      function(result) {
        if (result.value && result.value.ELEMENT) {
          browser.isVisible(`mat-nav-list > a:nth-child(${i})`,
            function(result) {
              if (result.value === true) {

                browser.click(`mat-nav-list > a:nth-child(${i})`)
                let chunkView = '#mat-input-0';
                let sideBar = `body > gps-app-root > div > div.sidebar-desktop > gps-app-sidebar-menu > div > div.product-list-wrap > mat-nav-list > a:nth-child(${i}) > div`

                browser.getValue(chunkView, function(result) {
                  chunkView = result.value
                  console.log(chunkView)
                })

                browser.getText(sideBar, function(result) {
                  console.log(result.value);
                })
              }
            })
        }
      })
  //.pause(2000)
  //.pause(10000)
}

When I loop through I would expect to get the two values sideBar result.value to equal chunkView result.value. Current output can only log the two separate values.


Solution

  • I found if I nested .getValue() and .getText and assigned variables I was able to compare the two.