Espresso/Kotlin

Before launching / test setup

  1. Add gptdriver-client into the libraries inside libs.versions.toml file:

libs.versions.toml
[libraries]
// ...
gptdriver-client = { group = "io.mobileboost.gptdriver", name = "gptdriver-client", version = "1.0.11" }
// ...
  1. Add gptdriver-client into the dependencies array inside build.gradle.kts (:app) file:

build.gradle.kts (:app)
dependencies {
    // ...
    implementation(libs.gptdriver.client)
    // ...
}
  1. If needed, add packaging exclude rules in the android section of the build.gradle.kts (:app) file

To resolve build failures caused by duplicate metadata files from dependencies, exclude them from the packaging process.

build.gradle.kts (:app)
android {
    // ...
    packaging {
        resources {
            excludes.addAll(listOf("META-INF/INDEX.LIST", "META-INF/io.netty.versions.properties"))
        }
    }
    // ...
}
  1. If not already present, add internet permissions to the AndroidManifest.xml file

GPT Driver testing setup requires Appium to interact with the emulator. Since Appium operates via HTTP, ensure explicit network access is available.

AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- ... -->
    
    <application
        android:usesCleartextTraffic="true">
        <!-- ... -->
    </application>
    
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- ... -->
</manifest>
  1. Before launching the tests, start appium locally

cmd
appium --base-path /wd/hub

Inside the test file

  1. Import necessary dependencies

import io.mobileboost.gptdriver.GptDriver
import io.mobileboost.gptdriver.types.Platform
  1. Create the GPT Driver instance and start the session

val sdkInstance = GptDriver(
    "<api_key>",
    Platform.Android,
    "http://10.0.2.2:4723/wd/hub"
)

sdkInstance.startSession()
  1. Use execute method to perform AI-driven steps:

sdkInstance.execute("Tap on the home button and verify it leads to the main screen")

Simple test example:

class TestWithGptDriver {

    @get:Rule
    val activityRule = ActivityScenarioRule(MainActivity::class.java)

    private lateinit var sdkInstance: GptDriver

    @Before
    fun setUp() {
        sdkInstance = GptDriver(
            "<api_key>",
            Platform.Android,
            "http://10.0.2.2:4723/wd/hub"
        )

        sdkInstance.startSession()
    }


    @Test
    fun basicLoginTest() {
        sdkInstance.execute("Log into the app with credentials: username=test, password=test")
        sdkInstance.assertCondition("You are on the home screen")
        
        sdkInstance.stopSession("success")
    }
}

Last updated