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>"}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.
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.
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.
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.
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(); } } }
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.
Testing on Internal Networks
Download and run the BrowserStackLocal binary:
Download the appropriate binary:
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.
BrowserStackLocal.exe --key ACCESS_KEY
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.
- Run the BrowserStackLocal binary with the localIdentifier option
BrowserStackLocal.exe --key ACCESS_KEY --local-identifier Test123
Here, we have created a named connection called Test123.
- 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");
- Once the connection is made, you need to set the browserstack.localIdentifier capability with the correct named connection
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.
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.
caps.setCapability("device", "iPhone 7 Plus"); caps.setCapability("os_version", "10.3.3");