drashland / sinco

Browser Automation and Testing Tool for Deno, written in full TypeScript
https://drash.land/sinco
MIT License
57 stars 3 forks source link

feat: In click(), check if element is still available and give helpful error message if it isn't #140

Closed pblzd closed 4 months ago

pblzd commented 2 years ago

Summary

I'd like to suggest to check if an element is still available in the document when trying to click() it. Currently an Error is thrown about its lacking quads, which is hard to make sense of unless one dives deep into the library and its preparations for click().

    error: TypeError: quad is not iterable
        for (const point of quad) {
                            ^
        at Element.click (https://deno.land/x/sinco@v4.1.0/src/element.ts:310:25)

I stumbled upon this when writing tests for code that re-renders a list of elements, as I was using a reference to an element that got thrown out of the DOM previously to the click(). The actual problem is with the calling code, no doubt. But the error message could be more precise and helpful, I think.

I'm not sure if empty quads is an adequate standalone criterion, and am generally not fluent with the inner workings of a browser, so I'm not comfortable suggesting actual code, sorry!

Independent of the outcome of this I'd like to thank you very much for building and maintaining this library, it is very helpful and important!

ebebbington commented 1 year ago

hey @pblzd,, i dont suppose you could share the code? This seems to be an awkward issue to replicate (so i can write a test that this fails, then adding the fix fixes it)

I believe the element is still visible as previously in the code for click(), we have a scrollIntoView() view, and it doesn't seem to throw at that so that element must exist. I would need to do some more digging into "quads", to be sure how we might get into a state of our filtering on quads returning an empty array

ebebbington commented 1 year ago

Ok i've found where we can add the fix, and what the fix is, it's just a matter of replicating it.

Seems to be if the node isn't clickable or isnt a html element

pblzd commented 1 year ago

Here's some code to reproduce and verify the issue: https://gist.github.com/pblzd/f005a4ee1d0039b360862c76b6c0b12d.

Thank you for working on this!

pblzd commented 1 year ago

Also, one refinement: the error saying quad is not iterable happens if the element is present but not visible. If the element was removed from the DOM, an error saying TypeError: Cannot read properties of null (reading 'scrollIntoView') is thrown.