.NET Example 1
Goal to Achieve
Do login with invalid credentials on AgileTV app on an Android Smartphone using .NET and take a screenshot.

Steps to reproduce:
- Unlock the Device using PIN.
- Install AgileTV app from apk file.
- Open AgileTV app.
- Wait until username input appears on screen.
- Fill username, password and click login button.
- Take a screenshot
Prerequisites
- Common environment steps to be completed.
- Android environment steps to be completed.
- .NET SDK installed on the computer. For more details, check the .NET Tips.
- A connected Smartphone, as described here.
- Appium server running, as described here.
- An AgileTV app APK file.
Project Setup
- Create a new .NET MSTest project. Follow the instructions here.
- Add the
Appium.WebDrivernuget package as described here. - Your
.csprojfile should look like this:xml<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>net8.0</TargetFramework> <LangVersion>latest</LangVersion> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Appium.WebDriver" Version="8.0.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> <PackageReference Include="MSTest" Version="3.6.4" /> </ItemGroup> <ItemGroup> <Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" /> </ItemGroup> </Project> - Replace the content of
UnitTest1.cswith the following code, and rename the file toAgileTvLoginTests.cs:csharpusing OpenQA.Selenium; using OpenQA.Selenium.Appium; using OpenQA.Selenium.Appium.Android; namespace AgileTvAppium.Tests; [TestClass] public sealed class AgileTvLoginTests { private AndroidDriver? driver; [TestInitialize] public void SetUp() { var options = new AppiumOptions { PlatformName = "Android", AutomationName = "UiAutomator2", App = @"C:\Users\mauro\Downloads\Agile_TV_mmMobileProdStd_release_21541.apk" // <-- Change this to your app's path }; options.AddAdditionalAppiumOption("udid", "00000000000"); // <-- Change this to your device's ID options.AddAdditionalAppiumOption("autoGrantPermissions", true); options.AddAdditionalAppiumOption("autoUnlock", true); options.AddAdditionalAppiumOption("unlockType", "pin"); options.AddAdditionalAppiumOption("unlockKey", "123456"); // <-- Change this to your device's PIN var serverUri = new Uri("http://localhost:4723/"); driver = new AndroidDriver(serverUri, options, TimeSpan.FromMinutes(1)); driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30); } [TestCleanup] public void TearDown() { driver?.Quit(); } [TestMethod] public void Login_TakesScreenshot() { if (driver is null) throw new InvalidOperationException("Driver not initialized"); string idPrefix = "com.agilecontent.agiletv.mobile:id"; driver.FindElement(By.Id($"{idPrefix}/login_user_input")).SendKeys("testuser"); driver.FindElement(By.Id($"{idPrefix}/login_password_input")).SendKeys("p4ssw0rd"); driver.FindElement(By.Id($"{idPrefix}/primary_button_view")).Click(); Thread.Sleep(8000); var screenshot = ((ITakesScreenshot)driver).GetScreenshot(); screenshot.SaveAsFile("screenshot.png"); } }
Capabilities
Before running, you need to adjust the AppiumOptions:
- udid:
Replace'00000000000'with the ID of your connected device.
You can find this ID as explained here. - unlockKey:
Replace'123456'with the PIN of the device you are using for the test. - App:
Replace the path to your APK file.
Running the test
To run the test, execute the following command in your terminal:
shell
dotnet test