Nightwatch .execute() how to pass parameter to execute function, is it possible?
Asked 07 September, 2021
Viewed 1.2K times
  • 56
Votes

Please be patient - I am a beginner in programming. Tester for long time but programming is not my domain.

My test is:

  1. from the backend I get some list with some element (e.g. 5 text strings)
  2. I click some element on page which displayed those 5 elements (of course I don't know if listed elements are correct or not)
  3. I need to check if list of elements displayed on ui is the list received from backend

Problem:

  • I cannot access the elements by Nightwatch api css selector, at least I could not manage (Angular app) to do it with Nightwatch
  • I found I could do it with .execute()

My code is (failing):

browser
            .click(selector.HEADER.APPS_GRID, function () {
                for (var app in appsList) {
                    let appShortName = appsList[app].shortName
                    let appLongName = appsList[app].longName
                    let appUrl = appsList[app].url
                    let appVisibility = appsList[app].visibility
                    browser.execute(function(app){
                        var appShortNameDisplayed = document.getElementsByClassName('logo-as-text')[app].innerText
                        var appLongNameDisplayed = document.getElementsByClassName('app-name')[app].innerText
                        return [appShortNameDisplayed, appLongNameDisplayed]
                    }, function(result){
                        console.log(result.value[0])
                    })
                }
            })

It fails in lines:

var appShortNameDisplayed = document.getElementsByClassName('logo-as-text')[app].innerText
var appLongNameDisplayed = document.getElementsByClassName('app-name')[app].innerText
  • unfortunately I have to make query with [app] - iterating by elements of object. If I skip [app].innerText I get some data like element-6066-11e4-a52e-4f735466cecf instead of text values displayed on page

I get error:


  

Error while running .executeScript() protocol action: TypeError: document.getElementsByClassName(...)[app] is undefined

Is it possible to pass the "app" param (counter) to the document query?

Or is it the way I have to make one query that will return as many data as necessary and then handle data returned in this block

function(result) {
   console.log(result.value[0])
  })

The fragment of html page is

<div _ngcontent-c8="" class="ep-app-icon mt-auto mb-auto text-center logo-as-text"> XXX </div>

... and I need to get this "XXX" text.

2 Answer