Skip to main content

Appium with NUnit

In this documentation, you will learn how to trigger a automation script of NUnit for application testing with Appium on LambdaTest, set the desired capabilities for appium testing, and other advanced features of LambdaTest.

Prerequisites

  • Your LambdaTest Username and Access key.
  • Install the MS Visual Studio 2013 or later version for C#. We recommend using the latest version.
  • Install the framework NUnit3.0, and NuGet plugin for Visual Studio and add the NuGet CLI executable installed in your path.
  • Access to an Android app (.apk or .aab file) or an iOS app (.ipa file).

Install and Setup the Dependencies

  • Install the NuGet packages for the project:
nuget.exe install ..\NUnitSelenium\packages.config
  • Clean and rebuild the project
nmake clean build

Try our Sample Repository

Step 1: Get a Sample Project

You can use your own project to configure and test it. For demo purposes, we are using the sample repository.

Sample repo

All the code samples in this documentation can be found on LambdaTest's Github Repository. You can either download or clone the repository to quickly run your tests. Image View on GitHub

Open the Android/iOS project using the file with a .sln extension.

Step 2: Setup the Environment Variables

You need to export your environment variables LT_USERNAME and LT_ACCESS_KEY that are available in your LambdaTest Profile page. Run the below mentioned commands in your terminal to setup the environment variables.

export LT_USERNAME="undefined"
export LT_ACCESS_KEY="undefined"

Step 3: Upload your Application

Upload your iOS application (.ipa file) or android application (.apk or .aab file) to the LambdaTest servers using our REST API. You need to provide your Username and AccessKey in the format Username:AccessKey in the cURL command for authentication.

Make sure to add the path of the appFile in the cURL request. Below is an example cURL request to upload your app using our REST API:

curl -u "undefined:undefined" -X POST "https://manual-api.lambdatest.com/app/upload/realDevice" -F "appFile=@"/Users/macuser/Downloads/proverbial_android.apk"" -F "name="proverbial_app""
tip
  • If you do not have any .apk or .ipa file, you can run your sample tests on LambdaTest by using our sample apps, 🔗 Android app or 🔗 iOS app.

  • Response of above cURL will be a JSON object containing the APP_URL of the format - lt://APP123456789123456789 and will be used in the next step

Step 4: Update your Automation Script

An automation script for the sample application given above has been provided here. Ensure to update the APP_URL, username and accessKey in the code scripts before running the tests.

NUnitAppiumTests.cs
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

using NUnit.Framework;
using System.Threading;
using System.Collections.Generic;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Appium.iOS;
using OpenQA.Selenium.Support.UI;

