Skip to content

Commit 1bccda8

Browse files
committed
adding helpers
1 parent 6cfd554 commit 1bccda8

File tree

4 files changed

+102
-20
lines changed

4 files changed

+102
-20
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
{
1+
{
22
"AzureAd": {
33
"Instance": "https://login.microsoftonline.com/",
4-
"Domain": "msidlab3.onmicrosoft.com",
5-
"TenantId": "8e44f19d-bbab-4a82-b76b-4cd0a6fbc97a",
6-
"ClientId": "d9cde0be-ad97-41e6-855e-2f85136671c1",
4+
"Domain": "[Enter the domain of your tenant, e.g. contoso.onmicrosoft.com]",
5+
"TenantId": "[Enter 'common', or 'organizations' or the Tenant Id (Obtained from the Azure portal. Select 'Endpoints' from the 'App registrations' blade and use the GUID in any of the URLs), e.g. da41245a5-11b3-996c-00a8-4d99re19f292]",
6+
"ClientId": "[Enter the Client Id (Application ID obtained from the Azure portal), e.g. ba74781c2-53c2-442a-97c2-3d60re42f403]",
77
"CallbackPath": "/signin-oidc",
88
"SignedOutCallbackPath": "/signout-callback-oidc"
99
},
@@ -15,4 +15,4 @@
1515
}
1616
},
1717
"AllowedHosts": "*"
18-
}
18+
}

UiTests/Common/TestConstants.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,26 @@ namespace Common
99
public static class TestConstants
1010
{
1111
public const string AppSetttingsDotJson = "appsettings.json";
12+
public const string ClientFilePrefix = "client_";
13+
public const string EmailText = "Email";
1214
public const string Headless = "headless";
1315
public const string HeaderText = "Header";
14-
public const string EmailText = "Email";
16+
public const string HttpStarColon = "http://*:";
17+
public const string HttpsStarColon = "https://*:";
18+
public const string KestrelEndpointEnvVar = "Kestrel:Endpoints:Http:Url";
19+
public const string LocalhostUrl = @"https://localhost:";
20+
public const string OIDCUser = "[email protected]";
1521
public const string PasswordText = "Password";
22+
public const string ServerFilePrefix = "server_";
1623
public const string TodoTitle1 = "Testing create todo item";
1724
public const string TodoTitle2 = "Testing edit todo item";
18-
public const string LocalhostUrl = @"https://localhost:";
19-
public const string KestrelEndpointEnvVar = "Kestrel:Endpoints:Http:Url";
20-
public const string HttpStarColon = "http://*:";
21-
public const string HttpsStarColon = "https://*:";
2225
public const string WebAppCrashedString = $"The web app process has exited prematurely.";
23-
public const string OIDCUser = "[email protected]";
26+
2427
public static readonly string s_oidcWebAppExe = Path.DirectorySeparatorChar.ToString() + "WebApp-OpenIDConnect-DotNet.exe";
2528
public static readonly string s_oidcWebAppPath = Path.DirectorySeparatorChar.ToString() + "WebApp-OpenIDConnect";
29+
public static readonly string s_todoListClientExe = Path.DirectorySeparatorChar.ToString() + "TodoListClient.exe";
30+
public static readonly string s_todoListClientPath = Path.DirectorySeparatorChar.ToString() + "Client";
31+
public static readonly string s_todoListServiceExe = Path.DirectorySeparatorChar.ToString() + "TodoListService.exe";
32+
public static readonly string s_todoListServicePath = Path.DirectorySeparatorChar.ToString() + "TodoListService";
2633
}
2734
}

UiTests/Common/UiTestHelpers.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,26 @@ private static string GetApplicationWorkingDirectory(string testAssemblyLocation
223223
);
224224
}
225225

