> For the complete documentation index, see [llms.txt](https://docs.qyrus.com/device-farm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.qyrus.com/device-farm/automation.md).

# Automation

### Running Automation Sessions

Automated testing allows you to leverage Qyrus Device Farm's real Android and iOS devices to execute your automated test scripts at scale. During and after execution, Device Farm provides comprehensive reporting that includes real-time live previews, video recordings, time-series device vitals, Appium logs, and device system logs.

***

#### Prerequisites:

* A Qyrus Device Farm project created [(click here for project creation)](/device-farm/project-creation-and-setup.md#creating-a-project)
* Application binary uploaded to the project [(click here for application upload)](/device-farm/project-creation-and-setup.md#application-management)
* **Appium:** Appium client and server (supports Appium 1.x and Appium 2.x)
* **Development Environment:**
  * **Java:** JDK 8, 11, or 17+ with an IDE (Eclipse, IntelliJ IDEA, VS Code) and build tool (Maven/Gradle).
  * **Python (Optional):** Python 3.8+ with `Appium-Python-Client` and `pytest`/`unittest`.
  * **JavaScript/TypeScript (Optional):** Node.js with WebdriverIO or `webdriverio`/`appium`.

***

### Setting Up Automated Tests

#### Creating an Automation Device Session via Quick Setup:

1. **Under Projects, click on “Sessions” for your project:**

![](https://2346916530-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkSZu8cT8D2Zwayrr4I91%2Fuploads%2Fgit-blob-77d53db14717cd2ae1fa7ad2fb3ca8bfb41166a2%2FAspose.Words.a5ba35ee-3494-4720-8f8c-7cedbeae1812.013.png?alt=media)

2. **Click on the “Automation” tab and select “Quick Setup Guide”:**

![](https://2346916530-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkSZu8cT8D2Zwayrr4I91%2Fuploads%2Fgit-blob-cf672b4edc8ca283a10e0c8ed95715e000f258ea%2FAspose.Words.a5ba35ee-3494-4720-8f8c-7cedbeae1812.038.png?alt=media)

***

### Configuring Test Parameters & Capabilities

1. **Choose a Device:** Filter devices by region, platform (Android / iOS / Cloud), brand, or search by model.

![](https://2346916530-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkSZu8cT8D2Zwayrr4I91%2Fuploads%2Fgit-blob-fcfc6ccb446a54fd24315f4c22e09fd8206ad796%2FAspose.Words.a5ba35ee-3494-4720-8f8c-7cedbeae1812.040.png?alt=media)

2. **Select Application File:**
   * **Android:** Select the uploaded `.apk`, `.apks`, or `.aab` file. The tool automatically detects the App Package and allows selecting the initial launch **App Activity**.
   * **iOS:** Select the uploaded `.ipa` file.

![](https://2346916530-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkSZu8cT8D2Zwayrr4I91%2Fuploads%2Fgit-blob-a7d0d0ede0458833af0c43c74f1835bd6ab58d7f%2FAspose.Words.a5ba35ee-3494-4720-8f8c-7cedbeae1812.041.png?alt=media)

3. **Click “Generate JSON”:**
   * The interactive CodeMirror editor outputs the JSON capabilities formatted for your testing framework.

![](https://2346916530-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkSZu8cT8D2Zwayrr4I91%2Fuploads%2Fgit-blob-59f3865d64801f85788090f55ace3eb5c128f89c%2FAspose.Words.a5ba35ee-3494-4720-8f8c-7cedbeae1812.042.png?alt=media) ![](https://2346916530-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkSZu8cT8D2Zwayrr4I91%2Fuploads%2Fgit-blob-28c8fd05c7b3c906c0d16e7a8d633ed2e34160af%2FAspose.Words.a5ba35ee-3494-4720-8f8c-7cedbeae1812.043.png?alt=media)

4. **Add Capabilities into Your Framework:**
   * Copy the generated capabilities and paste them into your test framework setup method.

![](https://2346916530-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkSZu8cT8D2Zwayrr4I91%2Fuploads%2Fgit-blob-7c952fb30920b8f04a85b3ab5299423dc7b5fbf7%2FAspose.Words.a5ba35ee-3494-4720-8f8c-7cedbeae1812.044.png?alt=media) ![](https://2346916530-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkSZu8cT8D2Zwayrr4I91%2Fuploads%2Fgit-blob-02e8efe3436977d304d1d0235f720c54dbca529b%2FAspose.Words.a5ba35ee-3494-4720-8f8c-7cedbeae1812.045.png?alt=media)

***

### Cloud Device Farm (AWS) Automation

For devices running on AWS Device Farm cloud infrastructure:

1. Generate the Cloud Device payload from the **Remote Devices** tab in Project Setup.
2. Start the automation session using the REST API endpoint:
   * `POST /device-farm/automation/provider/start?projectId=<ProjectToken>`
3. Execute your Appium test suite targeting the cloud endpoint.
4. Stop the session using the endpoint:
   * `POST /device-farm/automation/provider/stop?projectId=<ProjectToken>`

***

### Code Examples

#### 1. Java (TestNG & Appium)

```java
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;

public class DeviceFarmAutomation {
    private AndroidDriver driver;

    @BeforeTest
    public void setUp() throws MalformedURLException {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("deviceName", "<DEVICE_NAME>");
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("automationName", "UiAutomator2");
        desiredCapabilities.setCapability("platformVersion", "<OS_VERSION>");
        desiredCapabilities.setCapability("udid", "<DEVICE_UDID>"); 
        desiredCapabilities.setCapability("app", "<APP_URL>");
        desiredCapabilities.setCapability("appium:AuthToken", "<PROJECT_TOKEN>");
        desiredCapabilities.setCapability("appium:Authentication", "<USER_API_TOKEN>");
        desiredCapabilities.setCapability("appium:SessionName", "My Automation Run");

        URL remoteUrl = new URL("https://<REGION_ENDPOINT>/wd/hub");
        driver = new AndroidDriver(remoteUrl, desiredCapabilities);
    }

    @Test
    public void testLogin() throws InterruptedException {
        System.out.println("Session ID: " + driver.getSessionId());
        Thread.sleep(5000);
        driver.findElement(By.id("com.example.app:id/username")).sendKeys("testuser");
        driver.findElement(By.id("com.example.app:id/password")).sendKeys("password123");
        driver.findElement(By.id("com.example.app:id/loginBtn")).click();
        Thread.sleep(5000);
    }

    @AfterTest
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }
}
```

![](https://2346916530-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkSZu8cT8D2Zwayrr4I91%2Fuploads%2Fgit-blob-64bbae8dec40b01f364c5fae887d908dab1c7978%2FAutomation-1.png?alt=media)

> **Note:** `remoteUrl`, `appium:AuthToken` (Project Token), and `appium:Authentication` (User API Token) authenticate and route your test traffic securely to the target real device.

***

#### 2. Python (Appium-Python-Client & PyTest)

```python
import pytest
from appium import webdriver
from appium.options.android import UiAutomator2Options

@pytest.fixture(scope="function")
def driver():
    options = UiAutomator2Options()
    options.platform_name = "Android"
    options.device_name = "<DEVICE_NAME>"
    options.platform_version = "<OS_VERSION>"
    options.udid = "<DEVICE_UDID>"
    options.app = "<APP_URL>"
    options.set_capability("appium:AuthToken", "<PROJECT_TOKEN>")
    options.set_capability("appium:Authentication", "<USER_API_TOKEN>")
    options.set_capability("appium:SessionName", "Python Automation Run")

    driver = webdriver.Remote("https://<REGION_ENDPOINT>/wd/hub", options=options)
    yield driver
    driver.quit()

def test_login_flow(driver):
    driver.implicitly_wait(10)
    username_field = driver.find_element(by="id", value="com.example.app:id/username")
    username_field.send_keys("testuser")
    login_button = driver.find_element(by="id", value="com.example.app:id/loginBtn")
    login_button.click()
```

***

#### 3. JavaScript / TypeScript (WebdriverIO)

```javascript
import { remote } from 'webdriverio';

const opts = {
  hostname: '<REGION_ENDPOINT_HOST>',
  path: '/wd/hub',
  protocol: 'https',
  capabilities: {
    platformName: 'Android',
    'appium:deviceName': '<DEVICE_NAME>',
    'appium:platformVersion': '<OS_VERSION>',
    'appium:udid': '<DEVICE_UDID>',
    'appium:automationName': 'UiAutomator2',
    'appium:app': '<APP_URL>',
    'appium:AuthToken': '<PROJECT_TOKEN>',
    'appium:Authentication': '<USER_API_TOKEN>',
    'appium:SessionName': 'WebdriverIO Automation Run'
  }
};

async function runTest() {
  const client = await remote(opts);
  const searchInput = await client.$('id:com.example.app:id/search');
  await searchInput.setValue('Qyrus Device Farm');
  await client.deleteSession();
}

runTest();
```

***

### Running and Managing Automated Tests

1. **Start Automated Tests:** Run your test script from your local machine, IDE, or CI/CD pipeline (Jenkins, GitHub Actions, GitLab CI).
2. **Monitor Live Execution:**
   * Switch to the **Automation** tab in the Device Farm dashboard.
   * The running test appears with a live stream preview card and real-time execution status (`Running`, `Completed`, `Failed`).

![](https://2346916530-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkSZu8cT8D2Zwayrr4I91%2Fuploads%2Fgit-blob-61a56a53abacd60c87d29b43a58f641c830761dd%2FAutomation-2.png?alt=media) ![](https://2346916530-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FkSZu8cT8D2Zwayrr4I91%2Fuploads%2Fgit-blob-35c554ae4857a7e4dea7eadf1186c746decf0571%2FAutomation-3.png?alt=media)

3. **Live Appium Logs:** Click on the active session to view live Appium streaming logs to debug commands and element locators as they execute.
4. **Access Detailed Reports:** Once execution finishes, click **Show Details** to view the comprehensive test report containing Appium logs, device system logs, device vitals graphs, screenshots, and video recordings. [Learn more about Test Reports](/device-farm/test-reports-and-analysis.md#test-reports).

***
