Search by

Run your first test

BrowserStack App Automate enables you to test native and hybrid mobile applications using Appium automation framework. Its easy to run your Appium tests using TestNG on real Android and iOS devices on BrowserStack. This guide will help you get started with your first test.

1. Setup

Ensure you have TestNG libraries installed.

<!-- Install using maven -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.9.10</version>
</dependency>

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 the app :

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

A sample response for the above request is shown below:

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

Note the value of app_url returned in the API response (bs://j3c874f21852b..... in the above example). This value will be used later to set the app capability to specify application under test in your Appium test scripts.

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 the test

In this step, you will learn how to configure your Appium test script using desired capabilities to test remotely on BrowserStack’s real device cloud. Here is a sample test case written for running with TestNG.

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.

public class SingleTest extends BrowserStackTestNGTest {
  @Test
  public void test() throws Exception {
    AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until(
        ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Search Wikipedia")));
    searchElement.click();
    AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until(
        ExpectedConditions.elementToBeClickable(MobileBy.id("org.wikipedia.alpha:id/search_src_text")));
    insertTextElement.sendKeys("BrowserStack");
    Thread.sleep(5000);

    List allProductsName = driver.findElementsByClassName("android.widget.TextView");
    Assert.assertTrue(allProductsName.size() > 0);
  }
}

If you are using our 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.

 public class SingleTest extends BrowserStackTestNGTest {
  @Test
  public void test() throws Exception {
    IOSElement textButton = (IOSElement) new WebDriverWait(driver, 30).until(
        ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Text Button")));
    textButton.click();
    IOSElement textInput = (IOSElement) new WebDriverWait(driver, 30).until(
        ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Text Input")));
    textInput.sendKeys("hello@browserstack.com"+"\n");

    Thread.sleep(5000);

    IOSElement textOutput = (IOSElement) new WebDriverWait(driver, 30).until(
        ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Text Output")));

    Assert.assertEquals(textOutput.getText(),"hello@browserstack.com");
  }
}

To actually run the test case, we need to integrate with BrowserStack as follows:

Integration with BrowserStack

Note: Running your TestNG tests on BrowserStack requires a username and an access key.

Integration of TestNG with BrowserStack is made possible by the use of the following module:

public class BrowserStackTestNGTest {
  public WebDriver driver;
  private Local l;

  @BeforeMethod(alwaysRun=true)
  @org.testng.annotations.Parameters(value={"config", "environment"})
  public void setUp(String config_file, String environment) throws Exception {
      JSONParser parser = new JSONParser();
      JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file));
      JSONObject envs = (JSONObject) config.get("environments");

      DesiredCapabilities capabilities = new DesiredCapabilities();

      Map<string, string=""> envCapabilities = (Map<string, string="">) envs.get(environment);
      Iterator it = envCapabilities.entrySet().iterator();
      while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
      }

      Map<string, string=""> commonCapabilities = (Map<string, string="">) config.get("capabilities");
      it = commonCapabilities.entrySet().iterator();
      while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        if(capabilities.getCapability(pair.getKey().toString()) == null){
            capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString());
        }
      }

      String username = System.getenv("BROWSERSTACK_USERNAME");
      if(username == null) {
        username = (String) config.get("user");
      }

      String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY");
      if(accessKey == null) {
        accessKey = (String) config.get("key");
      }

      String app = System.getenv("BROWSERSTACK_APP_ID");
      if(app != null && !app.isEmpty()) {
        capabilities.setCapability("app", app);
      }

      if(capabilities.getCapability("browserstack.local") != null && capabilities.getCapability("browserstack.local") == "true"){
        l = new Local();
        Map<string, string=""> options = new HashMap<string, string="">();
        options.put("key", accessKey);
        l.start(options);
      }

      driver = new AndroidDriver(new URL("https://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities);
  }

  @AfterMethod(alwaysRun=true)
  public void tearDown() throws Exception {
    driver.quit();
    if(l != null) l.stop();
  }
}

The module reads from the config file where you need to put the BrowserStack Hub URL and credentials. If the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY are set in Environment variables, then that will take preference. Also, update the app_url(bs://…..) after uploading App to the BrowserStack cloud.

Update the desired capability app with the app_url (bs://< hashed-id >) returned from the above app upload API call.

{
  "server": "hub-cloud.browserstack.com",
  "user": "YOUR_USERNAME",
  "key": "YOUR_ACCESS_KEY",

  "capabilities": {
    "build": "testng-browserstack",
    "name": "single_appium_test",
    "browserstack.debug": true,
    "app": "your_app_url"
  },

  "environments": {
    "galaxy_s7": {
      "device": "Samsung Galaxy S7"
    }
  }
}
{
  "server": "hub-cloud.browserstack.com",
  "user": "YOUR_USERNAME",
  "key": "YOUR_ACCESS_KEY",

  "capabilities": {
    "build": "testng-browserstack",
    "name": "single_appium_test",
    "browserstack.debug": true,
    "app": "your_app_url"
  },

  "environments": {
    "iPhone_7": {
      "device": "iPhone 7"
    }
  }
}

Run your test on BrowserStack using following command:

# Run using maven
mvn test -P single
Note: Refer to our GitHub repository for a complete example: BrowserStack - TestNG

4. Viewing test results

You can access results of your test sessions on the App Automate dashboard as well as using our REST API. You can drill down into the details of a specific test session to view its execution details and debugging information such as video recording, network logs and device logs.