3

I'm doing some black box testing (using UiAutomator 2.0 btw, extending InstrumentationTestCase) and I need to know:

1 - when a new activity is created

2 - to know if it's the first time the activity is created

I need this because there are some tests that I want to apply when a new activity appears but I want this detection to be automatic, not manual.

Prior to Android L there was the UiDevice.getCurrentActivityName() method. However, now it is deprecated (moreover, they don't even ensure it works for previous versions). This also happened with the options to getting the activity though the PackageManager.

As such, I would like to know:

  • Is it possible to programatically detect a new activity? If so, is is possible to know if it's the first time the activity occurs.

  • If it's not possible, how should I define an activity according to its UI? How many widgets should change for me to conclude it's a different activity?

Thanks.

EDIT: Just to be clear, I don't want to test what happens when the activity is created, I want to be able to identify if it's the first time this activity occurs in a run.

Inês
  • 882
  • 3
  • 13
  • 28
  • Just to clarify, because you said black box testing, I assume you don't actually have access to the source code of the app you are testing, right? – Chamatake-san Jun 04 '15 at 14:46
  • that's exactly right (only exactly wasn't enough to add the comment xD) – Inês Jun 04 '15 at 16:42
  • `Black-box testing` stinks of `reverse engineering`. Renamed. – Phantômaxx Jun 04 '15 at 17:32
  • Can your test automation simulate rotating the device? It's not the best approach, but the activity in focus will often have to be destroyed and recreated after the device orientation changes (depending on how the code handles configuration changes). If you know that the targeted activity will be recreated after device orientation changes, you may be able to run your tests after the app redraws that activity. Activity.onStart() will be called when stuff is first visible; Activity.onResume() will be called when the user can interact with the app. Don't know if that helps any. Good luck! – Chamatake-san Jun 04 '15 at 22:41
  • @DerGolem, why do you say that? -.- – Inês Jun 05 '15 at 15:25
  • @Chamatake-san, I'm not interested in testing the change of an activity. I want to be able to detect if it's the first time this activity is being run. – Inês Jun 05 '15 at 15:26
  • Because it really stinks so. MUCH. – Phantômaxx Jun 05 '15 at 16:27
  • @Inês, Did you find it? UiDevice.getCurrentActivityName() is deprecated.. Is there another way to get the current activity? e.g., through another API call? – Thanasis Petsas Oct 23 '15 at 20:54
  • @ThanasisPetsas, I didn't find any immediate way. I had to implement an heuristic to analyse the screen, compare with the previous one and decide for myself if it was a different one or not. – Inês Oct 25 '15 at 17:15

1 Answers1

0

Here is what I currently implemented for this problem, base on a assumption that the same Activity won't change it's UI View hierarchy dynamically. This is OK to me, seems I only want to distinguish between UI changes, not necessary for Activities.

1.Build a signature to identify between screens

signature1:FrameLayout;ListView;LinearLayout;RelativeLayout;...
signature2:FrameLayout;FrameLayout;FrameLayout;LinearLayout;...

2.Then, use a List to keep screens you already known.

3.For performance, you can use a fixed length for screen signatue.

The idea is refer to this paper

And if adb is ok to you, you can use this command,

adb shell dumpsys activity

This refer to another post here

Community
  • 1
  • 1
Eaway Lu
  • 51
  • 2
  • I ended up doing something similar to this. I save the elements present in the screen and then compare it to a different reading of the screen. And I ignore any changes to editable fields (edit boxes, check boxes, ...) – Inês Nov 24 '15 at 10:35