Skip to content

UITests

dustin-wojciechowski edited this page Aug 5, 2024 · 46 revisions

Prerequisites

To run the tests on iOS or Catalyst, you'll need a Mac. To run the tests on Windows, you'll need a Windows machine. Android tests can be run from either platform.

Structure

├──  Controls
│    ├── tests
│    ├── ├── Controls.TestCases.App
│    │   ├── Controls.TestCases.Shared.Tests
  • Controls.TestCases.App: .NET MAUI Sample used for the automated UI tests
  • Controls.TestCases.Shared.Tests: .NET MAUI library used to define the UI tests

Each platform has a specific UI tests library project where it is possible to add specific tests per platform.

├──  Controls
│    ├── tests
│    │   ├── Controls.TestCases.Android.Tests
│    │   ├── Controls.TestCases.iOS.Tests
│    │   ├── Controls.TestCases.Mac.Tests
│    │   ├── Controls.TestCases.WinUI.Tests

Writing Tests

https://github.com/dotnet/maui/blob/main/docs/design/UITesting.md

Windows

For Mac

  • make sure to run dotnet tool restore on the repo home

Option 1

Option 2 (HomeBrew)

Android prerequisites

  • You will need to setup your ANDROID_HOME environment variable so that Appium is able to find it.

  • Ensure that you have the Android API 30 SDK installed, with the emulator image for Play store on x86/x64. By default, the tests use that. See screenshot below. If you want to use another image you can specify the --device argument, with something like --device="android-emulator-64_33" where the 64 means image x86_x64 and 33 indicates the API level.

image

Running from the command line (you can replace ./build.ps1 by dotnet cake)

./build.ps1 --target=dotnet-buildtasks --configuration="Release" --workloads=global

Android

./build.ps1 --target=dotnet-samples --configuration="Release" --workloads=global --android --usenuget=false
./build.ps1 -Script eng/devices/android.cake --target=uitest --configuration="Release"

Windows

./build.ps1 --target=dotnet-samples --configuration="Release" --windows --verbosity=diagnostic --workloads=global --usenuget=false
./build.ps1 -Script eng/devices/windows.cake --target=uitest --device="windows10" --apiversion="10.0.20348" --configuration="Release"

iOS

./build.ps1 --target=dotnet-samples --configuration="Release" --workloads=global --ios --usenuget=false
./build.ps1 -Script eng/devices/ios.cake --target=uitest --device=ios-simulator-64 --apiversion="16.2" --configuration="Release"

Catalyst

./build.ps1 --target=dotnet-samples --configuration="Release" --catalyst --verbosity=diagnostic --workloads=global --usenuget=false
./build.ps1 -Script eng/devices/catalyst.cake --target=uitest --apiversion="10.13" --configuration="Release" --device=mac

Running from the IDE

  • Deploy the app Control.Sample.UITests to your simulator, device or on your desktop.
  • Build the Controls.AppiumTests.csproj so the tests show up on the Test Explorer
  • Run the tests

Running from Visual Studio Code (requires C# Dev Kit with .NET MAUI Extension installed)

  • You'll want to deploy the app first. Go to the Run and Debug window and select the correct project:
C# Controls TestCases HostApp

However, this will just load the test in the app. To have Appium drive the test, you’ll need to run them in the Testing pane in VS Code. Before that, make sure to build the project where your test is located:

  1. Ensure that you are in Explorer.
  2. Expand the Solution Explorer
  3. Find the project that your test is located in.
  4. Right click and select build.
Pasted Graphic 2

Now if you click on the Testing icon, you will see the test that you were working on. Click the play icon to launch your test:

• GestureRecognizerUlTests(Android)

Running Xamarin.UITests on Compatibility Gallery

Android

./build.ps1 --target=dotnet-legacy-controlgallery --configuration="Release" --android --verbosity=diagnostic --workloads=global
./build.ps1 -Script eng/devices/android.cake --target=cg-uitest --project="/Users/ruimarinho/dotnet/maui/src/Compatibility/ControlGallery/test/Android.UITests/Compatibility.ControlGallery.Android.UITests.csproj" --appproject="/Users/ruimarinho/dotnet/maui/src/Compatibility/ControlGallery/src/Android/Compatibility.ControlGallery.Android.csproj" --configuration="Release"

iOS

dotnet cake eng/devices/ios.cake --target=cg-uitest --project="./maui/src/Compatibility/ControlGallery/test/iOS.UITests/Compatibility.ControlGallery.iOS.UITests.csproj" --appproject="./maui/src/Compatibility/ControlGallery/src/iOS/Compatibility.ControlGallery.iOS.csproj" --where="cat=FailsOnMauiiOS" --workloads=global

Troubleshooting

The local appium server has not been started

  • Try starting appium from the command line to get a better error message node /usr/local/lib/node_modules/appium/build/lib/main.js
Clone this wiki locally