Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 191 additions & 0 deletions .idea/androidTestResultsUserPreferences.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- `clickable` selector.
- `getObject` function works like `findObject` used to work (throws if the view represented by the UiObject doesn't exist).
- `findObject` now returns a `UiObject` even if the view represented by the UiObject doesn't exist.
41 changes: 30 additions & 11 deletions relax/src/main/java/com/emergetools/relax/Flow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,19 @@ class Flow(val packageName: String, val config: FlowConfig) {
textOrResId(textOrResId)
}

/**
* @return A UiObject which represents a view that matches the criteria.
*/
fun findObject(select: RelaxSelector.() -> Unit): UiObject = intercept {
val selector = RelaxSelector(packageName).apply(select).toUiSelector()
device.findObject(selector)
}

/**
* If the object exists, calls [block] on it and returns its return value, otherwise returns
* [default].
*/
private fun <T> findObject(
private fun <T> getObject(
select: RelaxSelector.() -> Unit,
default: T,
block: (UiObject) -> T
Expand All @@ -197,12 +205,23 @@ class Flow(val packageName: String, val config: FlowConfig) {
return if (obj.exists()) block(obj) else default
}

/**
* @param textOrResId If the string contains ":id/" it will be treated as a resource ID,
* otherwise searches for the element whose visible text matches exactly. Matching is
* case-sensitive.
* @return A UiObject which represents a view that matches the criteria.
* @throws UiObjectNotFoundException if the view represented by this UiObject does not exist
*/
fun getObject(textOrResId: String) = getObject {
textOrResId(textOrResId)
}

/**
* @return A UiObject which represents a view that matches the criteria.
* @throws UiObjectNotFoundException if the view represented by this UiObject does not exist
*/
fun findObject(select: RelaxSelector.() -> Unit): UiObject = intercept {
val selector = RelaxSelector(packageName).apply(select).toUiSelector()
device.findObject(selector).also {
fun getObject(select: RelaxSelector.() -> Unit): UiObject = intercept {
findObject(select).also {
if (!it.exists()) error(UiObjectNotFoundException(it.selector.toString()))
}
}
Expand Down Expand Up @@ -233,7 +252,7 @@ class Flow(val packageName: String, val config: FlowConfig) {
* @return true if successful, false otherwise
*/
fun click(select: RelaxSelector.() -> Unit): Boolean = intercept {
findObject(select, false) {
getObject(select, false) {
it.click().also { result ->
if (!result) error(UnsupportedOperationException("click ${it.selector}"))
waitForIdle()
Expand Down Expand Up @@ -281,7 +300,7 @@ class Flow(val packageName: String, val config: FlowConfig) {
* @return true if successful, false otherwise
*/
fun longClick(select: RelaxSelector.() -> Unit): Boolean = intercept {
findObject(select, false) {
getObject(select, false) {
it.longClick().also { result ->
if (!result) error(UnsupportedOperationException("longClick ${it.selector}"))
waitForIdle()
Expand Down Expand Up @@ -322,7 +341,7 @@ class Flow(val packageName: String, val config: FlowConfig) {
* @return true if successful, false otherwise
*/
fun inputText(inputText: String, select: RelaxSelector.() -> Unit): Boolean = intercept {
findObject(select, false) {
getObject(select, false) {
it.setText(inputText).also { result ->
if (!result) error(UnsupportedOperationException("inputText $inputText ${it.selector}"))
waitForIdle()
Expand Down Expand Up @@ -575,7 +594,7 @@ class Flow(val packageName: String, val config: FlowConfig) {
* @return true if successful, false otherwise
*/
fun swipeDown(select: RelaxSelector.() -> Unit): Boolean = intercept {
findObject(select, false) {
getObject(select, false) {
it.swipeDown(config.swipeSteps).also { result ->
if (!result) error(UnsupportedOperationException("swipeDown ${it.selector}"))
}
Expand Down Expand Up @@ -608,7 +627,7 @@ class Flow(val packageName: String, val config: FlowConfig) {
* @return true if successful, false otherwise
*/
fun swipeLeft(select: RelaxSelector.() -> Unit): Boolean = intercept {
findObject(select, false) {
getObject(select, false) {
it.swipeLeft(config.swipeSteps).also { result ->
if (!result) error(UnsupportedOperationException("swipeLeft ${it.selector}"))
}
Expand Down Expand Up @@ -641,7 +660,7 @@ class Flow(val packageName: String, val config: FlowConfig) {
* @return true if successful, false otherwise
*/
fun swipeRight(select: RelaxSelector.() -> Unit): Boolean = intercept {
findObject(select, false) {
getObject(select, false) {
it.swipeRight(config.swipeSteps).also { result ->
if (!result) error(UnsupportedOperationException("swipeRight ${it.selector}"))
}
Expand Down Expand Up @@ -674,7 +693,7 @@ class Flow(val packageName: String, val config: FlowConfig) {
* @return true if successful, false otherwise
*/
fun swipeUp(select: RelaxSelector.() -> Unit): Boolean = intercept {
findObject(select, false) {
getObject(select, false) {
it.swipeUp(config.swipeSteps).also { result ->
if (!result) error(UnsupportedOperationException("swipeUp ${it.selector}"))
}
Expand Down
3 changes: 3 additions & 0 deletions tests/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@

<queries>
<package android:name="com.emergetools.relaxexamples" />
<package android:name="org.joinmastodon.android" />
<package android:name="org.wikipedia" />
<package android:name="com.android.vending" />
</queries>
</manifest>
26 changes: 26 additions & 0 deletions tests/src/main/java/com/emergetools/relaxtests/AutoEmergeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.emergetools.relaxtests

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.emergetools.relax.FlowConfig
import com.emergetools.relax.Relax
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class AutoEmergeTest {

@Test
fun install() {
Relax("com.android.vending", FlowConfig(debug = true)) {
launchWithLink("https://play.google.com/store/apps/details?id=org.thoughtcrime.securesms")

if (findObject { description("Open") }.exists()) return@Relax

getObject { description("Install") }.click()

val installing = findObject("Installing…")
installing.waitForExists(60_000)
installing.waitUntilGone(60_000)
}
}
}
20 changes: 4 additions & 16 deletions tests/src/main/java/com/emergetools/relaxtests/FindObjectTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.emergetools.relaxtests
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.uiautomator.UiObjectNotFoundException
import com.emergetools.relax.Relax
import org.junit.Assert.assertFalse
import org.junit.Assert.assertThrows
import org.junit.Assert.assertTrue
import org.junit.Test
Expand All @@ -12,7 +13,7 @@ import org.junit.runner.RunWith
class FindObjectTest {

@Test
fun findObject() {
fun findObjectExists() {
Relax("com.emergetools.relaxexamples") {
pressHome()
launch()
Expand All @@ -27,22 +28,9 @@ class FindObjectTest {
Relax("com.emergetools.relaxexamples") {
pressHome()
launch()
val obj = findObject("DOESN'T EXIST")

assertThrows(UiObjectNotFoundException::class.java) {
findObject("DOESN'T EXIST")
}
}
}

@Test
fun optionalFindObject() {
Relax("com.emergetools.relaxexamples") {
pressHome()
launch()

optional {
findObject("DOESN'T EXIST")
}
assertFalse(obj.exists())
}
}
}
Loading