namespace NUnitSelenium
{
//-------------------Running two parallel test cases----------------------------
[TestFixture("OnePlus 6", "8", "Android", "APP_URL")] // Android Testing
[TestFixture("iPhone 11", "14", "iOS", "APP_URL")] // iOS testing
[Parallelizable(ParallelScope.Fixtures)]
public class NUnitSeleniumSample
{
//--------------------We can initialize username and access Key with hub url to authenticate our test script-------------------------------------
public static string LT_USERNAME = Environment.GetEnvironmentVariable("LT_USERNAME") ==null ? "your username" : Environment.GetEnvironmentVariable("LT_USERNAME");
public static string LT_ACCESS_KEY = Environment.GetEnvironmentVariable("LT_ACCESS_KEY") == null ? "your accessKey" : Environment.GetEnvironmentVariable("LT_ACCESS_KEY");
public static bool tunnel = Boolean.Parse(Environment.GetEnvironmentVariable("LT_TUNNEL")== null ? "false" : Environment.GetEnvironmentVariable("LT_TUNNEL"));
public static string build = Environment.GetEnvironmentVariable("LT_BUILD") == null ? "your build name" : Environment.GetEnvironmentVariable("LT_BUILD");
public static string seleniumUri = "https://mobile-hub.lambdatest.com:443/wd/hub";

//-------------------------Initialization of Driver--------------------------
AndroidDriver<AndroidElement> driver;

// Initialization some parameter
private String deviceName;
private String platformVersion;
private String platformName;
private String app;

public NUnitSeleniumSample(String deviceName, String platformVersion, String platformName, String app)
{
this.deviceName = deviceName;
this.platformVersion = platformVersion;
this.platformName = platformName;
this.app = app;
}

[SetUp]
public void Init()
{
//-----------------------------------Create instance for passing capabilities-----------------------------------------------------------------
AppiumOptions capabilities = new AppiumOptions();
capabilities.AddAdditionalCapability("user", "LT_USERNAME"); //Add LambdaTest username here
capabilities.AddAdditionalCapability("accessKey", "LT_ACCESS_KEY"); //Add LambdaTest accessKey here
capabilities.AddAdditionalCapability("app",app);
capabilities.AddAdditionalCapability("deviceName", deviceName);
capabilities.AddAdditionalCapability("platformVersion", platformVersion);
capabilities.AddAdditionalCapability("platformName", platformName);
capabilities.AddAdditionalCapability("build", "Csharp NUnit");
capabilities.AddAdditionalCapability("name", "NUnit Test");
capabilities.AddAdditionalCapability("isRealMobile", true);

driver = new AndroidDriver<AndroidElement> (new Uri(seleniumUri), capabilities, TimeSpan.FromSeconds(600));

// Console.Out.WriteLine(driver);
Console.Out.WriteLine("On Which Device/Platform test is running:"+deviceName+" "+platformVersion+" "+platformName);

}

[Test]
public void Todotest()
{
{
//----------------------Text Color Changes---------------------------------
Console.WriteLine("1.Text Color Changes");
AndroidElement searchElement = (AndroidElement)new WebDriverWait(
driver, TimeSpan.FromSeconds(20)).Until(
SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(
MobileBy.Id("color"))
);
System.Threading.Thread.Sleep(1000);
searchElement.Click();
System.Threading.Thread.Sleep(1000);
searchElement.Click();

System.Threading.Thread.Sleep(1000);

//----------------------Text Changes by clicking a button---------------------------------
Console.WriteLine("2.Text Changes by clicking a button");

AndroidElement changeelement = (AndroidElement)new WebDriverWait(
driver, TimeSpan.FromSeconds(10)).Until(
SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(
MobileBy.Id("Text"))
);
changeelement.Click();

System.Threading.Thread.Sleep(1000);

//----------------------Toast---------------------------------
Console.WriteLine("3.Toast");

AndroidElement toast = (AndroidElement)new WebDriverWait(
driver, TimeSpan.FromSeconds(10)).Until(
SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(
MobileBy.Id("toast"))
);
toast.Click();

System.Threading.Thread.Sleep(1000);

//----------------------Notification By clicking a button---------------------------------
Console.WriteLine("4.Notification Button clicked");

AndroidElement Notification = (AndroidElement)new WebDriverWait(
driver, TimeSpan.FromSeconds(10)).Until(
SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(
MobileBy.Id("notification"))
);
Notification.Click();
System.Threading.Thread.Sleep(2000);

//----------------------Geolocation button---------------------------------
Console.WriteLine("5.Geolocation");

AndroidElement geolocation = (AndroidElement)new WebDriverWait(
driver, TimeSpan.FromSeconds(10)).Until(
SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(
MobileBy.Id("geoLocation"))
);
geolocation.Click();
System.Threading.Thread.Sleep(4000);
driver.PressKeyCode(AndroidKeyCode.Back);
System.Threading.Thread.Sleep(1000);

//----------------------Speed Test Button---------------------------------
Console.WriteLine("6.Speed Test Button Clicked");

AndroidElement speed = (AndroidElement)new WebDriverWait(
driver, TimeSpan.FromSeconds(10)).Until(
SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(
MobileBy.Id("speedTest"))
);
speed.Click();
System.Threading.Thread.Sleep(5000);
driver.PressKeyCode(AndroidKeyCode.Back);
System.Threading.Thread.Sleep(1000);


//----------------------Browser Button---------------------------------
// Console.WriteLine("Browser Button Clicked");

AndroidElement BROWSER = (AndroidElement)new WebDriverWait(
driver, TimeSpan.FromSeconds(30)).Until(
SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(
MobileBy.XPath("//android.widget.FrameLayout[@content-desc=\"Browser\"]/android.widget.FrameLayout/android.widget.ImageView"))

);
BROWSER.Click();


AndroidElement url = (AndroidElement)new WebDriverWait(
driver, TimeSpan.FromSeconds(10)).Until(
SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(
MobileBy.Id("url"))
);

url.Click();

url.SendKeys("www.lambdatest.com");

System.Threading.Thread.Sleep(1000);

driver.PressKeyCode(AndroidKeyCode.Back);

System.Threading.Thread.Sleep(3000);
}
}

[TearDown]
public void Cleanup()

{

bool passed = TestContext.CurrentContext.Result.Outcome.Status == NUnit.Framework.Interfaces.TestStatus.Passed;
try
{
//-----------------Marking Test status passed or failed -----------------------------------------
((IJavaScriptExecutor)driver).ExecuteScript("lambda-status=" + (passed ? "passed" : "failed"));

}
finally
{
//---------------------Quit the session-----------------------

driver.Quit();
}
}
}
}

Step 5: Configure the Test Capabilities

You can update your custom capabilities in test scripts. In this sample project, we are passing platform name, platform version, device name and app url (generated earlier) along with other capabilities like build name and test name via capabilities object.

The capabilities object in the sample code are defined as:

AppiumOptions capabilities = new AppiumOptions();
capabilities.AddAdditionalCapability("user", "LT_USERNAME"); //Add LambdaTest username here
capabilities.AddAdditionalCapability("accessKey", "LT_ACCESS_KEY"); //Add LambdaTest accessKey here
capabilities.AddAdditionalCapability("app",app);
capabilities.AddAdditionalCapability("deviceName", deviceName);
capabilities.AddAdditionalCapability("platformVersion", platformVersion);
capabilities.AddAdditionalCapability("platformName", platformName);
capabilities.AddAdditionalCapability("build", "Csharp NUnit");
capabilities.AddAdditionalCapability("name", "NUnit Test");
capabilities.AddAdditionalCapability("isRealMobile", true);
info

Step 6: Execute and Monitor your Tests

Run the following command in your project directory to execute your build and run the tests parallelly.

nmake all

OR

Go to Build menu in Visual Studio Code menu bar and click on Build Solution. After the solution is built navigate built navigate to Test menu and click on Test All to execute the tests.

Your test results would be displayed on the test console (or CLI if you are using terminal/cmd) and on the LambdaTest App Automation Dashboard.

Reference Guides

Test across 3000+ combinations of browsers, real devices & OS.

Book Demo

Help and Support

Related Articles