forked from microsoft/fluentui-blazor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFluentPlaywrightBaseTest.cs
More file actions
79 lines (64 loc) · 2.37 KB
/
FluentPlaywrightBaseTest.cs
File metadata and controls
79 lines (64 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// ------------------------------------------------------------------------
// This file is licensed to you under the MIT License.
// ------------------------------------------------------------------------
using Microsoft.FluentUI.AspNetCore.Components.IntegrationTests.WebServer;
using Microsoft.Playwright;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.FluentUI.AspNetCore.Components.IntegrationTests.Components;
#pragma warning disable CS0612 // Type or member is obsolete
public abstract class FluentPlaywrightBaseTest : IAsyncDisposable, IDisposable
{
private IPlaywright? _playwright;
private IBrowser? _browser;
/// <summary>
/// Constructor for the FluentPlaywrightBaseTest
/// </summary>
/// <param name="output"></param>
/// <param name="server"></param>
protected FluentPlaywrightBaseTest(ITestOutputHelper output, StartServerFixture server)
{
Trace = output;
Server = server;
}
/// <summary>
/// Output helper for logging
/// </summary>
public virtual ITestOutputHelper Trace { get; set; }
/// <summary>
/// Server fixture for starting the server
/// </summary>
protected virtual StartServerFixture Server { get; set; }
public async Task<IPage> WaitOpenPageAsync(string url, bool? openDevTools = null, bool? openHeadlessBrowser = null)
{
_playwright = await Playwright.Playwright.CreateAsync();
_browser = await _playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions()
{
Devtools = openDevTools,
Headless = openHeadlessBrowser
});
var page = await _browser.NewPageAsync();
page.Console += (_, msg) => Trace.WriteLine(msg.Text);
await page.GotoAsync($"{Server.BaseUrl}{url}");
await page.WaitForConsoleMessageAsync(new PageWaitForConsoleMessageOptions()
{
Predicate = msg => msg.Text.Contains("WebSocket connected"),
Timeout = 2000
});
await Task.Delay(100); // Wait for page to render
return page;
}
public void Dispose()
{
Task.Run(async () => await DisposeAsync());
}
public async ValueTask DisposeAsync()
{
if (_browser != null)
{
await _browser.CloseAsync();
}
_playwright?.Dispose();
}
}
#pragma warning restore CS0612 // Type or member is obsolete