Header logo Open Menu Close Menu
  • Live
  • Automate
  • App Live
  • App Automate
  • More
    • Enterprise
    • Screenshots
    • Responsive
  • Pricing
  • Resources Resources
      • Appium
      • Espresso
      • XCUITest
      • Languages
      • Java
      • C#
      • Python
      • Ruby
      • NodeJS
      • Frameworks
        • JUnit
        • Cucumber
        • Behave
        • NUnit
        • Lettuce
        • TestNG
        • Webdriverio
      • Documentation
        • Capabilities
        • Security
        • Appium Features
        • Local Testing
        • Device Coverage
        • Rest API
        • App Automate Features
      • Get Started
      • Rest API
      • Documentation
        • Local Testing
        • Security
        • Device Coverage
        • App Automate Features
        • Espresso Features
      • Get Started
      • Rest API
      • Documentation
        • Local Testing
        • Security
        • Device Coverage
        • App Automate Features
        • XCUITest Features
    • BrowserStack for Open Source
      Learn More
      • Features
      • Live Features
      • Browsers & Platforms
      • Developer Tools
      • Local Testing
      • Security
      • Mobile
      • Test on Right Devices
      • Mobile Features
    • BrowserStack for Open Source
      Learn More
  • Sign in
  • Free Trial
  • Resources
  • Features
  • Mobile
  • Features
  • Live Features
  • Browsers & Platforms
  • Developer Tools
  • Local Testing
  • Security
  • Enterprise Features
  • Open Source
  • Mobile
  • Test on Right Devices
  • Mobile Features

Support App Automate C#

C#

Documentation for running Appium automated mobile app tests with BrowserStack.

Getting Started

BrowserStack supports Appium automated mobile app tests using C#, and running your tests on our cloud setup is simple and straightforward. Follow 3 easy steps to get started with your first Appium test on BrowserStack cloud.

Step 1: Setup environment

Install the following libraries:

//Download appium dotnet-driver from  https://github.com/appium/appium-dotnet-driver
//Extract the libraries to the folder of your convenience
Step 2: Upload your app

Upload your Android app (.apk file) or iOS app (.ipa file) to the BrowserStack servers using the REST API.

curl -u "USERNAME:ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/upload" \
-F "file=@/path/to/app/file/Application-debug.apk"

Please note the App URL (bs://<hashed appid>) returned in the response of this call:

{"app_url":"bs://<hashed appid>"}

Note: If you do not have an .apk or .ipa file and are looking to simply try App Automate, you can download our Android sample app or iOS sample app and upload to the BrowserStack servers using the above API. Complete list of REST APIs can be found here.

Note:

  1. App upload will take few seconds to about a minute depending on the size of your app. Do not interrupt the curl command until you get the response back.
  2. We will delete the uploaded app after 30 days from the date of upload.

Define Custom Id for your app:

If you would like to set a constant value in your 'app' capability instead updating your test code to change the App URL everytime you upload an app, define a Custom Id for your apps. Use the same Custom Id for every build you upload. Custom Id is optional.

curl -u "USERNAME:ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/upload" \
-F "file=@/path/to/app/file/Application-debug.apk" \
-F "data={\"custom_id\": \"MyApp\"}"

Sample response of the API call

{"custom_id":"MyApp", "app_url":"bs://<hashed appid>", "shareable_id":"USERNAME/MyApp"}

You can use either of the above values in the 'app' capability of your test.

custom_id: If you use custom_id value in the 'app' capability, Appium will pick the latest app uploaded by you using that custom_id. For example, if you upload 3 different builds of your app using the same custom_id, and if you are using custom_id in your 'app' capability, Appium will pick the last uploaded build and execute your test.

shareable_id: If you would like some other user with in your group to run test using the app uploaded by you, provide the shareable_id to that user. The user can set shareable_id value in the 'app' capability and run the test.

Note: Complete list of REST API's can be found here. Use browserstack.app_version capability if you would like to test older version of apps uploaded under the custom_id. Refer Capabilities page for more details.

Step 3: Configure and run test

Copy sample code provided in C# for Android and iOS. Update the desired capability "app" with the App URL returned from the above API call.

  • Android
  • iOS

If you are using our Sample App, the sample test below will install the Sample App (Wikipedia App) on the device, search for 'browserstack' and asserts for the list of results. If you are using your own app, modify the code as per your test cases. Copy the code below into your editor, and run the test from the command-line interface.

Note: Refer our sample repo for csharp on Github: csharp-appium-app-browserstack

using System;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using System.Collections.Generic;
 
namespace BrowserStackAppiumSingleTest
{
    class MainClass
    {
 
        readonly static string userName = "USERNAME";
        readonly static string accessKey = "ACCESS_KEY";
 
 
        public static void Main(string[] args)
        {
            DesiredCapabilities caps = new DesiredCapabilities();
            caps.SetCapability("browserstack.user", userName);
            caps.SetCapability("browserstack.key", accessKey);
 
            caps.SetCapability("device", "Google Pixel");
            caps.SetCapability("app", "bs://<hashed app-id>");
 
            AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new Uri("http://hub-cloud.browserstack.com/wd/hub"), caps);
            AndroidElement searchElement = (AndroidElement)new WebDriverWait(driver,TimeSpan.FromSeconds(30)).Until(
                ExpectedConditions.ElementToBeClickable(MobileBy.AccessibilityId("Search Wikipedia"))
            );
            searchElement.Click();
            AndroidElement insertTextElement = (AndroidElement)new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(
                ExpectedConditions.ElementToBeClickable(MobileBy.Id("org.wikipedia.alpha:id/search_src_text"))
            );
            insertTextElement.SendKeys("BrowserStack");
            System.Threading.Thread.Sleep(5000);
 
            IReadOnlyList<AndroidElement> allTextViewElements =  driver.FindElementsByClassName("android.widget.TextView");
            Console.WriteLine(allTextViewElements.Count() > 0);
 
            driver.Quit();
        }
    }
}

If you are using our iOS Sample App, the sample test below will install the Sample App (WordPress App) on the device, navigate to the Login screen, enters the login email and check whether the email is registered on WordPress. If you are using your own app, modify the code as per your test cases. Copy the code below into your editor, and run the test from the command-line interface.

Note: Refer our sample repo for csharp on Github: csharp-appium-app-browserstack

using System;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.iOS;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using System.Collections.Generic;
 
namespace BrowserStackAppiumSingleTest
{
    class MainClass
    {
 
        readonly static string userName = "USERNAME";
        readonly static string accessKey = "ACCESS_KEY";
 
 
        public static void Main(string[] args)
        {
            DesiredCapabilities caps = new DesiredCapabilities();
            caps.SetCapability("browserstack.user", userName);
            caps.SetCapability("browserstack.key", accessKey);
 
            caps.SetCapability("device", "iPhone 7");
            caps.SetCapability("app", "bs://<hashed app-id>");
            IOSDriver<IOSElement> driver = new IOSDriver<IOSElement> (new Uri("http://hub-cloud.browserstack.com/wd/hub"), caps);
 
            IOSElement loginButton = (IOSElement) new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(
                ExpectedConditions.ElementToBeClickable(MobileBy.AccessibilityId("Log In"))
            );
            loginButton.Click();
            IOSElement emailTextField = (IOSElement)new WebDriverWait(driver, TimeSpan.FromSeconds(30)).Until(
                ExpectedConditions.ElementToBeClickable(MobileBy.AccessibilityId("Email address"))
            );
            emailTextField.SendKeys("hello@browserstack.com");
 
            driver.FindElementByAccessibilityId("Next").Click();
            System.Threading.Thread.Sleep(5000);
 
 
            IReadOnlyList<IOSElement> textElements = driver.FindElementsByXPath("//XCUIElementTypeStaticText");
 
            String matchedString = "";
            foreach(IOSElement textElement in textElements)
            {
                try {
                  String textContent = textElement.Text;
                  if (textContent.Contains("not registered"))
                  {
                      matchedString = textContent;
                  }
                }
                catch (NullReferenceException) {
                  continue;
                }
            }
 
            Console.WriteLine(matchedString);
            driver.Quit();
        }
    }
}

Warning: The driver.quit statement is required, otherwise the test continues to execute, leading to a timeout.

The test results are available on the command-line interface, as well as the Automate dashboard. You have now run your first test on BrowserStack App Automate.

Note:

  1. UIAutomator2 is the default automation engine for Android.
  2. Pass automationName as 'Appium' if you are using Appium automation engine
  3. If your app uses webview, set the webview debugging flag to true when creating the webview object as described in the Android remote debugging docs: WebView.setWebContentsDebuggingEnabled(true);

