Skip to content

Instantly share code, notes, and snippets.

View heitorpaceli's full-sized avatar
💻

Heitor Paceli Maranhão heitorpaceli

💻
View GitHub Profile
@heitorpaceli
heitorpaceli / WifiTest.kt
Last active November 3, 2021 01:05
Validating Wi-Fi connection - UI Automator Medium article
// ...
// 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)
@heitorpaceli
heitorpaceli / WifiTest.kt
Last active November 3, 2021 01:08
Add wifi network - UI Automator medium article
// ...
// 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()
@heitorpaceli
heitorpaceli / WifiTest.kt
Last active November 3, 2021 01:09
Adding network - UI Automator medium article
// ...
// 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()
@heitorpaceli
heitorpaceli / WifiTest.kt
Last active November 3, 2021 01:11
Launching Settings - UI Automator article on Medium
@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(
@heitorpaceli
heitorpaceli / WifiTest.kt
Created July 7, 2021 14:29
Get UiDevice instance - UiAutomator article on Medium
package com.paceli.wifitest
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class WifiTest {
@heitorpaceli
heitorpaceli / UiAutomatorOrder.kt
Created July 7, 2021 13:57
Order of UI Automator methods
package com.paceli.wifitest
import android.util.Log
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.*
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class UiAutomatorOrder {
@heitorpaceli
heitorpaceli / AndroidManifest.xml
Created July 6, 2021 21:38
Manifest file from UI Automator Medium article
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.paceli.wifitest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" />
@heitorpaceli
heitorpaceli / build.gradle
Created July 6, 2021 21:16
Build.gradle from UI Automator Medium article
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
@heitorpaceli
heitorpaceli / build.gradle
Created July 6, 2021 21:13
UI Automator dependencies in main source set.
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.test.ext:junit:1.1.3'
implementation 'androidx.test:runner:1.4.0'
implementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}
@heitorpaceli
heitorpaceli / build.gradle
Last active November 3, 2021 01:14
Android Instrumented tests inside main source set
sourceSets {
androidTest {
java.srcDir 'src/main/java'
}
}