Skip to content

Android: Adds measureActivity test option #23

New issue

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

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

Already on GitHub? # to your account

Closed
wants to merge 4 commits into from
Closed
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
23 changes: 21 additions & 2 deletions src/test/kotlin/StartupTimeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import io.appium.java_client.android.AndroidDriver
import io.appium.java_client.ios.IOSDriver
import io.kotest.matchers.doubles.shouldBeGreaterThan
import io.kotest.matchers.doubles.shouldBeLessThan
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Assumptions
import org.junit.jupiter.api.Test
import org.openqa.selenium.WebDriver
import org.openqa.selenium.support.ui.FluentWait
import java.time.Duration
import kotlin.math.abs
import kotlin.math.ceil

Expand Down Expand Up @@ -192,6 +194,19 @@ class StartupTimeTest : TestBase() {
return appTimes
}

private fun AndroidDriver.waitForActivity(
desiredActivity: String,
sleepMs: Long = sleepTimeMs,
timeOutMs: Long = 10_000,
) {
printf("Waiting for activity: %s", desiredActivity)
val wait = FluentWait<WebDriver>(this)
.withTimeout(Duration.ofMillis(timeOutMs))
.pollingEvery(Duration.ofMillis(sleepMs))
wait.until { currentActivity()?.contains(desiredActivity) }
printf("Current activity: %s", currentActivity())
}

private fun collectAppStartupTime(
driver: AppiumDriver,
app: AppInfo,
Expand All @@ -212,6 +227,9 @@ class StartupTimeTest : TestBase() {
if (error != null) {
throw Exception(error[0])
}
app.measureActivity?.let {
androidDriver.waitForActivity(it)
}
} catch (e: Exception) {
// in case the app can't be launched or crashes on startup, print logcat output
val logs = driver.manage().logs().get("logcat").all.joinToString("\n")
Expand All @@ -220,7 +238,8 @@ class StartupTimeTest : TestBase() {
}

val logEntries = driver.manage().logs().get("logcat")
val regex = Regex("Displayed ${app.name}/\\.${app.activity}: \\+(?:([0-9]+)s)?([0-9]+)ms")
val trackedActivity = app.measureActivity ?: app.activity
val regex = Regex("Displayed ${app.name}/\\.$trackedActivity: \\+(?:([0-9]+)s)?([0-9]+)ms")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the time (ms) grabbed from the measureActivity is probably just the activity launch time and not the app launch time. The later needs to be calculated or grabbed in another way.

val times = logEntries.mapNotNull {
val groups = regex.find(it.message)?.groupValues
if (groups == null) {
Expand Down
7 changes: 6 additions & 1 deletion src/test/kotlin/TestOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import java.util.logging.Logger
import kotlin.io.path.createDirectories
import kotlin.io.path.notExists

data class AppInfo(val name: String, val activity: String? = null, val path: String) {
data class AppInfo(
val name: String,
val activity: String? = null,
val path: String,
val measureActivity: String? = null
) {
private val pathIsUrl = path.contains(':')

val file: Path by lazy {
Expand Down