Local Testing with App Automate

Local Testing is a BrowserStack feature that helps you test mobile apps that access resources hosted in development or testing environments during automated test execution. This page will guide you through enabling Local Testing for App Automate sessions, and then using it to test apps that retrieve data from servers on your local machine, CI/CD machines or nodes, and other private network configurations. In this guide you will learn how to :

  1. Setup your environment
  2. Upload your app
  3. Configure and run your Local test
  4. View test execution results

1. Setup your environment

  • You will need a BrowserStack username and access_key. If you haven’t created an account yet, sign up for a free trial or purchase a paid plan. After signup, you can obtain your access credentials here
  • Ensure you have Microsoft Visual Studio installed on your system. You can download an updated version from Visual Studio. Install .NET Core while installing Visual Studio
  • You will need access to your Android app (.apk or .aab file) or iOS app (.ipa file)
Note: If you do not have an .apk or .ipa file and are looking to simply try App Automate Local Testing, you can download and test using our sample Android Local app or sample iOS Local app.

2. Upload your app

Upload your Android app (.apk or .aab file) or iOS app (.ipa file) to BrowserStack servers using our REST API. Here is an example cURL request to upload app on App Automate :

curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" \
-X POST "https://api-cloud.browserstack.com/app-automate/upload" \
-F "file=@/path/to/apk/file"
curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" ^
-X POST "https://api-cloud.browserstack.com/app-automate/upload" ^
-F "file=@/path/to/apk/file"

Ensure that @ symbol is prepended to the file path in the above request. A sample response for the above request is shown below:

{
    "app_url" : "bs://j3c874f21852ba57957a3fdc33f47514288c4ba4"
}

