Skip to content

Commit

Permalink
Issue 630 bump UI automator (#641)
Browse files Browse the repository at this point in the history
* ISSUE-630: add new UiAutomatorViewer matchers
  • Loading branch information
Nikitae57 authored May 16, 2024
1 parent 40b6cdd commit 0aad56b
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ configure<BaseExtension> {
}

defaultConfig {
minSdk = 18
minSdk = 21
targetSdk = 30
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ kotson = "com.github.salomonbrys.kotson:kotson:2.5.0"

espressoCore = { module = "androidx.test.espresso:espresso-core", version.ref = "espresso" }
espressoWeb = { module = "androidx.test.espresso:espresso-web", version.ref = "espresso" }
uiAutomator = "androidx.test.uiautomator:uiautomator:2.2.0"
uiAutomator = "androidx.test.uiautomator:uiautomator:2.3.0"
robolectric = "org.robolectric:robolectric:4.8.2"
kakao = { module = "io.github.kakaocup:kakao", version.ref = "kakao" }
kakaoCompose = { module = "io.github.kakaocup:compose", version.ref = "kakaoCompose" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ class PermissionsImpl(
*/
private fun getPermissionDialogButtonAsUiObject(button: Permissions.Button): UiObject? {
return try {
val buttonResId = buttonResNameMap[button] ?: return null
val btnSelector = UiSelector()
.clickable(true)
.checkable(false)
.resourceId(buttonResNameMap[button])
.resourceId(buttonResId)
uiDevice.findObject(btnSelector)
} catch (e: UiObjectNotFoundException) {
logger.e("There are no permissions dialog to interact with.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
@file:Suppress("unused")
package com.kaspersky.components.kautomator.component.common.builders

import android.os.Build
import androidx.annotation.IdRes
import androidx.annotation.RequiresApi
import androidx.annotation.StringRes
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.BySelector
Expand Down Expand Up @@ -315,6 +317,41 @@ class UiViewBuilder {
*/
fun withMaxDepth(max: Int) = addSelector { maxDepth(max) }

/**
* Matches the view being on the display with the provided id
*/
@RequiresApi(Build.VERSION_CODES.R)
fun withDisplayId(id: Int) = addSelector { displayId(id) }

/**
* Matches the view that has parent which fits the given matcher
*/
fun withParent(function: UiViewBuilder.() -> Unit) = addSelector { hasParent(UiViewBuilder().apply(function).build().bySelector) }

/**
* Matches the view with given hint
*
* @param text Text to match
*/
@RequiresApi(Build.VERSION_CODES.O)
fun withHint(text: String) = addSelector { hint(text) }

/**
* Matches the view with given hint
*
* @param textRes Text to match
*/
@RequiresApi(Build.VERSION_CODES.O)
fun withHint(@StringRes textRes: Int) = addSelector { hint(KString.getString(textRes)) }

/**
* Matches the view with given hint
*
* @param text Text to match
*/
@RequiresApi(Build.VERSION_CODES.O)
fun withHint(text: Pattern) = addSelector { hint(text) }

/**
* Matches the view which has child of given matcher
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.kaspersky.kaspresso.kautomatorsample.kaspresso.sanity

import android.os.Build
import androidx.test.ext.junit.rules.activityScenarioRule
import com.kaspersky.kaspresso.kautomatorsample.ComponentsActivity
import com.kaspersky.kaspresso.kautomatorsample.screen.ComponentsScreen
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
import org.junit.Assume
import org.junit.Rule
import org.junit.Test

class HintMatchersTest : TestCase() {

@get:Rule
val activityRule = activityScenarioRule<ComponentsActivity>()

@Test
fun test() = run {
Assume.assumeTrue("Hint matchers are available on API 26+", Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)

ComponentsScreen {
editTextByHintResId { isDisplayed() }
editTextByHintText { isDisplayed() }
editTextByHintPattern { isDisplayed() }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import com.kaspersky.components.kautomator.component.bottomnav.UiBottomNavigatio
import com.kaspersky.components.kautomator.component.check.UiCheckBox
import com.kaspersky.components.kautomator.component.chip.UiChipGroup
import com.kaspersky.components.kautomator.component.dialog.UiAlertDialog
import com.kaspersky.components.kautomator.component.edit.UiEditText
import com.kaspersky.components.kautomator.component.text.UiButton
import com.kaspersky.components.kautomator.screen.UiScreen
import com.kaspersky.kaspresso.kautomatorsample.R

object ComponentsScreen : UiScreen<ComponentsScreen>() {

Expand All @@ -16,4 +18,8 @@ object ComponentsScreen : UiScreen<ComponentsScreen>() {
val chipGroup = UiChipGroup { withId(this@ComponentsScreen.packageName, "chipGroup") }
val showDialogBtn = UiButton { withId(this@ComponentsScreen.packageName, "showDialogBtn") }
val dialog = UiAlertDialog(packageName)

val editTextByHintResId = UiEditText { withHint(R.string.hint) }
val editTextByHintText = UiEditText { withHint("Some hint") }
val editTextByHintPattern = UiEditText { withHint("Some.*".toPattern()) }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.kaspersky.kaspresso.kautomatorsample.screen

import com.kaspersky.components.kautomator.component.text.UiTextView
import com.kaspersky.components.kautomator.screen.UiScreen
import com.kaspersky.kaspresso.kautomatorsample.R

object ParentSearchScreen : UiScreen<ParentSearchScreen>() {
override val packageName = "com.kaspersky.kaspresso.kautomatorsample"

val textByParentId = UiTextView {
isFocusable()
withParent { withId(R.id.container1) }
}

val textByParentChild = UiTextView {
isFocusable()
withParent {
withChild { withId(R.id.title2) }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.kaspersky.kaspresso.kautomatorsample.test

import androidx.test.ext.junit.rules.activityScenarioRule
import com.kaspersky.kaspresso.kautomatorsample.ParentSearchActivity
import com.kaspersky.kaspresso.kautomatorsample.screen.ParentSearchScreen
import com.kaspersky.kaspresso.testcases.api.testcase.TestCase
import org.junit.Rule
import org.junit.Test
import com.kaspersky.kaspresso.kautomatorsample.R

class ParentSearchTest : TestCase() {
@get:Rule
val activityRule = activityScenarioRule<ParentSearchActivity>()

@Test
fun test() = run {
ParentSearchScreen {
textByParentId {
val text1 = device.targetContext.resources.getString(R.string.parent_search_value1)
hasText(text1)
}
textByParentChild {
val text2 = device.targetContext.resources.getString(R.string.parent_search_value2)
hasText(text2)
}
}
}
}
1 change: 1 addition & 0 deletions samples/kautomator-sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<activity android:name=".autoscroll.AutoScrollActivity" />
<activity android:name=".flaky.FlakyActivity" />
<activity android:name=".systemdialogs.SystemDialogsActivity" />
<activity android:name=".ParentSearchActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kaspersky.kaspresso.kautomatorsample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class ParentSearchActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_parent_search)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:id="@+id/container1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:text="Some title" />
<TextView
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:text="@string/parent_search_value1" />
</LinearLayout>

<com.google.android.material.divider.MaterialDivider
android:layout_width="match_parent"
android:layout_height="1dp" />

<LinearLayout
android:id="@+id/container2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/title2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:text="Some title" />
<TextView
android:focusable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:text="@string/parent_search_value2" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/chipGroup" />

<EditText
android:id="@+id/edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="@string/hint"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/showDialogBtn" />

</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.navigation.NavigationView
Expand Down
4 changes: 4 additions & 0 deletions samples/kautomator-sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@
<string name="chip_2">Chip 2</string>
<string name="chip_3">Chip 3</string>
<string name="dialog">Dialog</string>
<string name="hint">Some hint</string>

<string name="parent_search_value1">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor </string>
<string name="parent_search_value2">Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium</string>
</resources>

0 comments on commit 0aad56b

Please sign in to comment.