forked from Lombiq/UI-Testing-Toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MonkeyTests.cs
105 lines (93 loc) · 5.18 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
using Lombiq.Tests.UI.Attributes;
using Lombiq.Tests.UI.Extensions;
using Lombiq.Tests.UI.MonkeyTesting;
using Lombiq.Tests.UI.MonkeyTesting.UrlFilters;
using Lombiq.Tests.UI.Services;
using Shouldly;
using System;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
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.
[Theory, Chrome]
public Task TestCurrentPageAsMonkeyShouldWorkWithConfiguredRandomSeed(Browser browser) =>
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);
},
browser);
// Recursive testing will just continue testing following the configured rules until it runs out of time or new
// pages.
[Theory, Chrome]
public Task TestCurrentPageAsMonkeyRecursivelyShouldWorkWithAnonymousUser(Browser browser) =>
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().
},
browser);
// Let's test with an authenticated user too.
[Theory, Chrome]
public Task TestAdminPagesAsMonkeyRecursivelyShouldWorkWithAdminUser(Browser browser) =>
ExecuteTestAfterSetupAsync(
context =>
// Monkey tests needn't all start from the homepage. This one starts from the Orchard admin dashboard.
context.TestAdminAsMonkeyRecursivelyAsync(CreateMonkeyTestingOptions()),
browser,
configuration =>
// This is necessary to work around this bug: https://github.com/OrchardCMS/OrchardCore/issues/11420.
configuration.AssertBrowserLog = messages => messages.ShouldNotContain(
message => IsValidAdminBrowserLogMessage(message),
messages.Where(IsValidAdminBrowserLogMessage).ToFormattedString()));
// Let's just test the background tasks management admin area.
[Theory, Chrome]
public Task TestAdminBackgroundTasksAsMonkeyRecursivelyShouldWorkWithAdminUser(Browser browser) =>
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.SignInDirectlyAndGoToRelativeUrlAsync("/Admin/BackgroundTasks");
await context.TestCurrentPageAsMonkeyRecursivelyAsync(monkeyTestingOptions);
},
browser);
// Monkey testing has its own configuration too. Check out the docs of the options too.
private static MonkeyTestingOptions CreateMonkeyTestingOptions() =>
new()
{
PageTestTime = TimeSpan.FromSeconds(10),
};
private static bool IsValidAdminBrowserLogMessage(BrowserLogMessage message) =>
OrchardCoreUITestExecutorConfiguration.IsValidBrowserLogMessage(message) &&
!(message.Source == BrowserLogMessage.Sources.Intervention &&
message.Message.ContainsOrdinalIgnoreCase(
"Blocked attempt to show a 'beforeunload' confirmation panel for a frame that never had a user gesture since its load."));
}
// END OF TRAINING SECTION: Monkey tests.