browser-monkey icon indicating copy to clipboard operation
browser-monkey copied to clipboard

Should browser-monkey really do all those assertions?

Open joshski opened this issue 8 years ago • 5 comments

Browser Monkey is a DOM assertion library

Is it? ....really?

I think it's core strength is actually finding and manipulating elements in a DOM. To back up this statement, consider the fact that the first example in the readme does no assertions!

I feel like maybe assertions should be left to the user. So perhaps I would rather express this:

await browser.find('ul.monkey-species li').shouldHave({ text: [
  'Olive Baboon',
  'Patas Monkey']
})

...as this:

assert.deepEqual(await browser.find('ul.monkey-species li').text(), [
  'Olive Baboon',
  'Patas Monkey']
)

As a result:

  • I can use whatever assertion library I want
  • My tests throw assertion exceptions, instead of some framework
  • The browser-monkey API has a smaller surface area, so it's easier to learn and maintain

What do you think?

joshski avatar Jun 15 '17 12:06 joshski

sounds like a good experimental API that could become v3 :+1:

dereke avatar Jun 15 '17 14:06 dereke

major problem here is that browser monkey retries assertions until they pass (or until timeout) - if we can get this to work with custom assertions then i'm in favour ;)

refractalize avatar Jun 15 '17 14:06 refractalize

Yeah, good point! Maybe there's not much difference when you factor that stuff in. The way this works in capybara is that the driver answers a query, which eventually returns false, so:

async browser.exists(options) // => returns false

...it's then up to the assertion framework to assert it exists or describe the failure, e.g:

assert(await browser.exists(options), `Expected element to exist with ${options}`)

joshski avatar Jun 15 '17 14:06 joshski

FWIW I think a wise strategy when waiting for something to exist, is wait for the smallest piece of evidence that it exists. In the example above, I think you generally want to be waiting for ul.monkey-species to exist, then synchronously asserting on the li items inside it. There's a subtle difference between that and waiting for a more complex thing (such as the list with the items in it) but it's an important difference in my mind. Mixing the assertions with the finding makes it difficult to know exactly what the test is doing IMO.

joshski avatar Jun 15 '17 14:06 joshski

Also, you know.... perhaps the retrying isn't really browser-monkey's responsibility either 😲

assert.eventuallyDeepEqual(browser.find('ul.monkey-species li').text(), [
  'Olive Baboon',
  'Patas Monkey']
)

joshski avatar Jun 15 '17 14:06 joshski