Run sample native app test

Step 1: Setup environment

Requirements

  • JDK (Highly recommended to use JDK 8 SE)
  • Java build framework
  • Access key
    • You can generate an Access key using the following instruction – Access key
  • Billing unit
    • The billing unit is your team’s unique identifier, or just the word “personal” in the case of an individual account.
      More information can be found here.

Step 2: Upload your app

Upload your Android app (.apk file) or iOS app (.ipa file) to the Mobitru using our REST API. Here is an example cURL request to upload the app :

curl --location --request POST 'https://app.mobitru.com/billing/unit/<BILLING_UNIT>/automation/api/v1/spaces/artifacts' \
--header 'x-File-Name: AppDemo.apk' \
--header 'X-Content-Type: application/zip' \
--header 'Authorization: Bearer <ACCESS_KEY>' \
--form 'file=@"/path/to/app/file/app-debug.apk"' \
--form 'checksum="None"'

Below you can find a sample of the response with an id of the uploaded app:

{
    "id": "b7b0aae5-a839-4c15-b6ba-98edb75d07a1",
    "name": "22d69718-59a9-40c6-809c-0e3d4a7313b7.apk",
    "realName": "AppDemo.apk",
    "bid": "916f0549-4ddc-491a-9fe7-3f27597fd3b7",
    "wid": "0",
    "private": true,
    "uploadedBy": "test_user@epam.com",
    "uploadedAt": 1670264922,
    "verified": false,
    "target": "android",
    "contentType": "application/zip",
    "contentLength": 0,
    "href": "quarantine/22d69718-59a9-40c6-809c-0e3d4a7313b7.apk",
    "checksum": "None",
    "alias": "automation",
    "apk": null,
    "ipa": null
}

Step 3: Find and take a device

Find and take appropriate device using our APIs.

Here is an example of cURL request to find a device via API:

Android:

curl --location --request GET 'https://app.mobitru.com/billing/unit/<BILLING_UNIT>/automation/api/device/android?model=Pixel 6' \
--header 'Authorization: Bearer <ACCESS_KEY>'

iOS:

curl --location --request GET 'https://app.mobitru.com/billing/unit/<BILLING_UNIT>/automation/api/device/ios?version=16.2' \
--header 'Authorization: Bearer <ACCESS_KEY>'

Below you can find a sample of the response with capabilities for found devices:

Android:

[
    {
        "desiredCapabilities": {
            "platformName": "Android",
            "platformVersion": "12",
            "deviceName": "GOOGLE Pixel 6a",
            "udid": "26281JEGR04493"
        }
    },
    {
        "desiredCapabilities": {
            "platformName": "Android",
            "platformVersion": "12",
            "deviceName": "GOOGLE Pixel 6",
            "udid": "19161FDF600DJP"
        }
    },
    {
        "desiredCapabilities": {
            "platformName": "Android",
            "platformVersion": "13",
            "deviceName": "GOOGLE Pixel 6 Pro",
            "udid": "1A281FDEE007T3"
        }
    }
]


iOS:

[
    {
        "desiredCapabilities": {
            "platformName": "iOS",
            "platformVersion": "16.2",
            "deviceName": "IPHONE iPhone13,3",
            "udid": "00008101-00042D8A1190001E",
            "automationName": "XCUITest"
        }
    },
    {
        "desiredCapabilities": {
            "platformName": "iOS",
            "platformVersion": "16.2",
            "deviceName": "IPAD iPad12,1",
            "udid": "00008030-001E79460A10C02E",
            "automationName": "XCUITest"
        }
    },
    {
        "desiredCapabilities": {
            "platformName": "iOS",
            "platformVersion": "16.2",
            "deviceName": "IPHONE iPhone13,2",
            "udid": "00008101-001219601A51003A",
            "automationName": "XCUITest"
        }
    }
]

Here is an example of cURL request to take a device via API :

Android:

curl --location --request POST 'https://app.mobitru.com/billing/unit/<BILLING_UNIT>/automation/api/device/26281JEGR04493' \
--header 'Authorization: Bearer <ACCESS_KEY>'

iOS:

curl --location --request POST 'https://app.mobitru.com/billing/unit/<BILLING_UNIT>/automation/api/device/00008101-00042D8A1190001E' \
--header 'Authorization: Bearer <ACCESS_KEY>'

Below you can find a sample of the response with capabilities for the device:

Android:

