joelgriffith / navalia

A bullet-proof, fast, and reliable headless browser API
https://joelgriffith.github.io/navalia/
GNU General Public License v3.0
957 stars 33 forks source link

New chainable interface #35

Closed joelgriffith closed 7 years ago

joelgriffith commented 7 years ago

This makes it so a workflow like this:

chrome.goto('http://www...')
.then(() => chrome.type('input', 'foo'))
.then(() => chrome.type('input', 'bar'))
.then(() => chrome.click('.buy-now'))

Becomes

chrome
  .goto('http://www...')
  .type('input', 'foo')
  .type('input', 'bar')
  .click('.buy-now')

This new interface is still bw compatible with the old, however I had to hand-roll my own then which executes the actions. Internally this just queues actions until the then member is called. The ONLY gotcha is that anything that does new Chrome can't return it inside a promise-chain, otherwise the then method will be called and return undefined.

Thanks @mute for the tests, they helped tremendously here

joelgriffith commented 7 years ago

Forgot to add that this new interface also "retries" since a queue is being used. This allows us to do some cool things like not need pageload rarely (since a pageload will trigger an action failure, and it'll re-run)