Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(tests) E2E launch tests to protect the Windows desktop app #1338

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Rnwood.Smtp4dev.Desktop.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
64 changes: 64 additions & 0 deletions Rnwood.Smtp4dev.Desktop.Tests/LaunchTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using FlaUI.Core.AutomationElements;
using FlaUI.Core.Definitions;
using FlaUI.Core.Tools;
using FlaUI.UIA3;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Windows.Forms;
namespace Rnwood.Smtp4dev.Desktop.Tests
{
public class Tests
{
[SetUp]
public void Setup()
{
}

[Test]
public void LaunchAndCheckUILoaded()
{
string workingDir = Environment.GetEnvironmentVariable("SMTP4DEV_E2E_WORKINGDIR");
string binary = Environment.GetEnvironmentVariable("SMTP4DEV_E2E_BINARY");


using FlaUI.Core.Application app = FlaUI.Core.Application.Launch(new ProcessStartInfo(binary, "--smtpport=0") { WorkingDirectory = workingDir });
using var automation = new UIA3Automation();

app.WaitWhileMainHandleIsMissing();
var window = app.GetMainWindow(automation);
window.Move(0, 0);


try
{

Retry.WhileException(() =>
{
app.WaitWhileBusy();
window = app.GetMainWindow(automation);
var element = window.FindFirstDescendant(f => f.ByName("SMTP server", FlaUI.Core.Definitions.PropertyConditionFlags.MatchSubstring)) ??
throw new Exception( "Did not find status label");
var text = element.AsButton().Name;
if (!text.Contains("listening on port"))
throw new Exception("Server not listening: " + text);
}, TimeSpan.FromSeconds(30), null, true);

} finally
{
try
{
string screenshotFileName = Path.Combine(TestContext.CurrentContext.WorkDirectory, Guid.NewGuid() + ".png");
window.CaptureToFile(screenshotFileName);
TestContext.AddTestAttachment(screenshotFileName);
} catch (Exception e)
{
TestContext.WriteLine($"Screenshot failed with exception: {e}");
}

app.Close();
}


}
}
}
22 changes: 22 additions & 0 deletions Rnwood.Smtp4dev.Desktop.Tests/Rnwood.Smtp4dev.Desktop.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FlaUI.Core" Version="4.0.0" />
<PackageReference Include="FlaUI.UIA3" Version="4.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions Rnwood.Smtp4dev.Desktop.Tests/local.runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<RunConfiguration>
<EnvironmentVariables>
<SMTP4DEV_E2E_WORKINGDIR>C:\src\smtp4dev-master\Rnwood.Smtp4dev.Desktop\bin\Debug\net8.0</SMTP4DEV_E2E_WORKINGDIR>
<SMTP4DEV_E2E_BINARY>C:\src\smtp4dev-master\Rnwood.Smtp4dev.Desktop\bin\Debug\net8.0\Rnwood.Smtp4dev.Desktop.exe</SMTP4DEV_E2E_BINARY>
</EnvironmentVariables>
</RunConfiguration>
</RunSettings>
3 changes: 3 additions & 0 deletions Rnwood.Smtp4dev.Desktop/DesktopApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ private static PhotinoWindow CreateWindow(string workingDir, string title = "smt
.SetIconFile(Path.Combine(workingDir, iconFile))
.SetTitle(title)
.SetDevToolsEnabled(true)
.SetUseOsDefaultLocation(true)
.SetMinSize(800,600)
.SetUseOsDefaultSize(true)
.SetContextMenuEnabled(false);

result.RegisterWebMessageReceivedHandler((s, m) =>
Expand Down
17 changes: 16 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ stages:
SMTP4DEV_E2E_BINARY: $(Agent.TempDirectory)/e2e/Rnwood.Smtp4dev
inputs:
command: test
projects: Rnwood.Smtp4dev.Tests
configuration: release
- powershell: move-item $(Build.ArtifactStagingDirectory)/$(platformName)/Rnwood.Smtp4dev.zip $(Build.ArtifactStagingDirectory)/$(platformName)/Rnwood.Smtp4dev-$(platformName)-$(tag).zip
displayName: Rename artifact
Expand Down Expand Up @@ -177,12 +178,26 @@ stages:
projects: "Rnwood.Smtp4dev.Desktop/Rnwood.Smtp4dev.Desktop.csproj"
publishWebProjects: false
arguments: '-c Release -r win-x64 -p:version=$(tag) --self-contained -p:PublishSingleFile=true -p:PublishReadyToRun=true -p:PublishSingleFile=true -o "$(Build.ArtifactStagingDirectory)/win-x64-desktop"'
- task: ExtractFiles@1
displayName: Extract files for E2E tests
inputs:
archiveFilePatterns: '$(Build.ArtifactStagingDirectory)/win-x64-desktop/Rnwood.Smtp4dev.Desktop.zip'
destinationFolder: $(Agent.TempDirectory)/e2e
- task: DotNetCoreCLI@2
displayName: Run Tests
env:
SMTP4DEV_E2E_WORKINGDIR: $(Agent.TempDirectory)/e2e
SMTP4DEV_E2E_BINARY: $(Agent.TempDirectory)/e2e/Rnwood.Smtp4dev.Desktop
inputs:
command: test
projects: Rnwood.Smtp4dev.Desktop.Tests
configuration: release
- powershell: move-item $(Build.ArtifactStagingDirectory)/win-x64-desktop/Rnwood.Smtp4dev.Desktop.zip $(Build.ArtifactStagingDirectory)/win-x64-desktop/Rnwood.Smtp4dev.Desktop-win-x64-$(tag).zip
displayName: Rename artifact
- publish: $(Build.ArtifactStagingDirectory)/win-x64-desktop
artifact: win-x64-desktop

# - task: DotNetCoreCLI@2
# - task: DotNetCoreCLI@2run
# displayName: Build .NET Core - linux-x64-desktop
# inputs:
# command: publish
Expand Down
6 changes: 6 additions & 0 deletions smtp4dev.sln
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Rnwood.Smtp4dev.Desktop", "Rnwood.Smtp4dev.Desktop\Rnwood.Smtp4dev.Desktop.csproj", "{E75DDB87-5000-416E-91DB-1CCC996F5882}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rnwood.Smtp4dev.Desktop.Tests", "Rnwood.Smtp4dev.Desktop.Tests\Rnwood.Smtp4dev.Desktop.Tests.csproj", "{44686E8D-112A-4506-BD48-432E99D2D0DC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -32,6 +34,10 @@ Global
{E75DDB87-5000-416E-91DB-1CCC996F5882}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E75DDB87-5000-416E-91DB-1CCC996F5882}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E75DDB87-5000-416E-91DB-1CCC996F5882}.Release|Any CPU.Build.0 = Release|Any CPU
{44686E8D-112A-4506-BD48-432E99D2D0DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44686E8D-112A-4506-BD48-432E99D2D0DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44686E8D-112A-4506-BD48-432E99D2D0DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44686E8D-112A-4506-BD48-432E99D2D0DC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down