Skip to content

Commit

Permalink
Merge pull request #186 from Lombiq/issue/OSOE-46
Browse files Browse the repository at this point in the history
OSOE-46: Use WebApplicationFactory directly instead of running the tested app with the dotnet CLI
  • Loading branch information
sarahelsaig authored Oct 14, 2022
2 parents f183777 + dc810d2 commit a874a57
Show file tree
Hide file tree
Showing 45 changed files with 1,148 additions and 630 deletions.
3 changes: 2 additions & 1 deletion Lombiq.Tests.UI.Samples/Lombiq.Tests.UI.Samples.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
Expand Down Expand Up @@ -43,6 +43,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Lombiq.OSOCE.Web\Lombiq.OSOCE.Web.csproj" />
<ProjectReference Include="..\Lombiq.Tests.UI\Lombiq.Tests.UI.csproj" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion Lombiq.Tests.UI.Samples/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ For general details about and on using the Toolbox see the [root Readme](../Read
## Tutorial

1. Be sure to read the [root Readme](../Readme.md) first to understand how the UI Testing Toolbox works and how you can use it with an Orchard Core app.
2. The web app under test also needs a bit of configuration. For an example of this, check out [the `Program` class of Lombiq's Open-Source Orchard Core Extensions](https://github.com/Lombiq/Open-Source-Orchard-Core-Extensions/blob/dev/src/Lombiq.OSOCE.Web/Program.cs). This project assumes it's running in that solution.
2. The web app under test also needs a bit of configuration. For an example of this, check out [the `Program` class of Lombiq's Open-Source Orchard Core Extensions](https://github.com/Lombiq/Open-Source-Orchard-Core-Extensions/blob/dev/src/Lombiq.OSOCE.Web/Program.cs). This project assumes it's running in that solution, so it depends on `Lombiq.OSOCE.Web` to be present at the correct path to be able to build it.
3. Note that the project includes an _xunit.runner.json_ file. This is [xUnit's configuration file](https://xunit.net/docs/configuration-files). You don't necessarily need to include one if you're OK with [the default one](https://github.com/Lombiq/UI-Testing-Toolbox/blob/dev/Lombiq.Tests.UI/xunit.runner.json).
4. Now that we have the basics out of the way start the tutorial in the [_GlobalSuppressions.cs_](GlobalSuppressions.cs) file.

Expand Down
5 changes: 2 additions & 3 deletions Lombiq.Tests.UI.Samples/Tests/ErrorHandlingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Lombiq.Tests.UI.Pages;
using Lombiq.Tests.UI.Samples.Helpers;
using Lombiq.Tests.UI.Services;
using Microsoft.SqlServer.Management.Dmf;
using Shouldly;
using System;
using System.Linq;
Expand Down Expand Up @@ -108,7 +107,7 @@ public Task BrowserLogsShouldPersist(Browser browser) =>
public Task ErrorDuringSetupShouldHaltTest(Browser browser) =>
Should.ThrowAsync<PageChangeAssertionException>(() =>
ExecuteTestAfterSetupAsync(
_ => throw new InvalidOperandException("This point shouldn't be reachable because setup fails."),
_ => throw new InvalidOperationException("This point shouldn't be reachable because setup fails."),
browser,
configuration =>
{
Expand All @@ -127,7 +126,7 @@ await context.GoToSetupPageAndSetupOrchardCoreAsync(
ConnectionString = "An invalid connection string which causes an error during setup.",
});

throw new InvalidOperandException(
throw new InvalidOperationException(
"This point shouldn't be reachable if the logs are properly kept.");
};
}));
Expand Down
14 changes: 13 additions & 1 deletion Lombiq.Tests.UI.Samples/Tests/MonkeyTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Atata;
using Lombiq.Tests.UI.Attributes;
using Lombiq.Tests.UI.Extensions;
using Lombiq.Tests.UI.MonkeyTesting;
Expand All @@ -10,6 +11,7 @@
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using LogLevel = OpenQA.Selenium.LogLevel;

namespace Lombiq.Tests.UI.Samples.Tests;

Expand Down Expand Up @@ -87,7 +89,17 @@ public Task TestAdminBackgroundTasksAsMonkeyRecursivelyShouldWorkWithAdminUser(B
await context.SignInDirectlyAndGoToRelativeUrlAsync("/Admin/BackgroundTasks");
await context.TestCurrentPageAsMonkeyRecursivelyAsync(monkeyTestingOptions);
},
browser);
browser,
configuration => configuration.AssertBrowserLog = (logs) => logs
.Where(message =>
!message
.Message
.Contains("An invalid form control with name='LockTimeout' is not focusable.")
&& !message
.Message
.Contains("An invalid form control with name='LockExpiration' is not focusable.")
&& message.Level != LogLevel.Info)
.ShouldBeEmpty());

// Monkey testing has its own configuration too. Check out the docs of the options too.
private static MonkeyTestingOptions CreateMonkeyTestingOptions() =>
Expand Down
11 changes: 2 additions & 9 deletions Lombiq.Tests.UI.Samples/UITestBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Lombiq.Tests.UI.Constants;
using Lombiq.Tests.UI.Extensions;
using Lombiq.Tests.UI.Helpers;
using Lombiq.Tests.UI.Samples.Helpers;
using Lombiq.Tests.UI.Services;
using Shouldly;
Expand All @@ -12,13 +11,8 @@ namespace Lombiq.Tests.UI.Samples;

// This will be the base class for our UI test classes. Here we'll set up some common configuration. Inheriting test
// classes is not mandatory but the approach is simple and effective.
public class UITestBase : OrchardCoreUITestBase
public class UITestBase : OrchardCoreUITestBase<Program>
{
// We somehow need to tell the UI Testing Toolbox where the assemblies of the app under test is (since it'll run the
// app from the command line). We use a helper for that.
protected override string AppAssemblyPath => WebAppConfigHelper
.GetAbsoluteApplicationAssemblyPath("Lombiq.OSOCE.Web", "net6.0");

protected UITestBase(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
Expand Down Expand Up @@ -76,8 +70,7 @@ protected override Task ExecuteTestAsync(
(_, argumentsBuilder) =>
{
argumentsBuilder
.Add("--OrchardCore:OrchardCore_Admin:AdminUrlPrefix")
.Add("Admin");
.AddWithValue("OrchardCore:OrchardCore_Admin:AdminUrlPrefix", "Admin");

return Task.CompletedTask;
};
Expand Down
51 changes: 1 addition & 50 deletions Lombiq.Tests.UI.Shortcuts/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
using Lombiq.HelpfulLibraries.AspNetCore.Mvc;
using Lombiq.Tests.UI.Shortcuts.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using OrchardCore.Entities;
using OrchardCore.Settings;
using OrchardCore.Users;
using OrchardCore.Users.Models;
using OrchardCore.Users.Services;
using System.Threading.Tasks;

namespace Lombiq.Tests.UI.Shortcuts.Controllers;
Expand All @@ -17,19 +12,11 @@ public class AccountController : Controller
{
private readonly UserManager<IUser> _userManager;
private readonly SignInManager<IUser> _userSignInManager;
private readonly ISiteService _siteService;
private readonly IUserService _userService;

public AccountController(
UserManager<IUser> userManager,
SignInManager<IUser> userSignInManager,
ISiteService siteService,
IUserService userService)
public AccountController(UserManager<IUser> userManager, SignInManager<IUser> userSignInManager)
{
_userManager = userManager;
_userSignInManager = userSignInManager;
_siteService = siteService;
_userService = userService;
}

[AllowAnonymous]
Expand All @@ -50,40 +37,4 @@ public async Task<IActionResult> SignOutDirectly()

return Ok();
}

[AllowAnonymous]
public async Task<ActionResult> SetUserRegistrationType(UserRegistrationType type)
{
var settings = await _siteService.LoadSiteSettingsAsync();

settings.Alter<RegistrationSettings>(
nameof(RegistrationSettings),
registrationSettings => registrationSettings.UsersCanRegister = type);

await _siteService.UpdateSiteSettingsAsync(settings);

return Ok();
}

[AllowAnonymous]
public async Task<ActionResult> CreateUser([FromJsonQueryString] CreateUserRequest userData)
{
var user = await _userService.CreateUserAsync(
new User
{
UserName = userData.UserName,
Email = userData.Email,
EmailConfirmed = true,
IsEnabled = true,
},
userData.Password,
(key, error) => ModelState.AddModelError(key, error));

if (user == null)
{
return BadRequest(ModelState);
}

return Ok("Success");
}
}
55 changes: 0 additions & 55 deletions Lombiq.Tests.UI.Shortcuts/Controllers/RecipeController.cs

This file was deleted.

79 changes: 0 additions & 79 deletions Lombiq.Tests.UI.Shortcuts/Controllers/SecurityController.cs

This file was deleted.

50 changes: 0 additions & 50 deletions Lombiq.Tests.UI.Shortcuts/Controllers/ShellFeaturesController.cs

This file was deleted.

Loading

0 comments on commit a874a57

Please sign in to comment.