Skip to content

Commit 3fa0a74

Browse files
author
Zhaopeng Wang
committed
add ui initialization tests and add exit scope exe function
1 parent b96bcf1 commit 3fa0a74

File tree

4 files changed

+270
-3
lines changed

4 files changed

+270
-3
lines changed

src/common/UITestAutomation/Element/Element.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,26 @@ public T Find<T>(string name, int timeoutMS = 3000)
184184
return this.Find<T>(By.Name(name), timeoutMS);
185185
}
186186

187+
/// <summary>
188+
/// Shortcut for this.FindAllByAccessibilityId<T>(accessibilityId, timeoutMS)
189+
/// </summary>
190+
/// <typeparam name="T">The class of the element, should be Element or its derived class.</typeparam>
191+
/// <param name="accessibilityId">The accessibilityId of the element.</param>
192+
/// <param name="timeoutMS">The timeout in milliseconds (default is 3000).</param>
193+
/// <returns>The found element.</returns>
194+
public T FindByAccessibilityId<T>(string accessibilityId, int timeoutMS = 3000)
195+
where T : Element, new()
196+
{
197+
Assert.IsNotNull(this.windowsElement, $"WindowsElement is null in method Find<{typeof(T).Name}> with parameters: accessibilityId = {accessibilityId}, timeoutMS = {timeoutMS}");
198+
199+
// leverage findAll to filter out mismatched elements
200+
var collection = this.FindAllByAccessibilityId<T>(accessibilityId, timeoutMS);
201+
202+
Assert.IsTrue(collection.Count > 0, $"Element not found using selector: {accessibilityId}");
203+
204+
return collection[0];
205+
}
206+
187207
/// <summary>
188208
/// Finds an element by the selector.
189209
/// Shortcut for this.Find<Element>(by, timeoutMS)
@@ -231,6 +251,29 @@ public ReadOnlyCollection<T> FindAll<T>(By by, int timeoutMS = 3000)
231251
return foundElements ?? new ReadOnlyCollection<T>([]);
232252
}
233253

254+
/// <summary>
255+
/// Finds all elements by accessibilityId.
256+
/// </summary>
257+
/// <typeparam name="T">The class of the elements, should be Element or its derived class.</typeparam>
258+
/// <param name="accessibilityId">The accessibilityId to find the elements.</param>
259+
/// <param name="timeoutMS">The timeout in milliseconds (default is 3000).</param>
260+
/// <returns>A read-only collection of the found elements.</returns>
261+
public ReadOnlyCollection<T> FindAllByAccessibilityId<T>(string accessibilityId, int timeoutMS = 3000)
262+
where T : Element, new()
263+
{
264+
Assert.IsNotNull(this.windowsElement, $"WindowsElement is null in method FindAll<{typeof(T).Name}> with parameters: accessibilityId = {accessibilityId}, timeoutMS = {timeoutMS}");
265+
var foundElements = FindHelper.FindAll<T, AppiumWebElement>(
266+
() =>
267+
{
268+
var elements = this.windowsElement.FindElementsByAccessibilityId(accessibilityId);
269+
return elements;
270+
},
271+
this.driver,
272+
timeoutMS);
273+
274+
return foundElements ?? new ReadOnlyCollection<T>([]);
275+
}
276+
234277
/// <summary>
235278
/// Finds all elements by the selector.
236279
/// Shortcut for this.FindAll<T>(By.Name(name), timeoutMS)

src/common/UITestAutomation/SessionHelper.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,13 @@ public void StartExe(string appPath)
9595
}
9696

9797
/// <summary>
98-
/// Restarts now exe and takes control of it.
98+
/// Exit a exe.
9999
/// </summary>
100-
public void RestartScopeExe()
100+
/// <param name="path">The path to the application executable.</param>
101+
public void ExitExe(string path)
101102
{
102103
// Exit Exe
103-
string exeName = Path.GetFileNameWithoutExtension(sessionPath);
104+
string exeName = Path.GetFileNameWithoutExtension(path);
104105

105106
// PowerToys.FancyZonesEditor
106107
Process[] processes = Process.GetProcessesByName(exeName);
@@ -116,7 +117,22 @@ public void RestartScopeExe()
116117
Assert.Fail($"Failed to terminate process {process.ProcessName} (ID: {process.Id}): {ex.Message}");
117118
}
118119
}
120+
}
119121

