Skip to content

Commit cf9f27b

Browse files
Explicitly wait for UI Test App to load the Page for the Test before executing Test
Hopefully resolves issue with TAEF tests seen in the CI.
1 parent c5532f3 commit cf9f27b

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

UITests/UITests.App/App.AppService.xaml.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppS
5353

5454
if (host != null)
5555
{
56-
// TODO: Think tried to make this async before but it blewup the runtime for some reason...
57-
// We'd like the OpenPage method to ensure the navigation has finished.
58-
if (host.OpenPage(pageName))
56+
// We await the OpenPage method to ensure the navigation has finished.
57+
if (await host.OpenPage(pageName))
5958
{
6059
returnMessage.Add("Status", "OK");
6160
}

UITests/UITests.App/MainTestHost.xaml.cs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Windows.UI.Xaml;
1313
using Windows.UI.Xaml.Controls;
1414
using Windows.UI.Xaml.Input;
15+
using Windows.UI.Xaml.Media.Animation;
1516

1617
namespace UITests.App
1718
{
@@ -24,28 +25,37 @@ public sealed partial class MainTestHost
2425

2526
private Assembly _executingAssembly = Assembly.GetExecutingAssembly();
2627

28+
private TaskCompletionSource<bool> _loadingStateTask;
29+
2730
public MainTestHost()
2831
{
2932
InitializeComponent();
3033
((App)Application.Current).host = this;
3134
_queue = DispatcherQueue.GetForCurrentThread();
3235
}
3336

34-
internal bool OpenPage(string pageName)
37+
// TODO: we should better expose how to control the MainTestHost vs. making this internal.
38+
internal async Task<bool> OpenPage(string pageName)
3539
{
3640
try
3741
{
3842
Log.Comment("Trying to Load Page: " + pageName);
3943

44+
_loadingStateTask = new TaskCompletionSource<bool>();
45+
4046
// Ensure we're on the UI thread as we'll be called from the AppService now.
41-
_queue.EnqueueAsync(() =>
47+
_ = _queue.EnqueueAsync(() =>
4248
{
43-
navigationFrame.Navigate(FindPageType(pageName));
49+
// Navigate without extra animations
50+
navigationFrame.Navigate(FindPageType(pageName), new SuppressNavigationTransitionInfo());
4451
});
52+
53+
// Wait for load to complete
54+
await _loadingStateTask.Task;
4555
}
4656
catch (Exception e)
4757
{
48-
Log.Error("Exception Finding Page {0}: {1} ", pageName, e.Message);
58+
Log.Error("Exception Loading Page {0}: {1} ", pageName, e.Message);
4959
return false;
5060
}
5161

@@ -69,11 +79,34 @@ private Type FindPageType(string pageName)
6979
private void NavigationFrame_Navigated(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
7080
{
7181
Log.Comment("Navigated to Page {0}", e.SourcePageType.FullName);
82+
if (e.Content is Page page)
83+
{
84+
if (page.IsLoaded)
85+
{
86+
Log.Comment("Loaded Page {0}", e.SourcePageType.FullName);
87+
_loadingStateTask.SetResult(true);
88+
}
89+
else
90+
{
91+
page.Loaded += this.Page_Loaded;
92+
}
93+
}
94+
}
95+
96+
private void Page_Loaded(object sender, RoutedEventArgs e)
97+
{
98+
var page = sender as Page;
99+
100+
page.Loaded -= Page_Loaded;
101+
102+
Log.Comment("Loaded Page (E) {0}", page.GetType().FullName);
103+
_loadingStateTask.SetResult(true);
72104
}
73105

74106
private void NavigationFrame_NavigationFailed(object sender, Windows.UI.Xaml.Navigation.NavigationFailedEventArgs e)
75107
{
76108
Log.Error("Failed to navigate to page {0}", e.SourcePageType.FullName);
109+
_loadingStateTask.SetResult(false);
77110
}
78111
}
79112
}

0 commit comments

Comments
 (0)