Please note the app_url value returned in the API response (bs://j3c874..... in the above example). We will use this value to set the application under test while configuring the test later on.

Note:
  1. App upload will take a 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. If you upload an iOS app, we will re-sign the app with our own provisioning profile to be able to install your app on our devices during test execution.

3. Configure and run your Local test

Setup your project

Clone the NUnit sample integration code from our GitHub repository.

git clone https://github.com/browserstack/nunit-appium-app-browserstack.git

Next, switch to appium_dotnet_driver_4_examples under android or ios directory and open the android.sln or ios.sln file in Visual Studio.

Note: If you want to try Appium dotnet driver 3 examples, switch to appium_dotnet_driver_3_examples directory in the NUnit sample integration code repository and follow the steps outlined here.

To install the required NuGet packages, build the project. This will install requisite dependencies including Appium’s dotnet driver :

<packages>
  <package id="Appium.WebDriver" version="4.2.1" targetFramework="net461" />
  <package id="BrowserStackLocal" version="1.4.0" targetFramework="net461" />
  <package id="Castle.Core" version="4.4.1" targetFramework="net461" />
  <package id="DotNetSeleniumExtras.PageObjects" version="3.11.0" targetFramework="net461" />
  <package id="DotNetSeleniumExtras.WaitHelpers" version="3.11.0" targetFramework="net461" />
  <package id="Newtonsoft.Json" version="12.0.3" targetFramework="net461" />
  <package id="NUnit" version="3.12.0" targetFramework="net461" />
  <package id="NUnit3TestAdapter" version="3.17.0" targetFramework="net461" />
  <package id="Selenium.Support" version="3.141.0" targetFramework="net461" />
  <package id="Selenium.WebDriver" version="3.141.0" targetFramework="net461" />
  <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net461" />
</packages>

Configure Appium’s desired capabilities

Desired capabilities are a series of key-value pairs that allow you to configure your Appium tests on BrowserStack. The following capabilities are required :

  • app capability : Its used to specify your uploaded app that will be installed on device during test execution. Use the app_url obtained in Upload your App section to set its value
  • device capability : Its used to specify the BrowserStack device you want to run the test on
  • browserstack.local capability : Its used to enable BrowserStack Local feature for your test execution

In the NUnit sample integration code, Appium’s desired capabilities are defined in the App.config file located in the appium_dotnet_driver_4_examples/ directory :

<configuration>
     <!-- Provide BrowserStack username and access_key -->
    <appSettings>
        <add key="user" value="<YOUR_USERNAME>"/>
        <add key="key" value="<YOUR_ACCESS_KEY>"/>
        <add key="server" value="hub.browserstack.com"/>
    </appSettings>

        <capabilities>
        <local>
            <add key="app" value="bs://<app_id>"/>
            <add key="browserstack.local" value="true"/>
            <add key="project" value="NUnit Android"/>
            <add key="build" value="Local NUnit Android Test"/>
            <add key="name" value="local_test"/>
            <add key="browserstack.debug" value="true"/>
        </local>
    </capabilities>

    <!-- Devices on which test will run -->
    <environments>
        <pixel-3>
            <add key="device" value="Google Pixel 3"/>
            <add key="os_version" value="9.0"/>
        </pixel-3>
        <galaxy-s10e>
            <add key="device" value="Samsung Galaxy S10e"/>
            <add key="os_version" value="9.0"/>
        </galaxy-s10e>
    </environments>
</configuration>
<configuration>
     <!-- Provide BrowserStack username and access_key -->
    <appSettings>
        <add key="user" value="<YOUR_USERNAME>"/>
        <add key="key" value="<YOUR_ACCESS_KEY>"/>
        <add key="server" value="hub.browserstack.com"/>
    </appSettings>

        <capabilities>
        <local>
            <add key="app" value="bs://<app_id>"/>
            <add key="browserstack.local" value="true"/>
            <add key="project" value="NUnit iOS"/>
            <add key="build" value="Local NUnit iOS Test"/>
            <add key="name" value="local_test"/>
            <add key="browserstack.debug" value="true"/>
        </local>
    </capabilities>

    <!-- Devices on which test will run -->
    <environments>
        <iphone-11-pro>
            <add key="device" value="iPhone 11 Pro"/>
            <add key="os_version" value="13.0"/>
        </iphone-11-pro>
        <iphone-11-pro-max>
            <add key="device" value="iPhone 11 Pro Max"/>
            <add key="os_version" value="13.0"/>
        </iphone-11-pro-max>
    </environments>
</configuration>
Note:

Establish a Local Testing connection

In addition to using the browserstack.local capability, you also need to establish a Local Testing connection from your local or CI/CD machine to BrowserStack servers. Within your test scripts, you can add a code snippet that will automatically start and stop Local Testing connection using BrowserStack’s C# binding for BrowserStack Local.

In the NUnit sample integration code, the Local Testing connection is initialised in the BrowserStackNUnitTest.cs file located in the appium_dotnet_driver_4_examples/local-testdirectory as shown below :


    //...
    
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
            && options.ToCapabilities().HasCapability("browserstack.local")
            && options.ToCapabilities().GetCapability("browserstack.local").ToString() == "true")
    {
        browserStackLocal = new Local();
        List<KeyValuePair<string, string>> bsLocalArgs = new List<KeyValuePair<string, string>>() {
                new KeyValuePair<string, string>("key", accesskey)
        };
        browserStackLocal.start(bsLocalArgs);
    }

    //...

    [TearDown]
    public void Cleanup() {
        driver.Quit();
        if (browserStackLocal != null) {
            browserStackLocal.stop();
        }
    }

Note: If you are running Local test on Mac, you need to download and run the BrowserStack Local binary before starting the test:
  • Download BrowserStack Local binary for mac: OS X (10.7 and above)
  • Switch to the directory where binary is downloaded and run using following command : ./BrowserStackLocal --key <YOUR_ACCESS_KEY>

Create remote Webdriver

Once you have configured desired capabilities, you can initialize an Appium webdriver to test remotely on BrowserStack. In order to do so, you need to use the remote BrowserStack URL along with your BrowserStack access credentials.

In the NUnit sample integration code, the remote Webdriver is initialised in the BrowserStackNUnitTest.cs file located in the appium_dotnet_driver_4_examples/local-test directory as shown below :


namespace android.first
{
	public class BrowserStackNUnitTest
	{
		//...
		protected AndroidDriver<AndroidElement> driver;
		