122+
/// <summary>
123+
/// Exit now exe.
124+
/// </summary>
125+
public void ExitScopeExe()
126+
{
127+
ExitExe(sessionPath);
128+
}
129+
130+
/// <summary>
131+
/// Restarts now exe and takes control of it.
132+
/// </summary>
133+
public void RestartScopeExe()
134+
{
135+
ExitExe(sessionPath);
120136
StartExe(locationPath + sessionPath);
121137
}
122138

src/common/UITestAutomation/UITestBase.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public UITestBase(PowerToysModule scope = PowerToysModule.PowerToysSettings)
3333

3434
~UITestBase()
3535
{
36+
this.ExitScopeExe();
3637
this.sessionHelper.Cleanup();
3738
}
3839

@@ -163,5 +164,14 @@ public void RestartScopeExe()
163164
this.Session = new Session(this.sessionHelper.GetRoot(), this.sessionHelper.GetDriver());
164165
return;
165166
}
167+
168+
/// <summary>
169+
/// Restart scope exe.
170+
/// </summary>
171+
public void ExitScopeExe()
172+
{
173+
sessionHelper.ExitScopeExe();
174+
return;
175+
}
166176
}
167177
}
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
7+
using FancyZonesEditorCommon.Data;
8+
using Microsoft.FancyZonesEditor.UITests;
9+
using Microsoft.FancyZonesEditor.UITests.Utils;
10+
using Microsoft.FancyZonesEditor.UnitTests.Utils;
11+
using Microsoft.PowerToys.UITest;
12+
using Microsoft.VisualStudio.TestTools.UnitTesting;
13+
using Windows.UI;
14+
using static FancyZonesEditorCommon.Data.EditorParameters;
15+
using static Microsoft.ApplicationInsights.MetricDimensionNames.TelemetryContext;
16+
17+
namespace UITests_FancyZonesEditor
18+
{
19+
[TestClass]
20+
public class UIInitializaionTest : UITestBase
21+
{
22+
public UIInitializaionTest()
23+
: base(PowerToysModule.FancyZone)
24+
{
25+
}
26+
27+
[TestInitialize]
28+
public void TestInitialize()
29+
{
30+
FancyZonesEditorHelper.Files.ParamsIOHelper.RestoreData();
31+
}
32+
33+
[TestCleanup]
34+
public void TestCleanup()
35+
{
36+
this.ExitScopeExe();
37+
}
38+
39+
[TestMethod]
40+
public void EditorParams_VerifySelectedMonitor()
41+
{
42+
EditorParameters editorParameters = new EditorParameters();
43+
ParamsWrapper parameters = new ParamsWrapper
44+
{
45+
ProcessId = 1,
46+
SpanZonesAcrossMonitors = false,
47+
Monitors = new List<NativeMonitorDataWrapper>
48+
{
49+
new NativeMonitorDataWrapper
50+
{
51+
Monitor = "monitor-1",
52+
MonitorInstanceId = "instance-id-1",
53+
MonitorSerialNumber = "serial-number-1",
54+
MonitorNumber = 1,
55+
VirtualDesktop = "{FF34D993-73F3-4B8C-AA03-73730A01D6A8}",
56+
Dpi = 96,
57+
LeftCoordinate = 0,
58+
TopCoordinate = 0,
59+
WorkAreaHeight = 1040,
60+
WorkAreaWidth = 1920,
61+
MonitorHeight = 1080,
62+
MonitorWidth = 1920,
63+
IsSelected = false,
64+
},
65+
new NativeMonitorDataWrapper
66+
{
67+
Monitor = "monitor-2",
68+
MonitorInstanceId = "instance-id-2",
69+
MonitorSerialNumber = "serial-number-2",
70+
MonitorNumber = 2,
71+
VirtualDesktop = "{FF34D993-73F3-4B8C-AA03-73730A01D6A8}",
72+
Dpi = 96,
73+
LeftCoordinate = 1920,
74+
TopCoordinate = 0,
75+
WorkAreaHeight = 1040,
76+
WorkAreaWidth = 1920,
77+
MonitorHeight = 1080,
78+
MonitorWidth = 1920,
79+
IsSelected = true,
80+
},
81+
},
82+
};
83+
FancyZonesEditorHelper.Files.ParamsIOHelper.WriteData(editorParameters.Serialize(parameters));
84+
this.RestartScopeExe();
85+
86+
Assert.IsFalse(Session.Find<Element>("Monitor 1").Selected);
87+
Assert.IsTrue(Session.Find<Element>("Monitor 2").Selected);
88+
}
89+
90+
[TestMethod]
91+
public void EditorParams_VerifyMonitorScaling()
92+
{
93+
EditorParameters editorParameters = new EditorParameters();
94+
ParamsWrapper parameters = new ParamsWrapper
95+
{
96+
ProcessId = 1,
97+
SpanZonesAcrossMonitors = false,
98+
Monitors = new List<NativeMonitorDataWrapper>
99+
{
100+
new NativeMonitorDataWrapper
101+
{
102+
Monitor = "monitor-1",
103+
MonitorInstanceId = "instance-id-1",
104+
MonitorSerialNumber = "serial-number-1",
105+
MonitorNumber = 1,
106+
VirtualDesktop = "{FF34D993-73F3-4B8C-AA03-73730A01D6A8}",
107+
Dpi = 192, // 200% scaling
108+
LeftCoordinate = 0,
109+
TopCoordinate = 0,
110+
WorkAreaHeight = 1040,
111+
WorkAreaWidth = 1920,
112+
MonitorHeight = 1080,
113+
MonitorWidth = 1920,
114+
IsSelected = true,
115+
},
116+
},
117+
};
118+
FancyZonesEditorHelper.Files.ParamsIOHelper.WriteData(editorParameters.Serialize(parameters));
119+
this.RestartScopeExe();
120+
121+
var monitor = Session.Find<Element>("Monitor 1");
122+
var scaling = monitor.FindByAccessibilityId<Element>("ScalingText");
123+
Assert.AreEqual("200%", scaling.Text);
124+
}
125+
126+
[TestMethod]
127+
public void EditorParams_VerifyMonitorResolution()
128+
{
129+
EditorParameters editorParameters = new EditorParameters();
130+
ParamsWrapper parameters = new ParamsWrapper
131+
{
132+
ProcessId = 1,
133+
SpanZonesAcrossMonitors = false,
134+
Monitors = new List<NativeMonitorDataWrapper>
135+
{
136+
new NativeMonitorDataWrapper
137+
{
138+
Monitor = "monitor-1",
139+
MonitorInstanceId = "instance-id-1",
140+
MonitorSerialNumber = "serial-number-1",
141+
MonitorNumber = 1,
142+
VirtualDesktop = "{FF34D993-73F3-4B8C-AA03-73730A01D6A8}",
143+
Dpi = 192,
144+
LeftCoordinate = 0,
145+
TopCoordinate = 0,
146+
WorkAreaHeight = 1040,
147+
WorkAreaWidth = 1920,
148+
MonitorHeight = 1080,
149+
MonitorWidth = 1920,
150+
IsSelected = true,
151+
},
152+
},
153+
};
154+
FancyZonesEditorHelper.Files.ParamsIOHelper.WriteData(editorParameters.Serialize(parameters));
155+
this.RestartScopeExe();
156+
157+
var monitor = Session.Find<Element>("Monitor 1");
158+
var resolution = monitor.FindByAccessibilityId<Element>("ResolutionText");
159+
Assert.AreEqual("1920 × 1080", resolution.Text);
160+
}
161+
162+
[TestMethod]
163+
public void EditorParams_SpanAcrossMonitors()
164+
{
165+
EditorParameters editorParameters = new EditorParameters();
166+
ParamsWrapper parameters = new ParamsWrapper
167+
{
168+
ProcessId = 1,
169+
SpanZonesAcrossMonitors = true,
170+
Monitors = new List<NativeMonitorDataWrapper>
171+
{
172+
new NativeMonitorDataWrapper
173+
{
174+
Monitor = "monitor-1",
175+
MonitorInstanceId = "instance-id-1",
176+
MonitorSerialNumber = "serial-number-1",
177+
MonitorNumber = 1,
178+
VirtualDesktop = "{FF34D993-73F3-4B8C-AA03-73730A01D6A8}",
179+
Dpi = 192,
180+
LeftCoordinate = 0,
181+
TopCoordinate = 0,
182+
WorkAreaHeight = 1040,
183+
WorkAreaWidth = 1920,
184+
MonitorHeight = 1080,
185+
MonitorWidth = 1920,
186+
IsSelected = true,
187+
},
188+
},
189+
};
190+
FancyZonesEditorHelper.Files.ParamsIOHelper.WriteData(editorParameters.Serialize(parameters));
191+
this.RestartScopeExe();
192+
193+
var monitor = Session.Find<Element>("Monitor 1");
194+
Assert.IsNotNull(monitor);
195+
Assert.IsTrue(monitor.Selected);
196+
}
197+
}
198+
}

0 commit comments

Comments
 (0)