OnetapInc / chromy

Chromy is a library for operating headless chrome. 🍺🍺🍺
MIT License
606 stars 41 forks source link

Catching console messages (or just errors) #77

Open anton-kulagin opened 6 years ago

anton-kulagin commented 6 years ago

Hi guys, i`m wondering is there an availability to catch console errors? For example if i put this script into my web page

window.onerror = function (error, url, line) {
     console.log('-ERR:'+error)
};

I try add chromy.on('onerror',function(){console.log('error')}) but it doesn`t catch anything. Maybe you have some ideas?

dotneet commented 6 years ago

Hi @anton-kulagin,

You can use .console to catch an error message.

chromy.goto('http://yourhost/').console(msg => console.log(msg))
anton-kulagin commented 6 years ago

Hi, @dotneet . Unfortunatelly it doesn`t work for me I have local express server with homepage which have an error (i call property of undefined variable) And the script below doesn`t catch this error

const Chromy = require('chromy')
let chromy = new Chromy({visible:true})
chromy.chain()
      .goto('http://localhost:3000/')
      .on('onerror',function(){
          console.log('error')   //didn`t work
    })
     .console((text)=>{
          console.log("----------->" + text)  //didn`t work
     })
      .evaluate(() => {
        return document.querySelectorAll('*').length   
      })
      .result((r) => console.log(r))   //work
      .end()
      .then(() => chromy.close())
dotneet commented 6 years ago

following code works?

const Chromy = require('chromy')
let chromy = new Chromy({visible:true})
chromy.chain()
      .console((text)=>{
            console.log("-->" + text)  //didn`t work
      })
      .goto('http://localhost:3000/', {waitLoadEvent: false})
      .evaluate(() => {
        window.onerror = function(msg) {
          console.error(msg)
        }
      })
      .waitLoadEvent()
      .evaluate(() => {
        return document.querySelectorAll('*').length
      })
      .result((r) => console.log(r))   //work
      .end()
      // .catch(e => console.log(e))
      .then(() => chromy.close())
anton-kulagin commented 6 years ago

@dotneet This also doesn`t work. You can install server with error just in testing purpose from here https://github.com/anton-kulagin/service-server . It will create expressjs server on localhost:3000 with index page which have error. About code above i think that we run it and immediately end because evaluate doesn`t wait for anything.

.evaluate(() => {
        window.onerror = function(msg) {
          console.error(msg)
        }
      })

And after some investigation of your library i found that you use 'chrome-remote-interface' and there was issue similar to my. (https://github.com/cyrus-and/chrome-remote-interface/issues/245) So, there answer is there. What do you think about it? I mean can i create pull request to fix console method, or create one more method?

dotneet commented 6 years ago

@anton-kulagin Thanks. I was not aware of the issue. To catch the exception, you can use .on() like this:

      .on('Runtime.exceptionThrown', (params) => {
        console.log('---->', params)
      })

PR is always welcome. If you want a more simple method, please create PR.

anton-kulagin commented 6 years ago

@dotneet Unfortunately it doesn`t work either. I have an idea why it happened example how it should work As you see listener should be inside CDP and before on load page event. So when i set listeners with on method it already fired and i can`t catch it.

dotneet commented 6 years ago

@anton-kulagin waitLoadEvent property in .goto() can be used to skip waiting load event like this:

.goto('http://localhost:3000/', {waitLoadEvent: false})
.on('Runtime.exceptionThrown', (params) => {
        console.log('---->', params)
      })