Last active
November 3, 2021 01:03
-
-
Save heitorpaceli/daf16587af100f92f6a8b0882a00f2e4 to your computer and use it in GitHub Desktop.
Test method - UI Automator medium article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun validateWifi() { | |
// Open apps list by scrolling on home screen | |
val workspace = device.findObject( | |
By.res("com.google.android.apps.nexuslauncher:id/workspace") | |
) | |
workspace.scroll(Direction.DOWN, 1.0f) | |
// Click on Settings icon to launch the app | |
val settings = device.findObject( | |
By.res("com.google.android.apps.nexuslauncher:id/icon").text("Settings") | |
) | |
settings.click() | |
// Wait up to 2 seconds for the element be displayed on screen | |
val networkAndInternet = device.wait(Until.findObject(By.text("Network & internet")), 2000) | |
networkAndInternet.click() | |
// Click on element with text "Wi‑Fi" | |
val wifi = device.wait(Until.findObject(By.text("Wi‑Fi")), 2000) | |
wifi.click() | |
// Click on element with text "Add network" | |
val addNetwork = device.wait(Until.findObject(By.text("Add network")), 2000) | |
addNetwork.click() | |
// Obtain an instance of UiObject2 of the text field | |
val ssidField = device.wait(Until.findObject(By.res("com.android.settings:id/ssid")), 2000) | |
// Call the setText method using Kotlin's property access syntax | |
val ssid = "AndroidWifi" | |
ssidField.text = ssid | |
//Click on Save button | |
device.findObject(By.res("android:id/button1").text("Save")).click() | |
// 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) | |
.hasChild(ssidSelector) | |
.hasChild(status) | |
// 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