featurist / browser-monkey

Reliable DOM testing
https://browsermonkey.org
53 stars 6 forks source link

Should browser-monkey really do all those assertions? #74

Open joshski opened 7 years ago

joshski commented 7 years ago

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:

What do you think?

dereke commented 7 years ago

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

refractalize commented 7 years ago

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 ;)

joshski commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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']
)