0

I'm developing an Android Library Module which is highly customizable in terms of UI. It would be really nice to have a script or some sort of automated process that takes screen shots of the running app, concatenate them and send by e-mail - so then I could quickly check if some change has messed with some UI component and/or have the most recent assets to update library READ-ME.

Any idea on how this could be performed?


My current idea

So far I've thought in adding code to programmatically take SS, store them on a temporary folder and, when all images has been collected, send them via some REST API to a server. I'd like to know if there is a better way to do that.

E. Fernandes
  • 3,889
  • 4
  • 30
  • 48

1 Answers1

0

I ended up following my initial idea:

  1. Based on this answer I've implemented a method that takes screenshots;
  2. Base on this answer, I've implemented the API JavaMail capable of sending e-mails without the need of user interaction;

The combination of 1 and 2 can be found on my util library kotlin-components

Finally I've implemented UI tests that enters the desired state, takes the screen shots - saving them on external SD card - and, on the last step, it adds the SS as e-mail attachments sending to whatever I want to:

@RunWith(AndroidJUnit4::class)
@LargeTest
class UITestSearchSamples {

companion object {
    private val SCREENSHOTS_DIRECTORY = "search-interface"
    private val TIME_OUT = 3000L
    private val WAITING_TIME = 1000L

    @get:ClassRule
    var disableAnimationsRule = DisableAnimationsRule()
}

private var finished = false

@get:Rule
var mActivityRule = ActivityTestRule(ActivityHomepage::class.java)
private var mMonitor: Instrumentation.ActivityMonitor? = null

@Before
fun setup() {
    setWaitingPolice()
    mMonitor = getInstrumentation().addMonitor(ActivitySearch::class.java.name, null, false)
}

private fun performWaitingTime() {
    val idlingResource = ElapsedTimeIdlingResource(WAITING_TIME)
    Espresso.registerIdlingResources(idlingResource)
}

private fun setWaitingPolice() {
    IdlingPolicies.setMasterPolicyTimeout(TIME_OUT, TimeUnit.MILLISECONDS);
    IdlingPolicies.setIdlingResourceTimeout(TIME_OUT, TimeUnit.MILLISECONDS);
}

@After
fun tearDown() {
    closeSoftKeyboard()
    performWaitingTime()
    val activitySearch = getInstrumentation().waitForMonitorWithTimeout(mMonitor, TIME_OUT) as AppCompatActivity
    activitySearch.takeScreenShot(location = DirectoryPath.EXTERNAL, path = SCREENSHOTS_DIRECTORY, openScreenShot = false, showToast = false)
    activitySearch.finish()

    if (finished) {
        sendNotificationEmail(activitySearch)
    }
}

private fun sendNotificationEmail(activitySearch: AppCompatActivity) {
    try {
        val sender = Sender("sender_email", "sender_password")
        val email = Email(
                "Hello world: SMTP Server from Android with Attachments",
                "This is a sample e-mail sent via SMTP server from Android without the need of user interaction.",
                mutableListOf("recipient_01", "recipient_02"),
                File("${DirectoryPath.EXTERNAL.getValue(activitySearch)}/search-interface").listFiles()
        )
        activitySearch.sendEmail(sender, email)
    } catch (e: Exception) {
        Log.e("SENDER E-MAIL SLAVE", e.message, e)
    }
}

@Test
fun launchSample01() {
    onView(withId(R.id.btn_sample_01)).perform(click())
    onView(withId(R.id.input)).perform(typeText("Diana"))
}

@Test
fun launchSample02() {
    onView(withId(R.id.btn_sample_02)).perform(click())
    onView(withId(R.id.input)).perform(typeText("Clark"))
}

@Test
fun launchSample03() {
    onView(withId(R.id.btn_sample_03)).perform(click())
    onView(withId(R.id.input)).perform(typeText("Diana"))
    onView(withId(R.id.wrapper)).perform(click())
    performWaitingTime()
    onView(withId(R.id.input)).perform(typeText("a"))
    finished = true
}

}

Community
  • 1
  • 1
E. Fernandes
  • 3,889
  • 4
  • 30
  • 48