Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive functions inside evaluate #320

Closed
chiefsmurph opened this issue Nov 10, 2017 · 1 comment
Closed

Recursive functions inside evaluate #320

chiefsmurph opened this issue Nov 10, 2017 · 1 comment

Comments

@chiefsmurph
Copy link

chiefsmurph commented Nov 10, 2017

Perhaps this issue should be titled more generally "Function declarations within evaluate()", however more specifically, I am trying to make a recursive call within evaluate(). Use case: One of those "endless scrolling divs". So I want to scroll to bottom, see if more has been added to the div, and then continue...

horseman
        .open(url)
        .wait(6000)
        .click('button#openEndlessDiv')
        .wait(2000)
        .evaluate(function(done) {
        
          var scrollDiv = document.querySelector('#scrollableDiv');
          var scrolls = [];
        
          var scrollIt = function() {
            var beforeHeight = scrollDiv.scrollHeight;
            scrolls.push(beforeHeight);
            setTimeout(function() {
              var nowHeight = scrollDiv.scrollHeight;
              if (nowHeight === beforeHeight) {
                done(null, scrolls);
              } else {
                scrollIt();
              }
            }, 3000);
            $(scrollDiv).scrollTop(currentHeight);
          };
        
          scrollIt();
        
        })
        .log()
        .evaluate(function() {
          var allLis = [].slice.call(document.querySelectorAll('#scrollableDiv li'));
          return allLis.map(function(li) {
            return $(li).text();
          });
        })
        .then(list => {
          horseman.close();
          resolve(list);
        });

Get this error: Unhandled rejection fn evaluate

I tried injectJs('[external and relative url with that function]') and that did not work. Does this mean there can't be function declarations whatsoever within evaluate?

It does work (loads 1 more collection of results) when I remove the scrollIt function declaration and only scroll to the bottom once.

@chiefsmurph
Copy link
Author

Well apparently I made the silly mistake of referencing currentHeight when the var I was referencing was beforeHeight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant