Skip to main content

C#

Continuous Testing Cloud lets you execute Appium tests written in C# on remotely located devices.  This page has everything you need on order to run Appium tests using C#. It lists requirements, prerequisites and two sample tests hosted in a git repository - one for iOS and one for Android.

You can use the sample tests in the git repository by cloning them, or use the sample tests.

info

Replace <server> with the appropriate URL.

Android App Test with C Sharp Expand source

using System;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Appium.Android;
using NUnit;
using NUnit.Framework;
using System.Collections.Generic;
using OpenQA.Selenium.Appium.Enums;
using OpenQA.Selenium.Appium.Service;
using OpenQA.Selenium.Appium.Service.Options;

namespace AppiumCsharpFirstTest
{
[TestFixture]
public class AndroidAppTest
{
// you have configured an access key as environment variable,
// use the line below. Otherwise, specify the key directly.
private string accessKey = Environment.GetEnvironmentVariable("SEETEST_IO_ACCESS_KEY");
private string testName = "Android App Test with C#";

protected AndroidDriver<AndroidElement> driver = null;

protected AppiumOptions caps = new AppiumOptions();

[SetUp()]
public void SetupTest()
{
caps.AddAdditionalCapability("testName", testName);
caps.AddAdditionalCapability("accessKey", accessKey);
caps.AddAdditionalCapability("deviceQuery", "@os='android'");
caps.AddAdditionalCapability("platformName", "Android");
caps.AddAdditionalCapability("app", "cloud:com.experitest.ExperiBank/.LoginActivity");
caps.AddAdditionalCapability("appPackage", "com.experitest.ExperiBank");
caps.AddAdditionalCapability("appActivity", ".LoginActivity");

driver = new AndroidDriver<AndroidElement>(new Uri("<server>"), caps);
}

[Test()]
public void TestAndroidApp()
{
driver.FindElement(By.XPath("xpath=//*[@id='usernameTextField']")).SendKeys("company");
driver.FindElement(By.XPath("xpath=//*[@id='passwordTextField']")).SendKeys("company");
driver.FindElement(By.XPath("xpath=//*[@id='loginButton']")).Click();
driver.FindElement(By.XPath("xpath=//*[@id='makePaymentButton']")).Click();
driver.FindElement(By.XPath("xpath=//*[@id='phoneTextField']")).SendKeys("123456");
driver.FindElement(By.XPath("xpath=//*[@id='nameTextField']")).SendKeys("Test");
driver.FindElement(By.XPath("xpath=//*[@id='amountTextField']")).SendKeys("10");
driver.FindElement(By.XPath("xpath=//*[@id='countryTextField']")).SendKeys("US");
driver.FindElement(By.XPath("xpath=//*[@id='sendPaymentButton']")).Click();
driver.FindElement(By.XPath("xpath=//*[@id='button1']")).Click();
}

[TearDown()]
public void TearDown()
{
if (driver != null)
{
Console.WriteLine(driver.Capabilities.GetCapability("reportUrl"));
driver.Quit();
}
}
}
}

IOS App Test with C Sharp Expand source

using System;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium.iOS;
using NUnit.Framework;

namespace AppiumCsharpFirstTest
{
public class iOSAppTest
{

// you have configured an access key as environment variable,
// use the line below. Otherwise, specify the key directly.
private string accessKey = Environment.GetEnvironmentVariable("SEETEST_IO_ACCESS_KEY");
private string testName = "iOS App Test with C#";

protected IOSDriver<IOSElement> driver = null;

protected AppiumOptions caps = new AppiumOptions();

[SetUp()]
public void SetupTest()
{
caps.AddAdditionalCapability("testName", testName);
caps.AddAdditionalCapability("accessKey", accessKey);
caps.AddAdditionalCapability("deviceQuery", "@os='ios'");
caps.AddAdditionalCapability("platformName", "iOS");
caps.AddAdditionalCapability("app", "cloud:com.experitest.ExperiBank");
caps.AddAdditionalCapability("bundleId", "com.experitest.ExperiBank");

driver = new IOSDriver<IOSElement>(new Uri("<server>"), caps);
}

[Test()]
public void TestiOSApp()
{
driver.FindElement(By.XPath("xpath=//*[@id='usernameTextField']")).SendKeys("company");
driver.FindElement(By.XPath("xpath=//*[@id='passwordTextField']")).SendKeys("company");
driver.FindElement(By.XPath("xpath=//*[@id='loginButton']")).Click();
}

[TearDown()]
public void TearDown()
{
if (driver != null)
{
Console.WriteLine(driver.Capabilities.GetCapability("reportUrl"));
driver.Quit();
}
}

}
}

