John A Qualls
John A Qualls

Reputation: 849

Toggling Android Device Network During Execution of an Instrumentation Test

Has anyone had any luck toggling the device network in the middle of an instrumentation test? I've found some hacky outdated solutions that are no longer supported such as this one.

I can't seem to find any recent answers to this problem. I'm sure this is a common testing scenario for many applications. Thanks in advance!

Upvotes: 4

Views: 672

Answers (1)

John A Qualls
John A Qualls

Reputation: 849

Alright, below is a solution that I came up with to enable/disable the network during an instrumented test. Thank you for the reference to UiAutomator Sandi!

fun clickAirplaneMode(instrumentation: Instrumentation, targetContext: Context) {
    UiDevice.getInstance(instrumentation).run {
        targetContext.packageManager.getLaunchIntentForPackage("com.android.settings")?.let {intent ->
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
            targetContext.startActivity(intent)
            findObject(UiSelector().textContains("Network")).clickAndWaitForNewWindow()
            findObject(UiSelector().textContains("Airplane")).click()
        }
    }
}

Upvotes: 1

Related Questions