-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathErrorHandlingTests.cs
More file actions
146 lines (125 loc) · 6.26 KB
/
ErrorHandlingTests.cs
File metadata and controls
146 lines (125 loc) · 6.26 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
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
141
142
143
144
145
146
using Lombiq.Tests.UI.Exceptions;
using Lombiq.Tests.UI.Extensions;
using Lombiq.Tests.UI.Models;
using Lombiq.Tests.UI.Samples.Helpers;
using Lombiq.Tests.UI.Services;
using OpenQA.Selenium.BiDi.Log;
using Shouldly;
using System;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
namespace Lombiq.Tests.UI.Samples.Tests;
// Sometimes errors are expected. Let's check out what can be done with them!
public class ErrorHandlingTests : UITestBase
{
public ErrorHandlingTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}
// It's easier to diagnose a test failure if you know whether an element is missing because something is actually
// missing or because there was a server-side error. The below test visits a page where the action method throws an
// exception.
[Fact]
public Task ServerSideErrorOnLoadedPageShouldHaltTest() =>
ExecuteTestAfterSetupAsync(
async context =>
{
try
{
await context.GoToErrorPageDirectlyAsync();
// This point should be unreachable because Orchard logs are automatically asserted after a page
// load.
throw new InvalidOperationException("The log assertion didn't happen after page load!");
}
catch (PageChangeAssertionException)
{
// Remove all logs to have a clean slate.
await context.ClearLogsAsync(context.Configuration.TestCancellationToken);
}
});
// You can interact with the browser log and its history as well. E.g. 404s and JS exceptions show up in the browser
// log.
[Fact]
public Task ClientSideErrorOnLoadedPageShouldHaltTest() =>
ExecuteTestAfterSetupAsync(
async context =>
{
try
{
await context.GoToRelativeUrlAsync("/this-does-not-exist");
// This point should be unreachable because browser logs are automatically asserted after a page
// load.
throw new InvalidOperationException("The log assertion didn't happen after page load!");
}
catch (PageChangeAssertionException)
{
// Remove response logs to have a clean slate.
context.ClearCumulativeResponseLog();
}
});
// To be able to trust the test above, we have to be sure that the browser logs survive the navigation events and
// all get collected into the historic browser log.
[Fact]
public Task BrowserLogsShouldPersist() =>
ExecuteTestAfterSetupAsync(
async context =>
{
const string testLog = "--test log--";
void WriteConsoleLog() => context.ExecuteScript($"console.info('{testLog}');");
await context.SignInDirectlyAndGoToHomepageAsync();
WriteConsoleLog();
WriteConsoleLog();
await context.GoToDashboardAsync();
WriteConsoleLog();
await context.GoToHomePageAsync();
WriteConsoleLog();
WriteConsoleLog();
WriteConsoleLog();
// Since the browser log is updated asynchronously, we have to wait for most recent entries to appear.
context.DoWithRetriesOrFail(() =>
context
.CumulativeBrowserLog
.Count(entry => entry.Text.Contains(testLog)) == 6);
},
configuration =>
{
// By default, anything below warning is not logged to the browser log. So, to allow the info messages
// of the test, we change the filter.
configuration.BrowserLogFilter = logEntry =>
OrchardCoreUITestExecutorConfiguration.IsNonSuccessBrowserLogEntry(logEntry) || logEntry.Level == Level.Info;
// By default, the test will fail if the browser log is not empty. We allow info entries here.
configuration.AssertBrowserLog = logEntries => logEntries.ShouldNotContain(entry => entry.Level > Level.Info);
});
[Fact]
public Task ErrorDuringSetupShouldHaltTest() =>
Should.ThrowAsync<PageChangeAssertionException>(() =>
ExecuteTestAfterSetupAsync(
_ => throw new InvalidOperationException("This point shouldn't be reachable because setup fails."),
configuration =>
{
// The test is guaranteed to fail so we don't want to retry it needlessly.
configuration.MaxRetryCount = 0;
// Otherwise, a GitHub Actions error annotation would appear in the workflow run summary, indicating
// a problem, despite them being expected.
configuration.GitHubActionsOutputConfiguration.EnableErrorAnnotations = false;
// We introduce a custom setup operation that has an intentionally invalid SQL Server configuration.
configuration.SetupConfiguration.SetupOperation = async context =>
{
await context.GoToSetupAndSetupOrchardCoreAsync(
new OrchardCoreSetupParameters(context)
{
SiteName = "Setup Error Test",
RecipeId = SetupHelpers.RecipeId,
DatabaseProvider = OrchardCoreSetupParameters.DatabaseType.SqlConnection,
ConnectionString = "An invalid connection string which causes an error during setup.",
});
throw new InvalidOperationException(
"This point shouldn't be reachable if the logs are properly kept.");
};
// No need to create a failure dump folder for this test, since it'll always fail.
configuration.TestDumpConfiguration.CreateTestDump = false;
}));
}
// END OF TRAINING SECTION: Error handling.
// NEXT STATION: Head over to Tests/MonkeyTests.cs.