Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add synchronous API #2715

Open
Melkor972 opened this issue Oct 3, 2023 · 4 comments
Open

[Feature] Add synchronous API #2715

Melkor972 opened this issue Oct 3, 2023 · 4 comments

Comments

@Melkor972
Copy link

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();
@wolvi19
Copy link

wolvi19 commented Oct 11, 2023

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 so you can call something like this in test:

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

@Melkor972
Copy link
Author

@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

@jensakejohansson
Copy link

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.

@ddweber
Copy link

ddweber commented Jan 30, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants