I guess this is a classic C# to F# conversion I haven't quite got my head around.
I am trying to automate a browser using the quick start
https://playwright.dev/dotnet/docs/intro
The C# code is
using var playwright = await Playwright.CreateAsync();
await using var browser = await playwright.Chromium.LaunchAsync();
var page = await browser.NewPageAsync();
await page.GotoAsync("http://www.bing.com");
await page.ScreenshotAsync(path: outputFile);
I've made a start but getting a bit lost already.
let playwright: Playwright = PlaywrightSharp.Playwright.CreateAsync() |> Async.AwaitTask
Incorrect type. What am I doing wrong here?
Error FS0001 This expression was expected to have type
'Playwright'
but here has type
'Async<IPlaywright>'
One way to do this is with a F#'s built-in support for async computation expressions. The translation would look something like this:
There are a few subtleties here that you'll need to know about:
Async<'T>
. I've usedAsync.AwaitTask
to convert from C#-style tasks, and defined a prefix operator,~~
, to make this look a bit cleaner.DisposeAsync
inasync
computation expressions yet, so the browser doesn't get disposed of properly. If you want, you can adddo! browser.DisposeAsync().AsTask() |> Async.AwaitTask
at the end of the block to do this manually.Async.Ignore
.