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

[Feature] Add synchronous API

Open S-Kulyk opened this issue 1 year ago • 4 comments

The reasoning was pretty much covered in the following discussion thread: https://github.com/microsoft/playwright/discussions/26890 Async API doesn't bring many benefits in an automated testing context, but forces to add an additional syntax and prevents writing fluent PO classes For example, with async API we can't have a fluent chain like page1.ClickButton1().FillForm2().ValidateField3() Instead, it'll be

await page1.CilikButton1();
await page1.FillForm2();
await page1.ValidateField3();

S-Kulyk avatar Oct 03 '23 06:10 S-Kulyk

Not ideal workaround (but I didn't come up with better solution) for chaining is to write Task extension, something like:

public static async Task<TOut> Then<TIn, TOut>(this Task<TIn> inputTask, Func<TIn, Task<TOut>> followedByFunc)
{
    var inputResult = await inputTask;
    return await followedByFunc(inputResult);
}

and then, in your POM class methods, you need to return Task<Page> so you can call something like this in test:

await page1.ClickButton1().Then(page => page.FillForm2()).Then(page => page.ValidateField3());

wolvi19 avatar Oct 11 '23 14:10 wolvi19

@wolvi19 Nice solution, thanks! Nevertheless still believe that async API is redundant in 99.99% percent of UI automation cases and Playwright users would benefit from the sync option

S-Kulyk avatar Nov 06 '23 10:11 S-Kulyk

Agreed. This is one of the reasons I'm hesitant to move from Selenium/C# and try Playwright/C#. I don't consider the API to be as well designed and the async-only is one of the major things. I have no use of async-methods, it just forces me to add a lot of "ugly" and irrelevant syntax/boiler-plate code to keep the test code synchronous - which it of course must be in 99% of the time.

jensakejohansson avatar Dec 22 '23 08:12 jensakejohansson

Same in our case. Async API does not play well with older sync codebases.. Much boilerplate code & tweaking is needed. Also related question from a long time ago #1755

ddweber avatar Jan 30 '24 09:01 ddweber