Testing on Internal Networks

  1. Download and run the BrowserStackLocal binary:

    Download the appropriate binary:

    • OS X (Recommended for your system)
    • Linux 32-bit
    • Linux 64-bit
    • Windows

    The download links are secure. The binaries are digitally signed, identifying the publisher as BrowserStack Ltd. Read more about our security.

    Navigate to the folder containing the binary, and run it from the terminal.

    • OS X & Linux
    • Windows
    BrowserStackLocal.exe --key ACCESS_KEY
    
  2. Once the connection is made you need to set the browserstack.local capability to true.

    caps.setCapability("browserstack.local", "true");
    

Multiple Local Testing connections

If you are using same account to test multiple applications, you can setup a named connection using localIdentifier.

  1. Run the BrowserStackLocal binary with the localIdentifier option
    • OS X & Linux
    • Windows
    BrowserStackLocal.exe --key ACCESS_KEY --local-identifier Test123

    Here, we have created a named connection called Test123.

  2. Once the connection is made, you need to set the browserstack.localIdentifier capability with the correct named connection

    caps.setCapability("browserstack.local", "true");
    caps.setCapability("browserstack.localIdentifier", "Test123");
    

Note: Learn more about other common local testing use cases such as testing behind a firewall, proxy, browsermob proxy and whitelisting IPs.

Speed up testing

Parallel testing is running multiple tests concurrently to reduce your testing time, and is a key feature of BrowserStack Automate. This can be the same test running on different browser configurations, or different tests running on the same browser configuration. With access to our extensive BrowserStack cloud, parallel testing on many platforms becomes very efficient.

Queuing

With queuing, you can launch an additional number of parallel tests with different configurations that will be queued in a sequence, for a seamless parallel execution. For instance, if you want to run 5 additional tests, apart from your subscribed limit of 2 parallel tests, BrowserStack will queue the additional 5 tests until one of the 2 initial tests finish, and a slot is available for execution. With queuing, you can be less bothered about managing your tests, and it can save development time.

With this feature, accounts up to 5 parallel tests can queue 5 tests. Beyond 5 parallel tests, an equivalent number of tests will be queued.

Note: The wait limit for the execution of a pending queued job is 15 minutes and will be cancelled if exceeded.

We have provided examples of parallel testing implementation using popular testing frameworks.

Configuring Capabilities

Capabilities are a series of key-value pairs that allow customization of testing from within BrowserStack Automate.

Appium provides a series of capabilities that you can set for the Appium version you are running. Appium server on the BrowserStack will receive all the capabilities you set on the client side. You can also use BrowserStack specific capabilities to configure your tests. Visit Capabilities page to view the complete list of BrowserStack capabilities.

Use the drop down below to select your device and configure the capabilities.

Note: In free trial you can access "Google Pixel", "Google Nexus 6" and "iPhone 7 Plus" devices.

1. Select an OS
iOS
Mobile
  • iOS
  • Android
Desktop
2. Select a browser
Windows XP
2. Select a device
iOS
3. Select a resolution
1024 x 768
Resolution
    caps.setCapability("device", "iPhone 7 Plus");
    caps.setCapability("os_version", "10.3.3");
    

    Using test frameworks?

    C# has several popular testing frameworks, and we have provided examples on how to use them in conjunction with Automate. Check out our complete documentation for writing automate test scripts in NUnit.

    In This Article

    • Getting Started
    • Test on Internal Networks
      • Multiple local testing connections
    • Speed up testing
      • Queueing
    • Configuring Capabilities
    • Using test frameworks

    Related Articles

    Capabilities

    Local Testing

    Rest API

    Products
    • Live
    • Automate
    • App Live New
    • App Automate New
    • Screenshots
    • Responsive
    • Enterprise
    Mobile
    • Test on Right Devices
    • Mobile Features
    • Mobile Emulators
    • Test on iPhone
    • Test on iPad
    • Test on Galaxy
    Other Links
    • Open Source
    • Test in IE
    • Careers We're hiring!
    • Support
    • Contact
    • Company
    • News
    Social
    Header logo

    © 2011-2018 BrowserStack - A cross-browser testing tool.

    • Terms of Service
    • Privacy Policy