Skip to content

.NET Example 1

Goal to Achieve

Do login with invalid credentials on AgileTV app on an Android Smartphone using .NET and take a screenshot.

AgileTV

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

Project Setup

  1. Create a new .NET MSTest project. Follow the instructions here.
  2. Add the Appium.WebDriver nuget package as described here.
  3. Your .csproj file 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>
  4. Replace the content of UnitTest1.cs with the following code, and rename the file to AgileTvLoginTests.cs:
    csharp
    using 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