Replies: 3 comments 6 replies
-
In the context of the Mouse API, it may be productive when doing things other than await sendMouse({
move: [9, 30]
});
sendMouse({
down: [],
}); Or send an array of instructions to be taken sequentially: await sendMouse([
{move:[9,30]},
{down:[]},
]); The idea of batching the commands shifts me a little more towards the alternative command structure in that case: sendMouse([
{
type: 'move' ,
position: [9, 39];
},
{
type: 'down',
}
]); Luckily, while doing this, the underlying |
Beta Was this translation helpful? Give feedback.
-
Following discussion on slack, I thought I'd add my thoughts here for posterity... I think it would be useful to have a command for clicking on an element (as well as specific coordinate), since a programatic click does not always match behaviour of an actual mouse interaction. One such example is |
Beta Was this translation helpful? Give feedback.
-
The idea to add a command for triggering the |
Beta Was this translation helpful? Give feedback.
-
Building from https://modern-web.dev/docs/test-runner/commands/#send-keys, I'd like to open a conversation about supporting some sort of interface on the
Mouse
orElementHandle.click()
APIs to empower browser originating mouse events. WhilesendKeys
or the new proposeda11ySnapshot
are both without direct corollary in the JS context, mouse events can be pretty successfully synthesized in JS, so this might not be needed. However, let's discuss what it might or might not open up as testing possibilities.In JS Clicks
First, let's think about what control we have today in JS, both the
el.click()
method (which is actually a good context to leverage while testing as it allow for you element to perform in test similarly to how it would perform via imperative interactions in to a consumers element or application), and the various events that would otherwise be dispatched in this workflow. That is to say, we already have full control over the entire process of a click with:Negatives
The main difficulty that would arise from this would have to do with remembering to include all of the browser native options of dispatched events. Note that none of the above events use
bubbles
,composed
, orcancellable
all of which may in some way be needed to ensure the functionality in response to this interaction. We can certainly add these in the JS space, but it points to a complexity that we must keep in mind while doing so.Positives
The main benefit of this is that you have fine control over the event cycle. In this context, having access to each of the
click
"phases" means that you can test for specific response in each phase, which can help to ensure you code is performing as needed.Mouse
Here the Playwright and Puppeteer APIs are roughly the same and offer the following:
Which is a wrapper for:
An important difference here is that this workflow is not bound to an element. This means that we can test, without the knowledge that something exists, what happens when we click in a specific part of a page. Testing an interaction in this way leans as close to an Integration/e2e test as possible in the "unit" space in that it doesn't not make any assumptions about the unit under test and could be a valuable addition to a @web/test-runner tool belt.
It would seem that the above is both the negative and the positive of leveraging this API. The fact that it's not directly the same as
el.dispatchEvent(new Event('click'))
could point to it being the best addition to the tools of the two options the runner afford us here.For a usage API point of view, a
sendKeys
relative API might look like:I'm still working on my TS, but having the
options
locations like this separate should allow for more strictly typing the various shapes of that argument:However, I could see benefits around validation simplicity of something like:
ElementHandle.click()
This API is fairly similar to the
el.click()
options that we have native to the JS context. However, it does comes with some nice ergonomics around things like:The first three are things we have some access to in the JS context, but include direct mitigation to ensure, while the fourth has some interested possibilities within it. While we'd not want to actually be changing the URL in a unit context, we may want to mock data request, or file download, and having the test runner manage the wait for those things to resolve could be a neat side effect of leveraging this API.
Not being able to pass a ElementHandle over the commands interface (to the best of my understanding), we would need to take element resolution into account in the API for a command built around this. Much like the
a11ySnapshotPlugin
, we'd likely solve this with aselector
argument (that fallback to the entire page?) to ensure that we have the right information available when taking this action. So it might surface like:One specific feature of the options bag for this API is that is accepts
modifiers
, which allow us to make the click with<Array<"Alt"|"Control"|"Meta"|"Shift">>
modifiers pressed when enacting the click. I'd assume we could pair thesendKeys
andsendMouse
APIs to do the same there, but I've not checked that out yet.Negatives
clickElement
is much more abstract than having access to the entire series of events that make up a click event. Needing to have an element in question for testing brings this API much closer to the capabilities that we already have access to in the JS context. More research is needed to confirm whether falling back to the entire page with this API would seek to the lowest available element in the position sent before dispatching the event of not.Positives
The pairs abilities like waiting for availability, clicking a visible part of the element, applying modifier keys, and waiting for subsequent network requests in a way that could be more ergonomic than the other options. The simplicity of a
selector
and anoptions
bag leaves users with less API surface to keep in mind.Questions/Research
Do any of these interfaces surfaceCONFIRMED, yes.pointer events
? If we were getting this paired with mouse events by default by way of interacting with the browser runner then I'd be much more clearly sold on the benefits of leveraging one or more of the APIs?Beta Was this translation helpful? Give feedback.
All reactions