Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with mutiple UI Tests #4007

Merged
21 commits merged into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 56 additions & 37 deletions UITests/UITests.App/App.AppService.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.Toolkit.Mvvm.Messaging;
using UITests.App.Pages;
using Windows.ApplicationModel.Activation;
Expand All @@ -20,6 +20,9 @@ namespace UITests.App
/// </summary>
public sealed partial class App
{
private static readonly ValueSet BadResult = new() { { "Status", "BAD" } };
private static readonly ValueSet OkResult = new() { { "Status", "OK" } };

private AppServiceConnection _appServiceConnection;
private BackgroundTaskDeferral _appServiceDeferral;

Expand All @@ -37,45 +40,39 @@ protected override void OnBackgroundActivated(BackgroundActivatedEventArgs args)

private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
AppServiceDeferral messageDeferral = args.GetDeferral();
ValueSet message = args.Request.Message;
string cmd = message["Command"] as string;
var messageDeferral = args.GetDeferral();
var message = args.Request.Message;
if(!TryGetValueAndLog(message, "Command", out var cmd))
{
await args.Request.SendResponseAsync(BadResult);
messageDeferral.Complete();
return;
}

try
switch (cmd)
{
// Return the data to the caller.
if (cmd == "Start")
{
var pageName = message["Page"] as string;
case "OpenPage":
if (!TryGetValueAndLog(message, "Page", out var pageName))
{
await args.Request.SendResponseAsync(BadResult);
break;
}

Log.Comment("Received request for Page: {0}", pageName);

ValueSet returnMessage = new ValueSet();

// We await the OpenPage method to ensure the navigation has finished.
if (await WeakReferenceMessenger.Default.Send(new RequestPageMessage(pageName)))
{
returnMessage.Add("Status", "OK");
}
else
{
returnMessage.Add("Status", "BAD");
}
var pageResponse = await WeakReferenceMessenger.Default.Send(new RequestPageMessage(pageName));

await args.Request.SendResponseAsync(returnMessage);
}
}
catch (Exception e)
{
// Your exception handling code here.
Log.Error("Exception processing request: {0}", e.Message);
}
finally
{
// Complete the deferral so that the platform knows that we're done responding to the app service call.
// Note: for error handling: this must be called even if SendResponseAsync() throws an exception.
messageDeferral.Complete();
await args.Request.SendResponseAsync(pageResponse ? OkResult : BadResult);

break;
default:
break;
}

// Complete the deferral so that the platform knows that we're done responding to the app service call.
// Note: for error handling: this must be called even if SendResponseAsync() throws an exception.
messageDeferral.Complete();
}

private void OnAppServicesCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
Expand All @@ -88,16 +85,38 @@ private void AppServiceConnection_ServiceClosed(AppServiceConnection sender, App
_appServiceDeferral.Complete();
}

public async void SendLogMessage(string level, string msg)
public async Task SendLogMessage(string level, string msg)
{
var message = new ValueSet();
message.Add("Command", "Log");
message.Add("Level", level);
message.Add("Message", msg);
var message = new ValueSet
{
{ "Command", "Log" },
{ "Level", level },
{ "Message", msg }
};

await _appServiceConnection.SendMessageAsync(message);

// TODO: do we care if we have a problem here?
}

private static bool TryGetValueAndLog(ValueSet message, string key, out string value)
{
value = null;
if (!message.TryGetValue(key, out var o))
{
Log.Error($"Could not find the key \"{key}\" in the message.");
return false;
}

if (o is not string s)
{
Log.Error($"{key}'s value is not a string");
return false;
}

value = s;

return true;
}
}
}
8 changes: 8 additions & 0 deletions UITests/UITests.App/HomePage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Page x:Class="UITests.App.HomePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="No test loaded." />
</Page>
19 changes: 19 additions & 0 deletions UITests/UITests.App/HomePage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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 Windows.UI.Xaml.Controls;

namespace UITests.App
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class HomePage
{
public HomePage()
{
this.InitializeComponent();
}
}
}
26 changes: 25 additions & 1 deletion UITests/UITests.App/MainTestHost.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,35 @@
xmlns:testhelpers="using:AppTestAutomationHelpers"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">
<Page.Resources>
<Style x:Key="AutomationHelperStyle"
TargetType="Control">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="IsHitTestVisible" Value="False" />
<Setter Property="Width" Value="0" />
<Setter Property="Height" Value="0" />
<Setter Property="Opacity" Value="0" />
</Style>
</Page.Resources>

<Grid>
<testhelpers:TestAutomationHelpersPanel />

<StackPanel Width="0"
Height="0"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
Orientation="Horizontal">
<Button x:Name="GoBackInvokerButton"
AutomationProperties.AutomationId="__GoBackInvoker"
Click="GoBackInvokerButton_Click"
Style="{StaticResource AutomationHelperStyle}" />
<Button x:Name="CloseAppInvokerButton"
AutomationProperties.AutomationId="__CloseAppInvoker"
Click="CloseAppInvokerButton_Click"
Style="{StaticResource AutomationHelperStyle}" />
</StackPanel>
<Frame x:Name="navigationFrame"
IsNavigationStackEnabled="False"
Navigated="NavigationFrame_Navigated"
NavigationFailed="NavigationFrame_NavigationFailed" />
</Grid>
Expand Down
12 changes: 12 additions & 0 deletions UITests/UITests.App/MainTestHost.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,17 @@ private void NavigationFrame_NavigationFailed(object sender, Windows.UI.Xaml.Nav
Log.Error("Failed to navigate to page {0}", e.SourcePageType.FullName);
_loadingStateTask.SetResult(false);
}

