-
Notifications
You must be signed in to change notification settings - Fork 6
/
MonkeyTests.cs
123 lines (108 loc) · 6.04 KB
/
MonkeyTests.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
using Atata;
using Lombiq.Tests.UI.Extensions;
using Lombiq.Tests.UI.MonkeyTesting;
using Lombiq.Tests.UI.MonkeyTesting.UrlFilters;
using Lombiq.Tests.UI.Services;
using OpenQA.Selenium;
using Shouldly;
using System;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using LogLevel = OpenQA.Selenium.LogLevel;
namespace Lombiq.Tests.UI.Samples.Tests;
// It's possible to execute monkey tests that walk through site pages and do random interactions with pages, like click,
// scrolling, form filling, etc. Such random actions can uncover bugs that are otherwise difficult to find. Use such
// tests plug holes in your test suite which are not covered by explicit tests.
public class MonkeyTests : UITestBase
{
public MonkeyTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}
// The basic idea is that you unleash monkey testing on specific pages or sections of the site, like a contact form
// or the content management UI. First, we test a single page.
[Fact]
public Task TestCurrentPageAsMonkeyShouldWorkWithConfiguredRandomSeed() =>
ExecuteTestAfterSetupAsync(
async context =>
{
// Note how we define the starting point of the test as the homepage.
await context.GoToHomePageAsync();
// The specified random see gives you the option to reproduce the random interactions. Otherwise it
// would be calculated from MonkeyTestingOptions.BaseRandomSeed.
await context.TestCurrentPageAsMonkeyAsync(CreateMonkeyTestingOptions(), 12345);
});
// Recursive testing will just continue testing following the configured rules until it runs out of time or new
// pages.
[Fact]
public Task TestCurrentPageAsMonkeyRecursivelyShouldWorkWithAnonymousUser() =>
ExecuteTestAfterSetupAsync(
async context =>
{
await context.GoToHomePageAsync();
await context.TestCurrentPageAsMonkeyRecursivelyAsync(CreateMonkeyTestingOptions());
// The shortcut context.TestFrontendAuthenticatedAsMonkeyRecursivelyAsync(_monkeyTestingOptions) does
// the same thing but we wanted to demonstrate the contrast with
// TestCurrentPageAsMonkeyShouldWorkWithConfiguredRandomSeed().
});
// Let's test with an authenticated user too.
[Fact]
public Task TestAdminPagesAsMonkeyRecursivelyShouldWorkWithAdminUser() =>
ExecuteTestAfterSetupAsync(
context =>
{
// Monkey tests needn't all start from the homepage. This one starts from the Orchard admin dashboard.
var monkeyTestingOptions = CreateMonkeyTestingOptions();
// So we don't take too much time testing the whole Orchard admin, this sample restricts requests to
// "/Admin". But this is just this sample, you can unleash monkeys on the whole admin too!
monkeyTestingOptions.UrlFilters.Add(new MatchesRegexMonkeyTestingUrlFilter("/Admin$"));
return context.TestAdminAsMonkeyRecursivelyAsync(monkeyTestingOptions);
},
configuration =>
configuration.AssertBrowserLog = logEntries => logEntries.ShouldNotContain(
logEntry => IsValidAdminBrowserLogEntry(logEntry),
logEntries.Where(IsValidAdminBrowserLogEntry).ToFormattedString()));
// Let's just test the background tasks management admin area.
[Fact]
public Task TestAdminBackgroundTasksAsMonkeyRecursivelyShouldWorkWithAdminUser() =>
ExecuteTestAfterSetupAsync(
async context =>
{
var monkeyTestingOptions = CreateMonkeyTestingOptions();
// You can fence monkey testing with URL filters: Monkey testing will only be executed if the current
// URL matches. This way, you can restrict monkey testing to just sections of the site. You can also use
// such fencing to have multiple monkey testing methods in multiple test classes, thus running them in
// parallel.
monkeyTestingOptions.UrlFilters.Add(new StartsWithMonkeyTestingUrlFilter("/Admin/BackgroundTasks"));
// You could also configure the same thing with regex:
////_monkeyTestingOptions.UrlFilters.Add(new MatchesRegexMonkeyTestingUrlFilter(@"\/Admin\/BackgroundTasks"));
await context.SignInDirectlyAndGoToAdminRelativeUrlAsync("/BackgroundTasks");
await context.TestCurrentPageAsMonkeyRecursivelyAsync(monkeyTestingOptions);
},
configuration => configuration.AssertBrowserLog = (logEntries) => logEntries
.Where(logEntry =>
!logEntry
.Message
.Contains("An invalid form control with name='LockTimeout' is not focusable.")
&& !logEntry
.Message
.Contains("An invalid form control with name='LockExpiration' is not focusable.")
&& !logEntry.IsNotFoundLogEntry("/favicon.ico")
&& logEntry.Level != LogLevel.Info)
.ShouldBeEmpty());
// Monkey testing has its own configuration too. Check out the docs of the options too.
private static MonkeyTestingOptions CreateMonkeyTestingOptions() =>
new()
{
PageTestTime = TimeSpan.FromSeconds(5),
};
private static bool IsValidAdminBrowserLogEntry(LogEntry logEntry) =>
OrchardCoreUITestExecutorConfiguration.IsValidBrowserLogEntry(logEntry) &&
// Requests to /api/graphql without further parameters will fail with HTTP 400, but that's OK, since some
// parameters are required.
!logEntry.Message.ContainsOrdinalIgnoreCase("/api/graphql - Failed to load resource: the server responded with a status of 400");
}
// END OF TRAINING SECTION: Monkey tests.
// NEXT STATION: Head over to Tests/DatabaseSnapshotTests.cs.