playwright-dotnet icon indicating copy to clipboard operation
playwright-dotnet copied to clipboard

[Feature]: Add cancellation token support

Open markboyall opened this issue 1 year ago • 0 comments

🚀 Feature Request

Add CancellationToken options to page interactions, like ClickAsync.

Example

Create a CancellationTokenSource, try several actions, and cancel whichever of them is no longer needed.

           var cts = new CancellationTokenSource();
            await RunAndWaitForResponseAsync(async () =>
            {
                await page.RunAndWaitForRequestAsync(() => fileChooser.SetFilesAsync(fileName), "**/WebForms/ImportTemplate.aspx");
                FrameContents.Locator("div.popup").Locator("button:text(\"yes\")").ClickAsync(cts.Token);
            }, "**/Request.aspx");
            cts.Cancel();

Motivation

We have some actions in our application which can result in a few different responses. The Or locator seems to be intended to handle these, but not every possible response can be expressed as a locator. In this example, either a network request is sent, or, a popup is displayed which the user has to click on, then the network request is sent. There doesn't seem to be a great way to handle these actions at the moment.

The only obvious solution is to try to click on the popup button and ignore the timeout exception, but this makes the test pretty slow. Currently we are simply fire-and-forgetting the click on the popup whilst unconditionally waiting on the network request, but this could trigger later on if the popup shows again for other reasons, when it should be cancelled immediately if we observe the request. Maybe there should be some dedicated feature for various responses, but it seems more flexible to simply allow the user to cancel attempted interactions.

It may be useful in other situations where the test writer wants one of several different actions to succeed but these are not simple "one interaction on one of these locators" actions. We have a kinda similar situation where an expandable element may or may not be expanded, so we want to wait for the expanded content, or, click the button to expand. The Or locator can't quite accommodate this since we're looking for two different interactions on the different options.

markboyall avatar May 10 '24 14:05 markboyall