Martin Thompson
Martin Thompson

Reputation: 3755

PlayWright Code - Convert Async C# Code to F#

I'm having a bit of an issue converting some Microsoft playwright code from C# to F#

Specifically this code: https://playwright.dev/dotnet/docs/navigations#multiple-navigations

// Running action in the callback of waitForNavigation prevents a race
// condition between clicking and waiting for a navigation.
await page.RunAndWaitForNavigationAsync(async () =>
{
    // Triggers a navigation with a script redirect.
    await page.ClickAsync("a");
}, new PageWaitForNavigationOptions
{
    UrlString = "**/login"
});

My F# code is a little separated and specific to my requirements - but here is the attempt so far ( which doesn't work )

        let waitNavOptions = new PageWaitForNavigationOptions(UrlRegex=Regex("dashboard|login",RegexOptions.IgnoreCase))
        do! Async.AwaitTask(page.Value.RunAndWaitForNavigationAsync(page.Value.ClickAsync("#xl-form-submit"),waitNavOptions))

Upvotes: 0

Views: 246

Answers (1)

TheQuickBrownFox
TheQuickBrownFox

Reputation: 10624

let waitNavOptions = PageRunAndWaitForNavigationOptions(UrlRegex=Regex("dashboard|login",RegexOptions.IgnoreCase))
do! 
    page.Value.RunAndWaitForNavigationAsync(
        (fun () -> page.Value.ClickAsync("#xl-form-submit")),
        waitNavOptions)
    |> Async.AwaitTask
    |> Async.Ignore

There were a few things to fix here:

  • Changed PageWaitForNavigationOptions to PageRunAndWaitForNavigationOptions
  • Change the first method argument to a function returning a task, instead of just a task.
  • Ignore the Async result at the end so that do! is allowed

Upvotes: 2

Related Questions