teampoltergeist / poltergeist

A PhantomJS driver for Capybara
MIT License
2.5k stars 415 forks source link

TypeError: undefined is not a constructor - For javascript ForEach method #874

Closed johnloringpollard closed 7 years ago

johnloringpollard commented 7 years ago

I have a page with a forEach javascript loop that works in chrome/firefox but not in my tests. I am not using jQuery. I have to do the old traditional for loop in order to get this to work. I should be able to do a forEach loop right?

phantomjs 2.1.1

coffescript file

  document.querySelectorAll('input').forEach (input) ->
        console.log "Test"

output from test

Task::CreateTest#test_create_tasks:
Capybara::Poltergeist::JavascriptError: One or more errors were raised in the Javascript code on the page. If you don't care about these errors, you can ignore them by setting js_errors: false in your Poltergeist configuration (see documentation for details).

TypeError: undefined is not a constructor (evaluating 'document.querySelectorAll('input').forEach(function(input) {
          return console.log("Test");
        })')
TypeError: undefined is not a constructor (evaluating 'document.querySelectorAll('input').forEach(function(input) {
          return console.log("Test");
        })')
    at http://127.0.0.1:65212/assets/application-53b6248aacf66b96258245b414adeee29bba5e26c58f25dd377486d52549cb67.js:1318
    at :0 in dispatchEvent
    at phantomjs://code/agent.js:249 in keyupdowned
    at phantomjs://code/agent.js:365 in set
    at phantomjs://code/agent.js:90 in nodeCall
    at phantomjs://code/agent.js:15 in externalCall
    at :2
    at :3
    at :3
twalpole commented 7 years ago

Array.prototype.forEach is not in ES5 which is the latest revision PhantomJS 2.1.1 supports. You either need to write ES5 compatible code, transpile, or polyfill missing methods to be able to run on PhantomJS 2.1.1

twalpole commented 7 years ago

Oh and since you're actually writing in coffeescript you don't need to use a traditional for loop - just use comprehensions

console.log "Test" for input in document.querySelectorAll('input')
johnloringpollard commented 7 years ago

I thought it might have been that. Sorry, I should have done more research. Thanks for the improvement on the for loop. I appreciate it!

twalpole commented 7 years ago

@johnloringpollard Whenever PhantomJS 2.5 is released, there is a beta of it available but it has a few issues currently.