This commit is contained in:
Jerome Laban 2021-01-27 14:48:13 -05:00
Родитель 5eac5ed355
Коммит d57879600e
1 изменённых файлов: 100 добавлений и 100 удалений

200
README.md
Просмотреть файл

@ -8,136 +8,136 @@ The testing is available through :
- Selenium for WebAssembly apps, using Chrome
- [Xamarin.UITest](https://docs.microsoft.com/en-us/appcenter/test-cloud/uitest/) and [AppCenter](https://appcenter.ms/apps) for iOS and Android apps.
The following target platforms are not yet supported:
- SkiaSharp backends (GTK, WPF and Tizen )
- Xamarin.macOS
- Windows
## Build status
| Target | Branch | Status | Recommended Nuget packages version |
| ------ | ------ | ------ | ------ |
| development | master |[![Build Status](https://dev.azure.com/uno-platform/Uno%20Platform/_apis/build/status/Uno%20Platform/Uno.UITest?branchName=master)](https://dev.azure.com/uno-platform/Uno%20Platform/_build/latest?definitionId=58&branchName=master) | [![NuGet](https://img.shields.io/nuget/v/Uno.UITest.svg)](https://www.nuget.org/packages/Uno.UITest/) |
## How to use Uno.UITest with a WebAssembly app
## How to use Uno.UITest with an Uno Platform app
- Make sure [Chrome is installed](https://www.google.com/chrome)
- In Visual Studio for Windows, [create an application](https://platform.uno/docs/articles/getting-started-tutorial-1.html) using the Uno Platform templates
- In the Wasm `Program.cs`, add the following line at the top of the `Main` function to enable automation support:
```
Uno.UI.FeatureConfiguration.UIElement.AssignDOMXamlName = true;
```
Note that if running on iOS or Android, setting a property is required:
```xml
<IsUiAutomationMappingEnabled>true</IsUiAutomationMappingEnabled>
```
- Create a .NET Standard 2.0 library, and replace its content with the following:
```xml
<Project Sdk="Microsoft.NET.Sdk">
- Add the following code to each `.csproj` files (iOS, Android and WebAssembly), at the end before the closing `</project>` tag:
```xml
<PropertyGroup Condition="'$(Configuration)'=='Debug' or '$(IsUiAutomationMappingEnabled)'=='True'">
<IsUiAutomationMappingEnabled>True</IsUiAutomationMappingEnabled>
<DefineConstants>$(DefineConstants);USE_UITESTS</DefineConstants>
</PropertyGroup>
<ItemGroup>
<!-- remove this block after Uno.UI 3.5 is released -->
<UnoSourceGeneratorAdditionalProperty Include="IsUiAutomationMappingEnabled" />
</ItemGroup>
```
- In the iOS project, add a reference to the `Xamarin.TestCloud.Agent` nuget package (0.21.8 or later)
- In the `OnLaunched` method of `App.xaml.cs`, add the following at the beginning:
```csharp
#if __IOS__ && USE_UITESTS
// Launches Xamarin Test Cloud Agent
Xamarin.Calabash.Start();
#endif
```
- Install the Uno Platform `dotnet new` templates:
<PropertyGroup>
<TargetFramework>net47</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Uno.UITest" Version="1.0.0-dev.72" />
<PackageReference Include="Uno.UITest.Helpers" Version="1.0.0-dev.72" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
</ItemGroup>
</Project>
```
- Add a file named `TestBase.cs` that will be used as a base test:
```csharp
using NUnit.Framework;
using Uno.UITest;
using Uno.UITests.Helpers;
public class TestBase
{
private IApp _app;
static TestBase()
{
// Change this to your android app name
AppInitializer.TestEnvironment.AndroidAppName = "com.example.myapp";
// Change this to the URL of your WebAssembly app, found in launchsettings.json
AppInitializer.TestEnvironment.WebAssemblyDefaultUri = "http://localhost:CHANGEME";
// Change this to the bundle ID of your app
AppInitializer.TestEnvironment.iOSAppName = "com.example.myapp";
// Change this to the iOS device you want to test on
AppInitializer.TestEnvironment.iOSDeviceNameOrId = "iPad Pro (12.9-inch) (3rd generation)";
// The current platform to test.
AppInitializer.TestEnvironment.CurrentPlatform = Uno.UITest.Helpers.Queries.Platform.Browser;
#if DEBUG
// Show the running tests in a browser window
AppInitializer.TestEnvironment.WebAssemblyHeadless = false;
#endif
// Start the app only once, so the tests runs don't restart it
// and gain some time for the tests.
AppInitializer.ColdStartApp();
}
protected IApp App { get; set; }
[SetUp]
public void StartApp()
{
// Attach to the running application, for better performance
App = AppInitializer.AttachToApp();
}
}
```
```sh
dotnet new -i Uno.ProjectTemplates.Dotnet
```
You may need to use this line if Uno 3.5 has not yet been released:
```
dotnet new -i Uno.ProjectTemplates.Dotnet::3.5-dev*
```
- Navigate to your `.sln` folder using a command line:
- Create a folder named `YourAppName\YourAppName.UITests`
- Then run :
```
cd YourAppName.UITests
dotnet new unoapp-uitest
```
The new project will be added automatically to your solution.
- In the new UI Tests project, edit the `Constants.cs` file with values that match your project
- In your application, add the following XAML:
```XAML
<StackPanel>
<CheckBox x:Uid="cb1" Content="Test 1"/>
</StackPanel>
```
```XAML
<StackPanel>
<CheckBox AutomationProperties.AutomationId="cb1" Content="Test 1"/>
</StackPanel>
```
- Then following test can be written:
```csharp
using NUnit.Framework;
using Uno.UITest.Helpers.Queries;
using System.Linq;
// Alias to simplify the creation of element queries
using Query = System.Func<Uno.UITest.IAppQuery, Uno.UITest.IAppQuery>;
```csharp
using NUnit.Framework;
using Uno.UITest.Helpers.Queries;
using System.Linq;
// Alias to simplify the creation of element queries
using Query = System.Func<Uno.UITest.IAppQuery, Uno.UITest.IAppQuery>;
public class CheckBox_Tests : TestBase
{
[Test]
public void CheckBox01()
public class CheckBox_Tests : TestBase
{
Query checkBoxSelector = q => q.Marked("cb1");
App.WaitForElement(checkBoxSelector);
[Test]
public void CheckBox01()
{
Query checkBoxSelector = q => q.Marked("cb1");
App.WaitForElement(checkBoxSelector);
Query cb1 = q => q.Marked("cb1");
App.WaitForElement(cb1);
Query cb1 = q => q.Marked("cb1");
App.WaitForElement(cb1);
var value1 = App.Query(q => cb1(q).GetDependencyPropertyValue("IsChecked").Value<bool>()).First();
Assert.IsFalse(value1);
var value1 = App.Query(q => cb1(q).GetDependencyPropertyValue("IsChecked").Value<bool>()).First();
Assert.IsFalse(value1);
App.Tap(cb1);
App.Tap(cb1);
var value2 = App.Query(q => cb1(q).GetDependencyPropertyValue("IsChecked").Value<bool>()).First();
Assert.IsTrue(value2);
var value2 = App.Query(q => cb1(q).GetDependencyPropertyValue("IsChecked").Value<bool>()).First();
Assert.IsTrue(value2);
}
}
}
```
```
This sample is provided through the [`Sample.UITests` project](https://github.com/unoplatform/Uno.UITest/tree/master/src/Sample/Sample.UITests) in this repository.
### Running the tests for WebAssembly
- To test in Chrome, first deploy the WebAssemly app using `Ctrl+F5`, take note of the Url of the app
- Update the `AppInitializer.TestEnvironment.WebAssemblyDefaultUri` property in `TestBase.cs`
- Update the `Constants.WebAssemblyDefaultUri` property in `Constants.cs`
- Change the `Constants.CurrentPlatform` to `Platform.Browser`
- [Launch a test](https://docs.microsoft.com/en-us/visualstudio/test/getting-started-with-unit-testing?view=vs-2019) by right clicking on the test in the Test Explorer, or in the test code itself.
- A Chrome browser window will open in automated mode, and the test will execute.
Note that testing for iOS is only available through Visual Studio for Mac, where the simulators can run.
### Running the tests for Android
- Build and deploy the app on a simulator
- Update the `Constants.AndroidAppName` property in `Constants.cs` to the value set in your app manifest
- Change the `Constants.CurrentPlatform` to `Platform.Android`
- [Launch a test](https://docs.microsoft.com/en-us/visualstudio/test/getting-started-with-unit-testing?view=vs-2019) by right clicking on the test in the Test Explorer, or in the test code itself.
- The application will start on the emulator, and the test will execute
### Running the tests for iOS
> testing for iOS is only available through Visual Studio for Mac, where the simulators can run.
- Open your solution in Visual Studio for mac
- Build and deploy the app on an iOS simulator
- Update the `Constants.iOSAppName` property in `Constants.cs` to the value specified in your `info.plist` file
- Change the `Constants.CurrentPlatform` to `Platform.iOS`
- [Launch a test](https://docs.microsoft.com/en-us/visualstudio/mac/testing?view=vsmac-2019)
- The application will start on the emulator, and the test will execute
This sample is provided through the [`Sample.UITests` project](https://github.com/unoplatform/Uno.UITest/tree/master/src/Sample/Sample.UITests) in this repository.
### Validating the currently running environment
```csharp
if(AppInitializer.GetLocalPlatform() == Platform.Android)
{
Assert.Ignore();
}
```
## UI Testing in a CI environment
One of the design goal of the `Uno.UITest` library is to enable UI Testing in Pull Request builds, so that the UI testing is not an afterthought, and is part of the development flow.