Chrome on Android Test with C Sharp Expand source

using System;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Appium.Android;
using NUnit;
using NUnit.Framework;
using System.Collections.Generic;
using OpenQA.Selenium.Appium.Enums;
using OpenQA.Selenium.Appium.Service;
using OpenQA.Selenium.Appium.Service.Options;

namespace AppiumCsharpFirstTest
{
[TestFixture]
public class AndroidWebTest
{
// you have configured an access key as environment variable,
// use the line below. Otherwise, specify the key directly.
private string accessKey = Environment.GetEnvironmentVariable("SEETEST_IO_ACCESS_KEY");
private string testName = "Android Web Test with C#";

protected AndroidDriver<AndroidElement> driver = null;

protected AppiumOptions caps = new AppiumOptions();

[SetUp()]
public void SetupTest()
{
caps.AddAdditionalCapability("testName", testName);
caps.AddAdditionalCapability("accessKey", accessKey);
caps.AddAdditionalCapability("deviceQuery", "@os='android'");
caps.AddAdditionalCapability("platformName", "Android");
caps.AddAdditionalCapability("browserName", "chrome");

driver = new AndroidDriver<AndroidElement>(new Uri("<server>"), caps);
}

[Test()]
public void TestAndroidApp()
{
driver.Navigate().GoToUrl("https://amazon.com");
if (driver.Capabilities.GetCapability("device.category").Equals("TABLET"))
{
driver.FindElement(By.XPath("//*[@name='field-keywords']")).SendKeys("iPhone");
driver.FindElement(By.XPath("//*[@text='Go']")).Click();
}
else
{
driver.FindElement(By.XPath("//*[@name='k']")).SendKeys("iPhone");
driver.FindElement(By.XPath("//*[@value='Go']")).Click();
}
}

[TearDown()]
public void TearDown()
{
if(driver != null)
{
Console.WriteLine(driver.Capabilities.GetCapability("reportUrl"));
driver.Quit();
}

}
}
}

Safari on IOS Test with C Sharp Expand source

using System;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Appium.iOS;
using NUnit.Framework;
using OpenQA.Selenium.Appium.Enums;

namespace AppiumCsharpFirstTest
{
[TestFixture]
public class IOSWebTest
{
// you have configured an access key as environment variable,
// use the line below. Otherwise, specify the key directly.
private string accessKey = Environment.GetEnvironmentVariable("SEETEST_IO_ACCESS_KEY");
private string testName = "iOS Safari Web Test";

protected IOSDriver<IOSElement> driver = null;

protected AppiumOptions caps = new AppiumOptions();

[SetUp()]
public void SetupTest()
{
caps.AddAdditionalCapability("testName", testName);
caps.AddAdditionalCapability("accessKey", accessKey);
caps.AddAdditionalCapability("deviceQuery", "@os='ios'");
caps.AddAdditionalCapability("platformName", "iOS");
caps.AddAdditionalCapability("browserName", "safari");
caps.AddAdditionalCapability("autoDismissAlerts", true);

driver = new IOSDriver<IOSElement>(new Uri("<server>"), caps);
}

[Test()]
public void TestiOSApp()
{
driver.Navigate().GoToUrl("https://amazon.com");
if (driver.Capabilities.GetCapability("device.category").Equals("TABLET"))
{
driver.FindElement(By.XPath("//*[@name='field-keywords']")).SendKeys("iPhone");
driver.FindElement(By.XPath("//*[@text='Go']")).Click();
}
else
{
driver.FindElement(By.XPath("//*[@name='k']")).SendKeys("iPhone");
driver.FindElement(By.XPath("//*[@value='Go']")).Click();
}
}

[TearDown()]
public void TearDown()
{
if (driver != null)
{
Console.WriteLine(driver.Capabilities.GetCapability("reportUrl"));
driver.Quit();
}
}
}
}

Prerequisites

These samples require using the Visual Studio Community Edition.

tip

Before you proceed with fetching the sample tests:

  1. If you plan to test a native or hybrid app, upload your app to your project.

  2. Fetch your access key.

Getting Started

Appium requires you to fetch and integrate Appium Dependencies, also known as the Appium C# Client Libraries.  The git project is a packages.config file that takes care of adding Appium C# Client libraries and NUnit (unit testing for C#). 

Now you can clone the repository to your Visual Studio project.

tip

If you are having issues with obtaining the requires packages, run the following commands in Visual Studio Package Manager Console: To get Selenium:

Install-Package Selenium.Support

To get Appium:

Install-Package Appium.WebDriver

To get NUnit:

Install-Package NUnit
Install-Package NUnit3TestAdapter
Install-Package NUnit.Console