2

I try to make a function (custom command in nightwatch.js), that waits for an elements existence but waits maximum 10 seconds for that. The code in the .execute function is running in an console. I see that its not working because after the 4th time calling this function javascript execution isnt working anymore.

    function waitForElement(selector, scriptName) {
    return this
        .execute(function(sel) {    
            var found = false;    
            function myAsyncRequest() {
              return new Promise((resolve) => {
                var start = Date.now();
                if( $(sel).length || Date.now() - start > 10000 ) {
                    // found = true;
                    resolve();
                }    
              })
            }    
            myAsyncRequest()
              .then( function() {
                found = true;
              })
                 return found;           
        }, 
        [selector], 
        function(result) {
              if ( result.value === true ){
                  console.log( selector + " gefunden.");
              }
              else {
                  throw new Error(selector + " nicht gefunden in der Testdatei " + scriptName );    
              }           
        });
}
Droidboyx
  • 87
  • 1
  • 10
  • 1
    I would assume you have to `return myAsyncRequest()` and continue the chain there, instead of `return found;`, since the async request will still be running and you'll always return false once you do actual async work that won't resolve immediately as shown here. PS: you can use a MutationObserver to detect when an element becomes available inside the DOM so that you do not have to loop this multiple times. – Shilly Oct 02 '19 at 13:04
  • Seems another question that needs [this reference](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – trincot Oct 02 '19 at 13:06
  • As coded, you only resolve when el has a length, since `Date.now() - Date.now()` will never be more than 10000. So the entire async part of this question is redundant. The entire function can be replaced with `return $(sel).length > 0`. – Shilly Oct 02 '19 at 13:10
  • @Shilly could you please make an answer with some code with that? I cant follow you 100% – Droidboyx Oct 02 '19 at 13:16
  • @Droidboyx Are you looping this function every x miliseconds until it returns true? If not, how does this work? And the `myAsyncRequest()`, this is not a placeholder for an actual async request? Since your code is neither async, nor a request. – Shilly Oct 02 '19 at 13:19
  • @Shilly no I call this function only 1 time for every Selector and it should continue only when the element was found or 10s are up – Droidboyx Oct 02 '19 at 13:21
  • @Droidboyx I just opened the nightwatch API reference to look up how `this.execute()` works. First thing I notice when I browse the index pages: `.waitForElementPresent()`. Why isn't that built-in function not usable here? – Shilly Oct 02 '19 at 13:29
  • @Shilly because the function doesnt show in which file the error occurs in the report. Thats why I make custom commands and deliver the parameter scriptName – Droidboyx Oct 02 '19 at 13:31
  • 1
    You could use `.waitForElementPresent()` inside your own function and add the script name in the callback. – Shilly Oct 02 '19 at 13:41
  • 1
    @Shilly you are right, thanks!! If you want you can make an Answer and I will accept it – Droidboyx Oct 02 '19 at 14:04
  • 1
    Glad you found a solution. glhf. You can post the answer yourself as well if you want to write a full explanation. I would have gone with mutationObserver() if `.waitForElementPresent()` did not exist. – Shilly Oct 02 '19 at 14:06
  • I apologise for intervening, but I have to ask, why do you want to write a custom command that is covered by both [expect](https://nightwatchjs.org/api/expect/) assertions (e.g: _something like `browser.expect.element(selector).to.be.visible.before(10000);`_), as well as [waitForElementPresent](https://nightwatchjs.org/api/commands/#waitForElementPresent). Also that snippet of code is hard to follow. – iamdanchiv Oct 03 '19 at 06:45

0 Answers0