		[SetUp]
		public void Init()
		{
			//...
			Uri uri = new Uri("http://" + ConfigurationManager.AppSettings.Get("server") + "/wd/hub/");
			driver = new AndroidDriver<AndroidElement>(uri, options);
		}
		//...
	}
}

namespace ios.first
{
	public class BrowserStackNUnitTest
	{
		//...
		protected IOSDriver<IOSElement> driver;

		[SetUp]
		public void Init()
		{
			//...
			Uri uri = new Uri("http://" + ConfigurationManager.AppSettings.Get("server") + "/wd/hub/");
			driver = new IOSDriver<IOSElement>(uri, options);
		}
		//...
	}
}
Note: You can provide BrowserStack access credentials either by :
  • Setting BROWSERSTACK_USERNAME & BROWSERSTACK_ACCESS_KEY environment variables or by
  • Setting user & key parameters in Appium.config file located in appium_dotnet_driver_4_examples/ directory

Setup your test-case

This step will help you setup your first Local Testing test case with NUnit framework. In the NUnit sample integration code, we have provided a sample test-case in appium_dotnet_driver_4_examples/local-test directory for BrowserStack’s sample apps. If you are testing your own app, please modify the test case accordingly.


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

namespace android.local
{
    [TestFixture("local", "pixel-3")]
    public class LocalTest : BrowserStackNUnitTest
    {
        public LocalTest(string profile, string device) : base(profile,device){ }

        [Test]
        public void testLocal()
        {
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
            AndroidElement searchElement = (AndroidElement) wait.Until(
                SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(
                    By.Id("com.example.android.basicnetworking:id/test_action")));
            searchElement.Click();
            AndroidElement testElement = (AndroidElement) wait.Until(
                SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(
                    By.ClassName("android.widget.TextView")));

            ReadOnlyCollection<AndroidElement> allTextViewElements = driver.FindElements(
                By.ClassName("android.widget.TextView"));

            Thread.Sleep(5000);

            foreach (AndroidElement textElement in allTextViewElements)
            {
                if (textElement.Text.Contains("The active connection is"))
                {
                    Assert.True(textElement.Text.Contains(
                        "The active connection is wifi"),"Incorrect Text");
                }
            }
        }
    }
}


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

namespace ios.local
{
    [TestFixture("local", "iphone-11-pro")]
    public class LocalTest : BrowserStackNUnitTest
    {
        public LocalTest(string profile, string environment) : base(profile,environment){ }

        [Test]
        public void testLocal()
        {
            WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
            IOSElement testButton = (IOSElement) wait.Until(
                SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(
                    MobileBy.AccessibilityId("TestBrowserStackLocal")));
            testButton.Click();

            wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.
            TextToBePresentInElementValue(driver.FindElement(
                    MobileBy.AccessibilityId("ResultBrowserStackLocal")),
                 "Response is: Up and running"));
            
            IOSElement resultElement = (IOSElement)driver.FindElement(
                MobileBy.AccessibilityId("ResultBrowserStackLocal"));

            String resultString = resultElement.Text.ToLower();
            if(resultString.Contains("not working")) 
            {
                Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
                string screenshot = ss.AsBase64EncodedString;
                byte[] screenshotAsByteArray = ss.AsByteArray;
                ss.SaveAsFile("screenshot", ScreenshotImageFormat.Png);
                ss.ToString();
            }
            String expectedString = "Up and running";
            Assert.True(resultString.Contains(expectedString.ToLower()));
        }
    }
}

Run the test

Run parallel tests using following steps :

  • Build the project by selecting Build android or Build ios under Build menu
  • Open Unit Test explorer. To open it on Windows, select Test Explorer under Test menu or on Mac, select Unit Tests under View > Pads menu item
  • Right click on android.local or ios.local test and click run

4. View test execution results

You can access the test execution results, and debugging information such as video recording, network and device logs on App Automate dashboard or using our REST APIs.

What’s next

Congratulations! You just ran your first local test on App Automate. Next, you can learn to :

  • Run tests in parallel - Speed up test execution by running tests simultaneously across multiple BrowserStack devices

Need some help?

If you have any queries, please get in touch with us.

We're sorry to hear that. Please share your feedback so we can do better






Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better






Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback!

Talk to automation expert