test webview2

This commit is contained in:
Haiping Chen 2024-12-23 19:53:04 +00:00
parent 1035170886
commit e8d182494a
4 changed files with 50 additions and 32 deletions

View file

@ -3,4 +3,6 @@ namespace BotSharp.Abstraction.Browsing.Models;
public class BrowserActionArgs
{
public bool Headless { get; set; }
public string? UserDataDir { get; set; }
public string? RemoteHostUrl { get; set; }
}

View file

@ -190,7 +190,7 @@
<ItemGroup>
<PackageReference Include="Aspects.Cache" Version="2.0.4" />
<PackageReference Include="DistributedLock.Redis" Version="1.0.3" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.7.0" />
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.7.1" />
<PackageReference Include="Fluid.Core" Version="2.11.1" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.1" />

View file

@ -45,28 +45,36 @@ public class PlaywrightInstance : IDisposable
_playwright = await Playwright.CreateAsync();
}
string tempFolderPath = $"{Path.GetTempPath()}\\playwright\\{ctxId}";
_contexts[ctxId] = await _playwright.Chromium.LaunchPersistentContextAsync(tempFolderPath, new BrowserTypeLaunchPersistentContextOptions
if (!string.IsNullOrEmpty(args.RemoteHostUrl))
{
Headless = args.Headless,
Channel = "chrome",
ViewportSize = new ViewportSize
var browser = await _playwright.Chromium.ConnectOverCDPAsync(args.RemoteHostUrl);
_contexts[ctxId] = browser.Contexts[0];
}
else
{
string userDataDir = args.UserDataDir ?? $"{Path.GetTempPath()}\\playwright\\{ctxId}";
_contexts[ctxId] = await _playwright.Chromium.LaunchPersistentContextAsync(userDataDir, new BrowserTypeLaunchPersistentContextOptions
{
Width = 1600,
Height = 900
},
IgnoreDefaultArgs =
[
"--enable-automation",
],
Args =
[
"--disable-infobars",
"--test-type"
// "--start-maximized"
]
});
Headless = args.Headless,
Channel = "chrome",
ViewportSize = new ViewportSize
{
Width = 1600,
Height = 900
},
IgnoreDefaultArgs =
[
"--enable-automation",
],
Args =
[
"--disable-infobars",
"--test-type"
// "--start-maximized"
]
});
}
_pages[ctxId] = new List<IPage>();
_contexts[ctxId].Page += async (sender, page) =>

View file

@ -8,22 +8,30 @@ public partial class PlaywrightWebDriver
var context = await _instance.GetContext(message.ContextId);
try
{
var page = await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCallback,
responseInMemory: args.ResponseInMemory,
responseContainer: args.ResponseContainer,
excludeResponseUrls: args.ExcludeResponseUrls,
includeResponseUrls: args.IncludeResponseUrls);
Serilog.Log.Information($"goto page: {args.Url}");
if (args.OpenNewTab && page != null && page.Url == "about:blank")
IPage? page = null;
if (!args.OpenNewTab && !args.EnableResponseCallback)
{
page = await _instance.NewPage(message,
enableResponseCallback: args.EnableResponseCallback,
page = _instance.Contexts[message.ContextId].Pages.LastOrDefault();
}
else
{
page = await _instance.NewPage(message, enableResponseCallback: args.EnableResponseCallback,
responseInMemory: args.ResponseInMemory,
responseContainer: args.ResponseContainer,
excludeResponseUrls: args.ExcludeResponseUrls,
includeResponseUrls: args.IncludeResponseUrls);
Serilog.Log.Information($"goto page: {args.Url}");
if (args.OpenNewTab && page != null && page.Url == "about:blank")
{
page = await _instance.NewPage(message,
enableResponseCallback: args.EnableResponseCallback,
responseInMemory: args.ResponseInMemory,
responseContainer: args.ResponseContainer,
excludeResponseUrls: args.ExcludeResponseUrls,
includeResponseUrls: args.IncludeResponseUrls);
}
}
if (page == null)