chromy icon indicating copy to clipboard operation
chromy copied to clipboard

goto() returns http response

Open NimaSoroush opened this issue 8 years ago • 5 comments

Hi,

I would be interested in seeing goto() function sending HTTP response as a promise call back which you could validate based on the response you get (e.g status code)

Any suggestion what would be the best way to achieve this using chromy?

NimaSoroush avatar Jul 12 '17 12:07 NimaSoroush

Hi,

Currently chromy has no way to achieve this.

But chrome devtools protocol has the event listener that is possibly helpful.

https://chromedevtools.github.io/devtools-protocol/tot/Network/#event-responseReceived

Here is example:

const Chromy = require('chromy')

const chromy = new Chromy({visible: true})
async function main () {
  const url = 'http://example.com/'
  await chromy.start()
  let status;
  await chromy.client.Network.responseReceived((payload) => {
    if (payload.response.url === url) {
      status = payload.response.status
    }
  })
  await chromy.goto('http://example.com/')
  console.log(status)
  await chromy.close()
}

main()

dotneet avatar Jul 12 '17 14:07 dotneet

This is an important pattern. Would be a good idea to add this to the examples.

Unfortunately -- I suspect that await is going to be problematic for many older implementations -- especially for scripting. This will impact how many developers can start using chromy now. It may be a good idea to show some basic alternative syntax options for those who can't use that syntax yet.

garris avatar Jul 12 '17 16:07 garris

@dotneet: That looks pretty good in fact. Can we wrap that functionality in goto() method and make the payload.response as the return value of goto()? Currently, goto does not return anything

NimaSoroush avatar Jul 12 '17 21:07 NimaSoroush

@garris : Chromy supports node 6 and later since v0.2.11. which basically support normal promises

NimaSoroush avatar Jul 12 '17 21:07 NimaSoroush

@NimaSoroush sounds good! returning payload.response seems to be useful.

@garris thanks for your advice. I'll be conscious of the node6 compatibility with example code.

dotneet avatar Jul 16 '17 07:07 dotneet