apps-android-commons/app/src/androidTest/java/fr/free/nrw/commons/UITestHelper.kt
Madhur Gupta 36510a4301
Fix Instrumentation Tests (#4832)
* Fix AboutActivity Instrumentation Tests

* Fix WelcomeActivity Instrumentation Tests

* Fix SearchActivity Instrumentation Tests

* Fix SignupTest Instrumentation Tests

* Fix LoginActivityTest Instrumentation Tests

* Fix SettingsActivity Instrumentation Tests

* Fix LoginActivity Instrumentation Tests

* Update loginUser

* Add logoutUser

* Fix logoutUser

* Update github action to use env variables

* Fix github action to use env variables

* Fix github action

* Fix github action

* Fix github action

* Fix github action

* Fix github action

* Fix github action

* Fix github action

* Fix github action

* Fix github action

* Fix github action

* Fix github action

* Remove scroll

* Add wait time

* Fix keyboard issue

* Disable animation

* Enable animation

* Enable animation

* Disable animation

* Fix compile error

* Remove AVD Cache

* Set timezone and dns

* Add AVD cache

* Add API level

* Update API level to 24

* Add sleep timer

* Add scroll

* Remove logout

* Ignore some tests

* update logout

* update logout

* Fix scroll layout problem

* Fix scroll layout problem

* Ignore orientation test

* Update tests

* Update tests

* Update API level to 29

* Change device and API level to 27

* Use AndroidJUnit4

* Change API to 28

* Try with macos

* Use nested scrollview

* Update Profile Activity Test

* Update Profile Activity Test

* Turn off rotation

* Change device

* Remove failing test

* Change API to 24

* Add rotation lock for failing test

* Change API to 24

* Add sleep

* Remove text

* Remove child

* Force rotation on back press

* Force open more sheet

* Change device to pixel 5

* Change device to 13.5inch

* Update tests

* Ignore failing test

* Use linux

* Update tests

* Use repo secrets instead

* Add mainactivity tests

* Remove content description

* Different workflows for PR and branch, PR workflow will need maintainer approval to prevent secrets getting leaked

* Run instrumentation tests only for push and not for PRs

* Fix FileNotFound Exception

* Update MainActivity and Profile tests

* Fix coverage path issue

* Add wait time for map to load

* Open contribution

* Update settings tests

* Update main activity tests

* Fix failing test

* Fix failing test

* Increase login time

* Separate settings test

* Update review tests

* Ignore uploadCancelledAfterLocationPickedTest

* Update MainActivity Tests

* Update UploadCancelledTest

* Update UploadCancelledTest

* Update device

* Fix Init release error

* Fix failing test

* Fix failing test

* Try with long click

* Update UploadCancelledTest.kt

* Fix Leaderboard Test

* Update testContributions

* Set GPS coordinates

* Add zoomControllers to excludes

* Add ExploreActivityTest

* Remove failing condition

* Merge tests

* Remove failing test

* Remove failing test

* Fix failing test

* Fix failing test

* Update AboutActivityTest.kt

* Update jacoco.gradle
2022-04-26 21:09:15 +10:00

191 lines
No EOL
7.1 KiB
Kotlin

package fr.free.nrw.commons
import android.app.Activity
import android.content.pm.ActivityInfo
import android.view.View
import android.view.ViewGroup
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.NoMatchingViewException
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.rule.ActivityTestRule
import org.apache.commons.lang3.StringUtils
import org.hamcrest.*
import timber.log.Timber
class UITestHelper {
companion object {
fun skipWelcome() {
try {
onView(ViewMatchers.withId(R.id.button_ok))
.perform(ViewActions.click())
//Skip tutorial
onView(ViewMatchers.withId(R.id.finishTutorialButton))
.perform(ViewActions.click())
} catch (ignored: NoMatchingViewException) {
}
}
fun skipLogin() {
try {
//Skip Login
val htmlTextView = onView(
Matchers.allOf(
ViewMatchers.withId(R.id.skip_login), ViewMatchers.withText("Skip"),
ViewMatchers.isDisplayed()
)
)
htmlTextView.perform(ViewActions.click())
val appCompatButton = onView(
Matchers.allOf(
ViewMatchers.withId(android.R.id.button1), ViewMatchers.withText("Yes"),
childAtPosition(
childAtPosition(
ViewMatchers.withId(R.id.buttonPanel),
0
),
3
)
)
)
appCompatButton.perform(ViewActions.scrollTo(), ViewActions.click())
} catch (ignored: NoMatchingViewException) {
}
}
fun loginUser() {
try {
//Perform Login
sleep(3000)
onView(ViewMatchers.withId(R.id.login_username))
.perform(
ViewActions.replaceText(getTestUsername()),
ViewActions.closeSoftKeyboard()
)
sleep(2000)
onView(ViewMatchers.withId(R.id.login_password))
.perform(
ViewActions.replaceText(getTestUserPassword()),
ViewActions.closeSoftKeyboard()
)
sleep(2000)
onView(ViewMatchers.withId(R.id.login_button))
.perform(ViewActions.click())
sleep(10000)
} catch (ignored: NoMatchingViewException) {
}
}
fun logoutUser() {
try {
onView(
Matchers.allOf(
ViewMatchers.withContentDescription("More"),
childAtPosition(
childAtPosition(
ViewMatchers.withId(R.id.fragment_main_nav_tab_layout),
0
),
4
),
ViewMatchers.isDisplayed()
)
).perform(ViewActions.click())
onView(
Matchers.allOf(
ViewMatchers.withId(R.id.more_logout), ViewMatchers.withText("Logout"),
childAtPosition(
childAtPosition(
ViewMatchers.withId(R.id.scroll_view_more_bottom_sheet),
0
),
6
)
)
).perform(ViewActions.scrollTo(), ViewActions.click())
onView(
Matchers.allOf(
ViewMatchers.withId(android.R.id.button1), ViewMatchers.withText("Yes"),
childAtPosition(
childAtPosition(
ViewMatchers.withId(R.id.buttonPanel),
0
),
3
)
)
).perform(ViewActions.scrollTo(), ViewActions.click())
sleep(5000)
} catch (ignored: NoMatchingViewException) {
}
}
fun childAtPosition(
parentMatcher: Matcher<View>, position: Int
): Matcher<View> {
return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("Child at position $position in parent ")
parentMatcher.describeTo(description)
}
public override fun matchesSafely(view: View): Boolean {
val parent = view.parent
return parent is ViewGroup && parentMatcher.matches(parent)
&& view == parent.getChildAt(position)
}
}
}
fun sleep(timeInMillis: Long) {
try {
Timber.d("Sleeping for %d", timeInMillis)
Thread.sleep(timeInMillis)
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
private fun getTestUsername(): String {
val username = BuildConfig.TEST_USERNAME
if (StringUtils.isEmpty(username) || username == "null") {
throw NotImplementedError("Configure your beta account's username")
} else return username
}
private fun getTestUserPassword(): String {
val password = BuildConfig.TEST_PASSWORD
if (StringUtils.isEmpty(password) || password == "null") {
throw NotImplementedError("Configure your beta account's password")
} else return password
}
fun <T : Activity> changeOrientation(activityRule: ActivityTestRule<T>) {
activityRule.activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
assert(activityRule.activity.requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
activityRule.activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
assert(activityRule.activity.requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
}
fun <T> first(matcher: Matcher<T>): Matcher<T>? {
return object : BaseMatcher<T>() {
var isFirst = true
override fun matches(item: Any): Boolean {
if (isFirst && matcher.matches(item)) {
isFirst = false
return true
}
return false
}
override fun describeTo(description: Description) {
description.appendText("should return first matching item")
}
}
}
}
}