Skip to content

Commit

Permalink
feat: Disable screenshots and hierarchy snapshots on failures
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Aug 4, 2023
1 parent 05e3143 commit 4dee6d2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.espressoserver.lib.helpers

import android.content.Context
import android.view.View
import androidx.test.espresso.FailureHandler
import androidx.test.espresso.base.DefaultFailureHandler
import org.hamcrest.Matcher
import java.util.concurrent.atomic.AtomicInteger

class CustomFailureHandler(appContext: Context) : FailureHandler {
private val originalHandler = DefaultFailureHandler(appContext)

override fun handle(error: Throwable?, viewMatcher: Matcher<View>?) {
val failureCountField = DefaultFailureHandler::class.java.getDeclaredField("failureCount")
failureCountField.isAccessible = true
(failureCountField.get(originalHandler) as AtomicInteger).incrementAndGet()

val handlersField = DefaultFailureHandler::class.java.getDeclaredField("handlers")
handlersField.isAccessible = true
@Suppress("UNCHECKED_CAST")
for (handler in (handlersField.get(originalHandler) as java.util.ArrayList<FailureHandler>)) {
handler.handle(error, viewMatcher)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package io.appium.espressoserver.lib.http

import androidx.test.espresso.Espresso
import androidx.test.platform.app.InstrumentationRegistry
import com.google.gson.GsonBuilder
import fi.iki.elonen.NanoHTTPD
import io.appium.espressoserver.lib.helpers.AndroidLogger
import io.appium.espressoserver.lib.helpers.CustomFailureHandler
import io.appium.espressoserver.lib.helpers.StringHelpers
import io.appium.espressoserver.lib.helpers.setAccessibilityServiceState
import io.appium.espressoserver.lib.http.response.AppiumResponse
Expand Down Expand Up @@ -86,6 +89,7 @@ object Server : NanoHTTPD(DEFAULT_PORT) {
}

setAccessibilityServiceState()
setCustomFailureHandler()

try {
super.start(SOCKET_READ_TIMEOUT, false)
Expand All @@ -98,6 +102,11 @@ object Server : NanoHTTPD(DEFAULT_PORT) {
router = Router()
}

private fun setCustomFailureHandler() =
Espresso.setFailureHandler(
CustomFailureHandler(InstrumentationRegistry.getInstrumentation().targetContext)
)

override fun stop() {
super.stop()
AndroidLogger.info("\nStopping Appium Espresso at port $DEFAULT_PORT\n")
Expand Down

0 comments on commit 4dee6d2

Please sign in to comment.