226+
/// <summary>
227+
/// Builds the path to the process's directory
228+
/// </summary>
229+
/// <param name="testAssemblyLocation">The path to the test's directory</param>
230+
/// <param name="appLocation">The path to the processes directory</param>
231+
/// <returns>The path to the directory for the given app</returns>
232+
private static string GetAppsettingsDirectory(string testAssemblyLocation, string appLocation)
233+
{
234+
string testedAppLocation = Path.GetDirectoryName(testAssemblyLocation)!;
235+
// e.g. microsoft-identity-web\tests\E2E Tests\WebAppUiTests\bin\Debug\net6.0
236+
string[] segments = testedAppLocation.Split(Path.DirectorySeparatorChar);
237+
int numberSegments = segments.Length;
238+
int startLastSegments = numberSegments - 3;
239+
int endFirstSegments = startLastSegments - 2;
240+
return Path.Combine(
241+
Path.Combine(segments.Take(endFirstSegments).ToArray()),
242+
appLocation
243+
);
244+
}
245+
226246
/// <summary>
227247
/// Creates absolute path for Playwright trace file
228248
/// </summary>
@@ -428,6 +448,70 @@ public static string GetRunningProcessAsString(Dictionary<string, Process>? proc
428448
}
429449
return runningProcesses.ToString();
430450
}
451+
452+
/// <summary>
453+
/// Takes two paths to existing files and swaps their names and locations effectively swapping the contents of the files.
454+
/// </summary>
455+
/// <param name="path1">The path of the first file to swap</param>
456+
/// <param name="path2">The path of the file to swap it with</param>
457+
public static void SwapFiles(string path1, string path2)
458+
{
459+
string tempFile = Path.GetTempFileName();
460+
string file1Name = Path.GetFileName(path1);
461+
string file2Name = Path.GetFileName(path2);
462+
string file1Dir = Path.GetDirectoryName(path1);
463+
string file2Dir = Path.GetDirectoryName(path2);
464+
465+
// Move file1 to tempFile
466+
File.Move(path1, tempFile);
467+
468+
// Move file2 to file1's original location and rename it to file1's name
469+
File.Move(path2, Path.Combine(file1Dir, file1Name));
470+
471+
// Move tempFile (original file1) to file2's original location and rename it to file2's name
472+
File.Move(tempFile, Path.Combine(file2Dir, file2Name));
473+
474+
Console.WriteLine("Files swapped and renamed successfully.");
475+
}
476+
477+
478+
public static void RebuildSolution(string solutionPath)
479+
{
480+
ProcessStartInfo startInfo = new ProcessStartInfo
481+
{
482+
FileName = "dotnet",
483+
Arguments = $"build {solutionPath} --no-incremental",
484+
RedirectStandardOutput = true,
485+
RedirectStandardError = true,
486+
UseShellExecute = false,
487+
CreateNoWindow = true
488+
};
489+
490+
using (Process process = new Process())
491+
{
492+
process.StartInfo = startInfo;
493+
process.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data);
494+
process.ErrorDataReceived += (sender, e) => Console.WriteLine(e.Data);
495+
496+
process.Start();
497+
process.BeginOutputReadLine();
498+
process.BeginErrorReadLine();
499+
process.WaitForExit();
500+
}
501+
502+
Console.WriteLine("Solution rebuild initiated.");
503+
}
504+
505+
public static void BuildSampleWithTestAppsettings(string testAssemblyLocation, string appLocation, string testAppsettingsName)
506+
{
507+
string appsettingsDirectory = GetAppsettingsDirectory(testAssemblyLocation, appLocation);
508+
string appsettingsPath = Path.Combine(appsettingsDirectory, TestConstants.AppSetttingsDotJson);
509+
string testAppsettingsPath = Path.Combine(appsettingsDirectory, testAppsettingsName);
510+
SwapFiles(appsettingsPath, testAppsettingsPath);
511+
RebuildSolution(appsettingsDirectory);
512+
SwapFiles(appsettingsPath, testAppsettingsPath);
513+
514+
}
431515
}
432516

433517
/// <summary>

UiTests/UiTests/appsettings.Development.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)