{ 
    "desiredCapabilities": {
        "platformName": "Android",
        "platformVersion": "12",
        "deviceName": "GOOGLE Pixel 6a",
        "udid": "26281JEGR04493"
    }
}

iOS:

{
    "desiredCapabilities": {
        "platformName": "iOS",
        "platformVersion": "16.2",
        "deviceName": "IPHONE iPhone13,3",
        "udid": "00008101-00042D8A1190001E"
    }
}

Step 4: Install the app on the device

Install the App on the Device before starting the first test.

Here is an example of cURL request to install the app via API (udid from step 3 and app id from step 2):

curl --location --request GET 'https://app.mobitru.com/billing/unit/<BILLING_UNIT>/automation/api/storage/install/26281JEGR04493/b7b0aae5-a839-4c15-b6ba-98edb75d07a1' \
--header 'Authorization: Bearer <ACCESS_KEY>'

As a result, you will receive 201 Created response.

Step 5: Run sample test

After installing the app, you can run your first test using all existing information:

package com.mobitru;

import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import io.appium.java_client.remote.options.BaseOptions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import static java.lang.String.format;
import static java.time.Duration.ofSeconds;
import static java.util.Optional.ofNullable;
import static org.junit.Assert.assertEquals;


public class AppiumDemo {
    private static final String PROJECT_NAME = "--------- BILLING UNIT -----------";

    private static final String ACCESS_KEY = "--------- YOU ACCESS KEY -----------";
    private static final String APPIUM_HUB = "app.mobitru.com";

    private final BaseOptions<? extends MutableCapabilities> options;

    private AppiumDriver driver = null;

    public AppiumDemo() {
        options = new UiAutomator2Options();
        options.setCapability("udid", "26281JEGR04493");
        options.setCapability("appPackage", "'com.example.android.apis'");
        options.setCapability("appActivity", "'.ApiDemos'");
        options.setCapability("noReset", true);
    }

    @Before
    public void before() throws Exception {
        driver = new AppiumDriver(
                new URL(format("https://%s:%s@%s/wd/hub", PROJECT_NAME, ACCESS_KEY, APPIUM_HUB)), options);

        // For devices with low performance
        driver.manage().timeouts()
                .implicitlyWait(ofSeconds(10));
    }

    @Test
    public void demoTest() {
        driver.findElement(AppiumBy.androidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"Views\").instance(0));")).click();

        driver.findElement(AppiumBy.accessibilityId("Controls")).click();
        driver.findElement(AppiumBy.accessibilityId("1. Light Theme")).click();

        WebElement element = driver.findElement(AppiumBy.className("android.widget.EditText"));
        element.sendKeys("text");

        assertEquals("Entered test is incorrect",  "text", element.getText());
    }

    @After
    public void after() {
        ofNullable(driver).ifPresent(RemoteWebDriver::quit);
    }
}
package com.mobitru;

import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.ios.options.XCUITestOptions;
import io.appium.java_client.remote.options.BaseOptions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import static java.lang.String.format;
import static java.time.Duration.ofSeconds;
import static java.util.Optional.ofNullable;
import static org.junit.Assert.assertEquals;


public class AppiumDemo {
    private static final String PROJECT_NAME = "--------- BILLING UNIT -----------";

    private static final String ACCESS_KEY = "--------- YOU ACCESS KEY -----------";
    private static final String APPIUM_HUB = "app.mobitru.com";

    private final BaseOptions<? extends MutableCapabilities> options;

    private AppiumDriver driver = null;

    public AppiumDemo() {
        options = new XCUITestOptions();
        options.setCapability("udid", "00008030-000C312C1E69802E");
        options.setCapability("bundleId", "com.mobitru.appium.uicatalog");
        options.setCapability("autoAcceptAlerts", true);
    }

    @Before
    public void before() throws Exception {
        driver = new AppiumDriver(
                new URL(format("https://%s:%s@%s/wd/hub", PROJECT_NAME, ACCESS_KEY, APPIUM_HUB)), options);

        // For devices with low performance
        driver.manage().timeouts()
                .implicitlyWait(ofSeconds(10));
    }

    @Test
    public void demoTest() {
        String text = "text";
        WebElement input = driver.findElement(AppiumBy.className("XCUIElementTypeTextField"));
        input.sendKeys(text);
        String enteredText = input.getAttribute("value");

        assertEquals("Entered text is incorrect", text, enteredText);
    }

    @After
    public void after() {
        ofNullable(driver).ifPresent(RemoteWebDriver::quit);
    }
}
Scroll to Top