-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
UITests.cs
140 lines (106 loc) · 3.98 KB
/
UITests.cs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) Martin Costello, 2021. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
using Microsoft.Playwright;
namespace TodoApp;
[Collection(HttpServerCollection.Name)]
public class UITests : IAsyncLifetime
{
public UITests(HttpServerFixture fixture, ITestOutputHelper outputHelper)
{
Fixture = fixture;
// Route output from the fixture's logs to xunit's output
OutputHelper = outputHelper;
Fixture.SetOutputHelper(OutputHelper);
}
private HttpServerFixture Fixture { get; }
private ITestOutputHelper OutputHelper { get; }
public static TheoryData<string, string?> Browsers()
{
var browsers = new TheoryData<string, string?>()
{
{ BrowserType.Chromium, null },
{ BrowserType.Chromium, "chrome" },
{ BrowserType.Firefox, null },
};
// HACK Skip on macOS. See https://github.com/microsoft/playwright-dotnet/issues/2920.
if (!OperatingSystem.IsLinux() && !OperatingSystem.IsMacOS())
{
browsers.Add(BrowserType.Chromium, "msedge");
}
if (OperatingSystem.IsMacOS())
{
browsers.Add(BrowserType.Webkit, null);
}
return browsers;
}
[Theory]
[MemberData(nameof(Browsers))]
public async Task Can_Sign_In_And_Manage_Todo_Items(string browserType, string? browserChannel)
{
// Arrange
var options = new BrowserFixtureOptions
{
BrowserType = browserType,
BrowserChannel = browserChannel
};
var browser = new BrowserFixture(options, OutputHelper);
await browser.WithPageAsync(async page =>
{
// Load the application
await page.GotoAsync(Fixture.ServerAddress);
await page.WaitForLoadStateAsync(LoadState.DOMContentLoaded);
var app = new TodoPage(page);
// Act - Sign in
await app.SignInAsync();
// Assert
await app.WaitForSignedInAsync();
await app.UserNameAsync().ShouldBe("John Smith");
// Arrange - Wait for list to be ready
await app.WaitForNoItemsAsync();
// Act - Add an item
await app.AddItemAsync("Buy cheese");
// Assert
var items = await app.GetItemsAsync();
items.Count.ShouldBe(1);
await items[0].TextAsync().ShouldBe("Buy cheese");
await items[0].LastUpdatedAsync().ShouldBe("a few seconds ago");
// Act - Add another item
await app.AddItemAsync("Buy eggs");
// Assert
items = await app.GetItemsAsync();
items.Count.ShouldBe(2);
await items[0].TextAsync().ShouldBe("Buy cheese");
await items[1].TextAsync().ShouldBe("Buy eggs");
// Act - Delete an item and complete an item
await items[0].DeleteAsync();
await items[1].CompleteAsync();
await Task.Delay(TimeSpan.FromSeconds(0.5));
// Assert
items = await app.GetItemsAsync();
items.Count.ShouldBe(1);
await items[0].TextAsync().ShouldBe("Buy eggs");
// Act - Delete the remaining item
await items[0].DeleteAsync();
// Assert
await app.WaitForNoItemsAsync();
// Act - Sign out
await app.SignOutAsync();
// Assert
await app.WaitForSignedOutAsync();
});
}
public Task InitializeAsync()
{
InstallPlaywright();
return Task.CompletedTask;
}
public Task DisposeAsync() => Task.CompletedTask;
private static void InstallPlaywright()
{
int exitCode = Microsoft.Playwright.Program.Main(["install"]);
if (exitCode != 0)
{
throw new InvalidOperationException($"Playwright exited with code {exitCode}");
}
}
}