This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathUITestBase.cs
More file actions
160 lines (133 loc) · 6.13 KB
/
UITestBase.cs
File metadata and controls
160 lines (133 loc) · 6.13 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Net.Client;
using Microsoft.UI.Xaml.Tests.MUXControls.InteractionTests.Common;
using Microsoft.UI.Xaml.Tests.MUXControls.InteractionTests.Infra;
using Microsoft.Windows.Apps.Test.Foundation.Controls;
using UITests.App.Protos;
using Windows.Foundation.Collections;
#if USING_TAEF
using WEX.Logging.Interop;
using WEX.TestExecution;
using WEX.TestExecution.Markup;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif
namespace UITests.Tests
{
public abstract class UITestBase
{
private TestSetupHelper helper;
internal static TestApplicationInfo UITestsAppSampleApp
{
get
{
string assemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string baseDirectory = Path.Combine(Directory.GetParent(assemblyDir).Parent.Parent.Parent.Parent.FullName, "UITests.App.Package");
Log.Comment($"Base Package Search Directory = \"{baseDirectory}\"");
#if USING_TAEF
string testAppName = "3568ebdf-5b6b-4ddd-bb17-462d614ba50f_yeyc6z1eztrme!App";
string installerName = "UITests.App";
#else
var exclude = new[] { "Microsoft.WindowsAppRuntime", "Microsoft.VCLibs" };
var files = Directory.GetFiles(baseDirectory, "*.msix", SearchOption.AllDirectories).Where(f => !exclude.Any(Path.GetFileNameWithoutExtension(f).Contains));
if (files.Count() == 0)
{
throw new Exception(string.Format("Failed to find '*.msix' in {0}'!", baseDirectory));
}
string mostRecentlyBuiltPackage = string.Empty;
DateTime timeMostRecentlyBuilt = DateTime.MinValue;
foreach (string file in files)
{
DateTime fileWriteTime = File.GetLastWriteTime(file);
if (fileWriteTime > timeMostRecentlyBuilt)
{
timeMostRecentlyBuilt = fileWriteTime;
mostRecentlyBuiltPackage = file;
}
}
string testAppName = "3568ebdf-5b6b-4ddd-bb17-462d614ba50f_yeyc6z1eztrme!App";
string installerName = mostRecentlyBuiltPackage.Replace(".msix", string.Empty);
#endif
return new TestApplicationInfo(
testAppPackageName: "UITests.App",
testAppName: testAppName,
testAppPackageFamilyName: "3568ebdf-5b6b-4ddd-bb17-462d614ba50f_yeyc6z1eztrme",
testAppMainWindowTitle: "UITests.App",
processName: "UITests.App.exe",
installerName: installerName,
isUwpApp: false,
certSerialNumber: "24d62f3b13b8b9514ead9c4de48cc30f7cc6151d",
baseAppxDir: baseDirectory);
}
}
private static TestSetupHelper.TestSetupHelperOptions TestSetupHelperOptions
=> new()
{
AutomationIdOfSafeItemToClick = string.Empty
};
public TestContext TestContext { get; set; }
[TestInitialize]
public async Task TestInitialize()
{
// This will reset the test for each run (as from original WinUI https://github.com/microsoft/microsoft-ui-xaml/blob/master/test/testinfra/MUXTestInfra/Infra/TestHelpers.cs)
// We construct it so it doesn't try to run any tests since we use the AppService Bridge to complete
// our loading.
helper = new TestSetupHelper(new string[] { }, TestSetupHelperOptions);
var pageName = GetPageForTest(TestContext);
var rez = await TestAssembly.OpenPage(pageName);
if (!rez)
{
// Error case, we didn't get confirmation of test starting.
throw new InvalidOperationException("Test host didn't confirm test ready to execute page: " + pageName);
}
Log.Comment("[Harness] Received Host Ready with Page: {0}", pageName);
Wait.ForIdle();
Log.Comment("[Harness] Starting Test for {0}...", pageName);
}
[TestCleanup]
public void TestCleanup()
{
helper.Dispose();
}
private static string GetPageForTest(TestContext testContext)
{
#if USING_TAEF
var fullTestName = testContext.TestName;
var lastDotIndex = fullTestName.LastIndexOf('.');
var testName = fullTestName.Substring(lastDotIndex + 1);
var theClassName = fullTestName.Substring(0, lastDotIndex);
#else
var testName = testContext.TestName;
var theClassName = testContext.FullyQualifiedTestClassName;
#endif
var testClassString = $"test class \"{theClassName}\"";
if (Type.GetType(theClassName) is not Type type)
{
throw new Exception($"Could not find {testClassString}.");
}
Log.Comment($"Found {testClassString}.");
var testMethodString = $"test method \"{testName}\" in {testClassString}";
if (type.GetMethod(testName) is not MethodInfo method)
{
throw new Exception($"Could not find {testMethodString}.");
}
Log.Comment($"Found {testMethodString}.");
var testpageAttributeString = $"\"{typeof(TestPageAttribute)}\" on {testMethodString}";
if (method.GetCustomAttribute(typeof(TestPageAttribute), true) is not TestPageAttribute attribute)
{
throw new Exception($"Could not find {testpageAttributeString}.");
}
Log.Comment($"Found {testpageAttributeString}. {nameof(TestPageAttribute.XamlFile)}: {attribute.XamlFile}.");
return attribute.XamlFile;
}
}
}