Skip to content

Commit

Permalink
Merge pull request #438 from dotnet/fix_android_appium_sample
Browse files Browse the repository at this point in the history
Appium Android Fixes
  • Loading branch information
jfversluis committed Feb 21, 2024
2 parents 3bc23ba + abd8c6b commit aecea8d
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicAppiumSample", "MauiApp\BasicAppiumNunitSample.csproj", "{9F570461-4215-4CF7-A157-BFEB7BDACFCE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicAppiumNunitSample", "MauiApp\BasicAppiumNunitSample.csproj", "{9F570461-4215-4CF7-A157-BFEB7BDACFCE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UITests.Android", "UITests.Android\UITests.Android.csproj", "{9C1F3C00-352A-4E7C-93E1-441E7B85DC7A}"
EndProject
Expand All @@ -26,6 +26,7 @@ Global
{9F570461-4215-4CF7-A157-BFEB7BDACFCE}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{9F570461-4215-4CF7-A157-BFEB7BDACFCE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F570461-4215-4CF7-A157-BFEB7BDACFCE}.Release|Any CPU.Build.0 = Release|Any CPU
{9F570461-4215-4CF7-A157-BFEB7BDACFCE}.Release|Any CPU.Deploy.0 = Release|Any CPU
{9C1F3C00-352A-4E7C-93E1-441E7B85DC7A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9C1F3C00-352A-4E7C-93E1-441E7B85DC7A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C1F3C00-352A-4E7C-93E1-441E7B85DC7A}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' AND $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android' ">
<EmbedAssembliesIntoApk>true</EmbedAssembliesIntoApk>
</PropertyGroup>

<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;

namespace BasicAppiumSample;

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
[Register("com.companyname.basicappiumsample.MainActivity")]
public class MainActivity : MauiAppCompatActivity
{
}
8 changes: 7 additions & 1 deletion 8.0/UITesting/BasicAppiumNunitSample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Each method is adorned with a `[Test]` attribute. This marks the method as a tes

Tests can be ran from the Visual Studio Test Explorer or by running `dotnet test` from the command-line.

To run the tests, an Appium server needs to be available and running, and that in turn should be able to reach the emulators, Simulator or physical devices as needed.
To run the tests, an Appium server needs to be available and running, and that in turn should be able to reach the emulators, Simulator or physical devices as needed. For Android, make sure to either specify the emulator to be used in the configuration, or have an emulator already running/device already connected. If no emulator/device is detected, the tests will not run and throw an exception specifying that no connected Android device could be found.

> [!NOTE]
> For all platforms apart from macOS, you typically want to have your app deployed on the device that you want to test on. Make sure you have the latest app version is deployed to your device. Tests will be ran against that app. The way this sample is set up, it will **not** deploy the app for you as part of the test run.
Expand All @@ -121,3 +121,9 @@ This sample does automatically start and stop the Appium server for you as part
If you want to start the Appium server manually, go into each `AppiumSetup.cs` file in each of the platform projects and comment out the lines that call `AppiumServerHelper`. There are two: one to start the server and one to stop it.

You will have to make sure the Appium server is started before running the tests and optionally configure the Appium drivers used in code to be able to reach your own server.

### Android Debug vs Release Build

On Android there is the concept of [Fast Deployment](https://learn.microsoft.com/xamarin/android/deploy-test/building-apps/build-process#fast-deployment) and because of that needs some special configuration when running with Appium.

In the `AppiumSetup.cs` of the `UITest.Android` project there are inline comments with instructions. However, this part of the configuration is very much dependent on how you setup your testing. If you use a Debug configuration build, evertyhing is setup as it should. When you want to test your resulting apk/aab binary file (or the Release configuration build), you will want to comment out the Debug configuration code and uncomment the Release configuration code and make sure the path to your app binary is correct.
38 changes: 27 additions & 11 deletions 8.0/UITesting/BasicAppiumNunitSample/UITests.Android/AppiumSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.Enums;

namespace UITests;

Expand All @@ -25,21 +26,36 @@ public void RunBeforeAnyTests()
AutomationName = "UIAutomator2",
// Always Android for Android
PlatformName = "Android",
// This is the Android version, not API level
// This is ignored if you use the avd option below
PlatformVersion = "13",
// The full path to the .apk file to test or the package name if the app is already installed on the device
App = "com.companyname.basicappiumsample",

// RELEASE BUILD SETUP
// The full path to the .apk file
// This only works with release builds because debug builds have fast deployment enabled
// and Appium isn't compatible with fast deployment
// App = Path.Join(TestContext.CurrentContext.TestDirectory, "../../../../MauiApp/bin/Release/net8.0-android/com.companyname.basicappiumsample-Signed.apk"),
// END RELEASE BUILD SETUP
};

// Specifying the avd option will boot the emulator for you
// make sure there is an emulator with the name below
// If not specified, make sure you have an emulator booted
//androidOptions.AddAdditionalAppiumOption("avd", "pixel_5_-_api_33");
// DEBUG BUILD SETUP
// If you're running your tests against debug builds you'll need to set NoReset to true
// otherwise appium will delete all the libraries used for Fast Deployment on Android
// Release builds have Fast Deployment disabled
// https://learn.microsoft.com/xamarin/android/deploy-test/building-apps/build-process#fast-deployment
androidOptions.AddAdditionalAppiumOption(MobileCapabilityType.NoReset, "true");
androidOptions.AddAdditionalAppiumOption(AndroidMobileCapabilityType.AppPackage, "com.companyname.basicappiumsample");

//Make sure to set [Register("com.companyname.basicappiumsample.MainActivity")] on the MainActivity of your android application
androidOptions.AddAdditionalAppiumOption(AndroidMobileCapabilityType.AppActivity, $"com.companyname.basicappiumsample.MainActivity");
// END DEBUG BUILD SETUP


// Specifying the avd option will boot the emulator for you
// make sure there is an emulator with the name below
// If not specified, make sure you have an emulator booted
//androidOptions.AddAdditionalAppiumOption("avd", "pixel_5_-_api_33");

// Note there are many more options that you can use to influence the app under test according to your needs
// Note there are many more options that you can use to influence the app under test according to your needs

driver = new AndroidDriver(androidOptions);
driver = new AndroidDriver(androidOptions);
}

[OneTimeTearDown]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\UITests.Shared\**\*.cs" LinkBase="Shared" Visible="false" />
<Compile Include="..\UITests.Shared\**\*.cs" LinkBase="Shared" Visible="false" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="Appium.WebDriver" Version="5.0.0-rc.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\UITests.Shared\**\*.cs" LinkBase="Shared" Visible="false" />
<Compile Include="..\UITests.Shared\**\*.cs" LinkBase="Shared" Visible="false" />
</ItemGroup>

</Project>

0 comments on commit aecea8d

Please sign in to comment.