Skip to content

Commit 0a4f825

Browse files
committed
updated test to use refactored helpers
1 parent a68efab commit 0a4f825

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

UiTests/B2CUiTest/B2CUiTest.cs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@
1818
namespace B2CUiTest
1919
{
2020
public class B2CUiTest : IClassFixture<InstallPlaywrightBrowserFixture>
21-
{
21+
{// if some other app is listening on the port I want can I kick it off? Also, could I kill the process using the port or at least ID it?
2222
private const string KeyvaultEmailName = "IdWeb-B2C-user";
2323
private const string KeyvaultPasswordName = "IdWeb-B2C-password";
2424
private const string KeyvaultClientSecretName = "IdWeb-B2C-Client-ClientSecret";
2525
private const string NameOfUser = "unknown";
26+
private const uint ProcessStartupRetryNum = 3;
2627
private const uint TodoListClientPort = 5000;
2728
private const uint TodoListServicePort = 44332;
2829
private const string TraceClassName = "B2C-Login";
2930
private readonly LocatorAssertionsToBeVisibleOptions _assertVisibleOptions = new() { Timeout = 25000 };
30-
private readonly string _sampleAppPath = Path.Join("4-WebApp-your-API", "4-2-B2C");
31+
private readonly string _sampleClientAppPath = Path.Join("4-WebApp-your-API", "4-2-B2C", TC.s_todoListClientPath);
32+
private readonly string _sampleServiceAppPath = Path.Join("4-WebApp-your-API", "4-2-B2C", TC.s_todoListServicePath);
3133
private readonly Uri _keyvaultUri = new("https://webappsapistests.vault.azure.net");
3234
private readonly ITestOutputHelper _output;
33-
private readonly string _testAssemblyPath = typeof(B2CUiTest).Assembly.Location;
35+
private readonly string _testAssemblyLocation = typeof(B2CUiTest).Assembly.Location;
3436

3537
public B2CUiTest(ITestOutputHelper output)
3638
{
@@ -42,6 +44,7 @@ public B2CUiTest(ITestOutputHelper output)
4244
public async Task B2C_ValidCreds_LoginLogout()
4345
{
4446
// Web app and api environmental variable setup.
47+
Dictionary<string, Process>? processes = null;
4548
DefaultAzureCredential azureCred = new();
4649
string clientSecret = await UiTestHelpers.GetValueFromKeyvaultWitDefaultCreds(_keyvaultUri, KeyvaultClientSecretName, azureCred);
4750
var serviceEnvVars = new Dictionary<string, string>
@@ -67,21 +70,14 @@ public async Task B2C_ValidCreds_LoginLogout()
6770
IBrowserContext context = await browser.NewContextAsync(new BrowserNewContextOptions { IgnoreHTTPSErrors = true });
6871
await context.Tracing.StartAsync(new() { Screenshots = true, Snapshots = true, Sources = true });
6972

70-
Process? serviceProcess = null;
71-
Process? clientProcess = null;
72-
7373
try
7474
{
7575
// Start the web app and api processes.
7676
// The delay before starting client prevents transient devbox issue where the client fails to load the first time after rebuilding.
77-
serviceProcess = UiTestHelpers.StartProcessLocally(_testAssemblyPath, _sampleAppPath + TC.s_todoListServicePath, TC.s_todoListServiceExe, serviceEnvVars);
78-
await Task.Delay(3000);
79-
clientProcess = UiTestHelpers.StartProcessLocally(_testAssemblyPath, _sampleAppPath + TC.s_todoListClientPath, TC.s_todoListClientExe, clientEnvVars);
77+
var clientProcessOptions = new ProcessStartOptions(_testAssemblyLocation, _sampleClientAppPath, TC.s_todoListClientExe, clientEnvVars); // probs need to add client specific path
78+
var serviceProcessOptions = new ProcessStartOptions(_testAssemblyLocation, _sampleServiceAppPath, TC.s_todoListServiceExe, serviceEnvVars);
8079

81-
if (!UiTestHelpers.ProcessesAreAlive(new List<Process>() { clientProcess, serviceProcess }))
82-
{
83-
Assert.Fail(TC.WebAppCrashedString);
84-
}
80+
UiTestHelpers.StartAndVerifyProcessesAreRunning([serviceProcessOptions, clientProcessOptions], out processes, ProcessStartupRetryNum);
8581

8682
// Navigate to web app the retry logic ensures the web app has time to start up to establish a connection.
8783
IPage page = await context.NewPageAsync();
@@ -93,11 +89,11 @@ public async Task B2C_ValidCreds_LoginLogout()
9389
await page.GotoAsync(TC.LocalhostUrl + TodoListClientPort);
9490
break;
9591
}
96-
catch (PlaywrightException ex)
92+
catch (PlaywrightException)
9793
{
9894
await Task.Delay(1000);
9995
InitialConnectionRetryCount--;
100-
if (InitialConnectionRetryCount == 0) { throw ex; }
96+
if (InitialConnectionRetryCount == 0) { throw; }
10197
}
10298
}
10399
LabResponse labResponse = await LabUserHelper.GetB2CLocalAccountAsync();
@@ -127,14 +123,11 @@ public async Task B2C_ValidCreds_LoginLogout()
127123
}
128124
finally
129125
{
130-
// Add the following to make sure all processes and their children are stopped.
131-
Queue<Process> processes = new Queue<Process>();
132-
if (serviceProcess != null) { processes.Enqueue(serviceProcess); }
133-
if (clientProcess != null) { processes.Enqueue(clientProcess); }
134-
UiTestHelpers.KillProcessTrees(processes);
126+
// End all processes.
127+
UiTestHelpers.EndProcesses(processes);
135128

136129
// Stop tracing and export it into a zip archive.
137-
string path = UiTestHelpers.GetTracePath(_testAssemblyPath, TraceFileName);
130+
string path = UiTestHelpers.GetTracePath(_testAssemblyLocation, TraceFileName);
138131
await context.Tracing.StopAsync(new() { Path = path });
139132
_output.WriteLine($"Trace data for {TraceFileName} recorded to {path}.");
140133

UiTests/B2CUiTest/B2CUiTest.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Azure.Identity" Version="1.12.1" />
1011
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="$(MicrosoftAspNetCoreMvcTestingVersion)" />
1112
<PackageReference Include="Microsoft.Identity.Lab.Api" Version="$(MicrosoftIdentityLabApiVersion)" />
1213
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNetTestSdkVersion)" />
@@ -26,8 +27,6 @@
2627

2728
<ItemGroup>
2829
<ProjectReference Include="..\Common\Common.csproj" />
29-
<ProjectReference Include="..\..\4-WebApp-your-API\4-2-B2C\TodoListService\TodoListService.csproj" />
30-
<ProjectReference Include="..\..\4-WebApp-your-API\4-2-B2C\Client\TodoListClient.csproj" />
3130
</ItemGroup>
3231

33-
</Project>
32+
</Project>

UiTests/Common/UiTestHelpers.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4-
using System;
5-
using System.Collections.Generic;
4+
using Azure.Core;
5+
using Azure.Security.KeyVault.Secrets;
6+
using Microsoft.Playwright;
67
using System.Diagnostics;
78
using System.Management;
89
using System.Runtime.Versioning;
@@ -421,7 +422,6 @@ public static bool StartAndVerifyProcessesAreRunning(List<ProcessStartOptions> p
421422
if (!UiTestHelpers.ProcessesAreAlive(processes.Values.ToList())) { RestartProcesses(processes, processDataEntries); }
422423
else { break; }
423424
}
424-
}
425425

426426
if (!UiTestHelpers.ProcessesAreAlive(processes.Values.ToList()))
427427
{

0 commit comments

Comments
 (0)