PlayWright 代码 - 将异步 C# 代码转换为 F#

PlayWright Code - Convert Async C# Code to F#

我在将一些 Microsoft 编剧代码从 C# 转换为 F# 时遇到了一些问题

特别是这段代码: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"
});

我的 F# 代码有点分离并且特定于我的要求 - 但这是到目前为止的尝试(不起作用)

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

这里有一些问题需要解决:

  • 已将 PageWaitForNavigationOptions 更改为 PageRunAndWaitForNavigationOptions
  • 将第一个方法参数更改为返回任务的函数,而不仅仅是任务。
  • 忽略最后的异步结果,这样do!就可以了