private void GoBackInvokerButton_Click(object sender, RoutedEventArgs e)
{
Log.Comment("Go Back Clicked. Navigating to Page...");
navigationFrame.Navigate(typeof(HomePage));
Log.Comment("Navigated to Page.");
}

private void CloseAppInvokerButton_Click(object sender, RoutedEventArgs e)
{
App.Current.Exit();
}
}
}
12 changes: 12 additions & 0 deletions UITests/UITests.App/UITests.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<GenerateAppxPackageOnBuild>true</GenerateAppxPackageOnBuild>
<AppxPackageSigningEnabled>True</AppxPackageSigningEnabled>
<IsTestHost>true</IsTestHost>
<LangVersion>9.0</LangVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -131,6 +132,9 @@
<Compile Include="App.AppService.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="HomePage.xaml.cs">
<DependentUpon>HomePage.xaml</DependentUpon>
</Compile>
<Compile Include="MainTestHost.xaml.cs">
<DependentUpon>MainTestHost.xaml</DependentUpon>
</Compile>
Expand Down Expand Up @@ -158,6 +162,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="HomePage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="MainTestHost.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down Expand Up @@ -207,6 +215,10 @@
<Project>{daeb9cec-c817-33b2-74b2-bc379380db72}</Project>
<Name>Microsoft.Toolkit.Uwp.UI.Controls.DataGrid</Name>
</ProjectReference>
<ProjectReference Include="..\..\Microsoft.Toolkit.Uwp.UI.Controls.Input\Microsoft.Toolkit.Uwp.UI.Controls.Input.csproj">
<Project>{af1be4e9-e2e1-4729-b076-b3725d8e21ee}</Project>
<Name>Microsoft.Toolkit.Uwp.UI.Controls.Input</Name>
</ProjectReference>
<ProjectReference Include="..\..\Microsoft.Toolkit.Uwp.UI.Controls.Layout\Microsoft.Toolkit.Uwp.UI.Controls.Layout.csproj">
<Project>{cb444381-18ba-4a51-bb32-3a498bcc1e99}</Project>
<Name>Microsoft.Toolkit.Uwp.UI.Controls.Layout</Name>
Expand Down
2 changes: 1 addition & 1 deletion UITests/UITests.Tests.MSTest/UITests.Tests.MSTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<TargetFrameworkIdentifier>.NETCoreApp</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v3.1</TargetFrameworkVersion>
<RuntimeIdentifier>win10-x86</RuntimeIdentifier>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>

<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<PlatformTarget>x86</PlatformTarget>
Expand Down
71 changes: 71 additions & 0 deletions UITests/UITests.Tests.Shared/Controls/RangeSelectorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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 Microsoft.Windows.Apps.Test.Foundation.Controls;
using Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Common;
using Windows.UI.Xaml.Tests.MUXControls.InteractionTests.Infra;

#if USING_TAEF
using WEX.Logging.Interop;
using WEX.TestExecution;
using WEX.TestExecution.Markup;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif

namespace UITests.Tests
{

[TestClass]
public class RangeSelectorTest : UITestBase
{
[ClassInitialize]
[TestProperty("RunAs", "User")]
[TestProperty("Classification", "ScenarioTestSuite")]
[TestProperty("Platform", "Any")]
public static void ClassInitialize(TestContext testContext)
{
TestEnvironment.Initialize(testContext, WinUICsUWPSampleApp);
}

[TestMethod]
[TestPage("RangeSelectorTestPage")]
public void SimpleTestMethod2()
{
var inputStepFrequency = new Edit(FindElement.ById("inputStepFrequency"));
var inputMinimum = new Edit(FindElement.ById("inputMinimum"));
var inputRangeStart = new Edit(FindElement.ById("inputRangeStart"));
var inputRangeEnd = new Edit(FindElement.ById("inputRangeEnd"));
var inputMaximum = new Edit(FindElement.ById("inputMaximum"));

var submitStepFrequency = new Button(FindElement.ById("submitStepFrequency"));
var submitMinimum = new Button(FindElement.ById("submitMinimum"));
var submitRangeStart = new Button(FindElement.ById("submitRangeStart"));
var submitRangeEnd = new Button(FindElement.ById("submitRangeEnd"));
var submitMaximum = new Button(FindElement.ById("submitMaximum"));
var submitAll = new Button(FindElement.ById("submitAll"));

KeyboardHelper.EnterText(inputStepFrequency, "1");
KeyboardHelper.EnterText(inputMinimum, "0");
KeyboardHelper.EnterText(inputRangeStart, "10");
KeyboardHelper.EnterText(inputRangeEnd, "90");
KeyboardHelper.EnterText(inputMaximum, "100");

submitAll.Click();
Wait.ForIdle();

var currentStepFrequency = new TextBlock(FindElement.ById("currentStepFrequency"));
var currentMinimum = new TextBlock(FindElement.ById("currentMinimum"));
var currentRangeStart = new TextBlock(FindElement.ById("currentRangeStart"));
var currentRangeEnd = new TextBlock(FindElement.ById("currentRangeEnd"));
var currentMaximum = new TextBlock(FindElement.ById("currentMaximum"));

Verify.AreEqual("1", currentStepFrequency.GetText());
Verify.AreEqual("0", currentMinimum.GetText());
Verify.AreEqual("10", currentRangeStart.GetText());
Verify.AreEqual("90", currentRangeEnd.GetText());
Verify.AreEqual("100", currentMaximum.GetText());
}
}
}
Loading