Skip to content

Instantly share code, notes, and snippets.

Last active Nov 3, 2021
What would you like to do?
Test method - UI Automator medium article
fun validateWifi() {
// Open apps list by scrolling on home screen
val workspace = device.findObject(
workspace.scroll(Direction.DOWN, 1.0f)
// Click on Settings icon to launch the app
val settings = device.findObject(
// Wait up to 2 seconds for the element be displayed on screen
val networkAndInternet = device.wait(Until.findObject(By.text("Network & internet")), 2000)
// Click on element with text "Wi‑Fi"
val wifi = device.wait(Until.findObject(By.text("Wi‑Fi")), 2000)
// Click on element with text "Add network"
val addNetwork = device.wait(Until.findObject(By.text("Add network")), 2000)
// Obtain an instance of UiObject2 of the text field
val ssidField = device.wait(Until.findObject(By.res("")), 2000)
// Call the setText method using Kotlin's property access syntax
val ssid = "AndroidWifi"
ssidField.text = ssid
//Click on Save button
// BySelector matching the just added Wi-Fi
val ssidSelector = By.text(ssid).res("android:id/title")
// BySelector matching the connected status
val status = By.text("Connected").res("android:id/summary")
// BySelector matching on entry of Wi-Fi list with the desired SSID and status
val networkEntrySelector = By.clazz(RelativeLayout::class.qualifiedName)
// Perform the validation using hasObject
// Wait up to 5 seconds to find the element we're looking for
val isConnected = device.wait(Until.hasObject(networkEntrySelector), 5000)
Assert.assertTrue("Verify if device is connected to added Wi-Fi", isConnected)
// Perform the validation using Android APIs
val connectedWifi = getCurrentWifiSsid()
Assert.assertEquals("Verify if is connected to the Wifi", ssid, connectedWifi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment