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 / 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
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 / 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 / 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 / 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 / WifiTest.kt
Last active November 3, 2021 00:57
Get SSID of connected WiFi Get SSID of connected WiFi
private fun getCurrentWifiSsid(): String? {
val context = InstrumentationRegistry.getInstrumentation().context
val wifiManager = context.getSystemService(Context.WIFI_SERVICE) as WifiManager
val wifiInfo = wifiManager.connectionInfo
// The SSID is quoted, then we need to remove quotes
return wifiInfo.ssid?.removeSurrounding("\"")
}
@heitorpaceli
heitorpaceli / WifiTest.kt
Last active November 3, 2021 00:58
Tear down method UI Automator medium article
@After
fun tearDown() {
// Press Home key after running the test
device.pressHome()
}
@heitorpaceli
heitorpaceli / WifiTest.kt
Last active November 3, 2021 00:59
Set up method UI Automator medium article
@Before
fun setUp() {
// Press Home key before running the test
device.pressHome()
}
@heitorpaceli
heitorpaceli / WifiTest.kt
Last active November 3, 2021 01:03
Test method - UI Automator medium article
@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
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)