Possible bug in scrollTextIntoView() in UIAutomator

2,957 views
Skip to first unread message

Julian Harty

unread,
Mar 5, 2013, 9:37:49 AM3/5/13
to adt...@googlegroups.com
I have been experimenting with UI Automator using a Nexus 7 tablet and a Nexus 4 (LG) phone, (both running Android 4.2.2) together with the example code from http://developer.android.com/tools/testing/testing_ui.html

The example works on on my Nexus 7 tablet, however it fails to scroll to the "Settings" app on my Nexus 4. The main relevant difference between the devices seems to be the "Settings" are on the third page of Apps on my Nexus 4, and on the second page of Apps on my Nexus 7.

On the Nexus 4, the call to getChildByText(...) scrolls the list of Apps once then stops. It fails to find the "Settings" - on my phone "Settings" is on the third page of Apps. I have experimented with using various methods, including appViews.scrollTextIntoView("Settings"); which behaves similarly. I managed to get the test to work with a slight hack, calling appViews.scrollForward(); twice.

I've checked things like: appViews.getMaxSearchSwipes() which returns 30 - more than sufficient to allow the scrolling to work correctly e.g. using scrollTextIntoView(...).

My temporary workaround is to use appViews.scrollForward(); several times to scroll until the "Settings" is visible, then the call to getChildByText(...) works OK.

Note: I'm viewing the source for UiScrollable.java here https://android.googlesource.com/platform/frameworks/testing/+/f612e6a05f47b28ae0f5715545658c08dd759dd7/uiautomator/library/src/com/android/uiautomator/core/UiScrollable.java - this may not be the same version as used on my device, however it seems like the code *should* work for the example.

Let me know if you'd like me to post my code here. I'm assuming there's little value in doing so since I took it from the android example.

I'd appreciate someone testing the scenario where the Settings is on the 3rd page of results (or later) to see if the bug is reproducible. Also, if there's a reliable and relatively straight-forward way to build the UIAutomator library and deploy it to my devices I'd be happy to do some more testing of newer versions of the codebase, etc.

Thanks

Julian Harty


 


Julian Harty

unread,
Mar 5, 2013, 10:35:17 AM3/5/13
to adt...@googlegroups.com
An update: I have a Galaxy Nexus running Android 4.2.1 and the demo code works on this phone, albeit slowly. I'm about to apply the system update to 4.2.2 to this device and will retest once I've done so. Just waiting for the battery to charge sufficiently :)

Julian Harty

unread,
Mar 5, 2013, 4:58:09 PM3/5/13
to adt...@googlegroups.com
OK, the evidence is pointing to a bug in Android 4.2.2 - once I updated my Galaxy Nexus from 4.2.1 to 4.2.2 the test broke. As a precaution I then rebuilt the jar file and redeployed it. I'm building with a target of android-17.

Guang Zhu

unread,
Mar 25, 2013, 9:20:11 PM3/25/13
to adt...@googlegroups.com
Hi Julian,

Thanks for looking into this! We changed how end of scroll is detected in API Level 17, and this caused breakage in some cases where such events fail to be properly detected. We are addressing this issue in the next major platform release.

Thanks,
Guang Zhu

--
You received this message because you are subscribed to the Google Groups "adt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adt-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Guang Zhu
Android Test Engineering
Google Inc.

Please keep all discussions on related Google Groups, no direct emails. Thanks!

Harold Cuellar

unread,
Jul 11, 2013, 12:35:13 AM7/11/13
to adt...@googlegroups.com
Hi Guang Zhu,

I am just wondering when will be the next platform release for this issue? :)

Thanks,
Harold

Srilu Garlapati

unread,
Aug 5, 2013, 4:56:11 AM8/5/13
to adt...@googlegroups.com
Can you please post the code.

Thanks,
Sri

Julian Harty

unread,
Aug 13, 2013, 1:17:01 PM8/13/13
to adt...@googlegroups.com
Sri,
I wrote a blog post at the time which includes a workaround for this bug in 4.2.2 Here's the blog post http://blog.bettersoftwaretesting.com/2013/03/android-test-automation-getting-to-grips-with-ui-automator/ I hope it's straight-forward and enables you to do what you need to do.

Julian

Aaron

unread,
Dec 4, 2013, 4:22:37 PM12/4/13
to adt...@googlegroups.com
I've tested this on api 19 on KitKat using a Nexus 4.

It still doesn't scroll all the way through the Apps screen (swipes to the left once, then once to the right, and then fails), when searching for the Settings app, in the case of the example of Google's Android UI testing homepage.

Aaron

unread,
Dec 4, 2013, 5:23:47 PM12/4/13
to adt...@googlegroups.com
I believe I've found the bug.

If you look at https://android.googlesource.com/platform/frameworks/testing/+/jb-mr0-release%5E1/uiautomator/library/src/com/android/uiautomator/core/UiScrollable.java you'll get this public boolean scrollIntoView(UiSelector selector) method.

That method calls has the code:

  if(!scrollForward()) {
     return false;
  }

And if you put debugging statements into that, you'll see it fails to scroll forwards the first time. Simply changing the if statement to the below works.

if(!scrollForward() && x!=0) {

Obviously this is rather a nasty hack, and I'm unsure why it's needed.


In more detail, I have these print statements in my overridden methods. Note I'm simply commenting out the return statement in the if statement above to show it failing.

    @Override
    public boolean scrollIntoView(UiSelector selector) throws UiObjectNotFoundException {
        // if we happen to be on top of the text we want then return here
        System.out.println("My max search swipes are: ");
        System.out.println(getMaxSearchSwipes());
        if (exists(getSelector().childSelector(selector))) {
            return (true);
        } else {
            System.out.println("It doesn't exist on this page");
            // we will need to reset the search from the beginning to start search
            scrollToBeginning(getMaxSearchSwipes());
            System.out.println("I appear to have swiped to the beginning.");
            if (exists(getSelector().childSelector(selector))) {
                return (true);
            }
            for (int x = 0; x < getMaxSearchSwipes(); x++) {
                System.out.println("I'm going forward a page: " + x);
                if(!scrollForward()) {
                    System.out.println("Couldn't scroll forwards?");
                    //return false;
                }

                if(exists(getSelector().childSelector(selector))) {
                    return true;
                }
            }
        }
        return false;
    }   
   
    @Override
    public boolean scrollForward(int steps) throws UiObjectNotFoundException {
        System.out.println("I'm scrolling forwards by these steps: " + steps);
        return super.scrollForward(steps);
    }


And if I start search for an app (that doesn't exist) in the Apps method I get this output. Notice the Couldn't scroll forwards? at the end are normal, since I'm trying to scroll to a page that doesn't exist. However, the first 'Couldn't scroll forwards?' is incorrect -- it's on the first page and therefore should be able to scroll forwards. (in fact, from observations it does indeed seem to be scrolling the page)

My max search swipes are:
20
It doesn't exist on this page
I appear to have swiped to the beginning.
I'm going forward a page: 0
I'm scrolling forwards by these steps: 55
Couldn't scroll forwards?
I'm going forward a page: 1
I'm scrolling forwards by these steps: 55
I'm going forward a page: 2
I'm scrolling forwards by these steps: 55
I'm going forward a page: 3
I'm scrolling forwards by these steps: 55
I'm going forward a page: 4
I'm scrolling forwards by these steps: 55
I'm going forward a page: 5
I'm scrolling forwards by these steps: 55
I'm going forward a page: 6
I'm scrolling forwards by these steps: 55
I'm going forward a page: 7
I'm scrolling forwards by these steps: 55
I'm going forward a page: 8
I'm scrolling forwards by these steps: 55
I'm going forward a page: 9
I'm scrolling forwards by these steps: 55
I'm going forward a page: 10
I'm scrolling forwards by these steps: 55
I'm going forward a page: 11
I'm scrolling forwards by these steps: 55
I'm going forward a page: 12
I'm scrolling forwards by these steps: 55
Couldn't scroll forwards?
I'm going forward a page: 13
I'm scrolling forwards by these steps: 55
Couldn't scroll forwards?
I'm going forward a page: 14
I'm scrolling forwards by these steps: 55
Couldn't scroll forwards?
I'm going forward a page: 15
I'm scrolling forwards by these steps: 55
Couldn't scroll forwards?
I'm going forward a page: 16
I'm scrolling forwards by these steps: 55
Couldn't scroll forwards?
I'm going forward a page: 17
I'm scrolling forwards by these steps: 55
Couldn't scroll forwards?
I'm going forward a page: 18
I'm scrolling forwards by these steps: 55
Couldn't scroll forwards?
I'm going forward a page: 19
I'm scrolling forwards by these steps: 55
Couldn't scroll forwards?

It appears the  public boolean scrollForward(int steps) methods is erroneously saying it can't scroll forwards on the first attempt, whereas I believe it is doing from my observations.

Here's the source to my example program:

        getUiDevice().pressHome();
       
        UiObject allAppsButton = new UiObject(new UiSelector().description("Apps"));
        allAppsButton.clickAndWaitForNewWindow();
       
        UiObject appsTab = new UiObject(new UiSelector().text("Apps"));
        appsTab.click();
       
        UiScrollable appsView = new UiScrollable(new UiSelector().scrollable(true));
        appsView.setMaxSearchSwipes(20);
        appsView.setAsHorizontalList();
        UiObject settingsApp = appsView.getChildByText(
                new UiSelector().className(android.widget.TextView.class.getName()),
                "xxSettings",  // Intentionally failing here for the above demonstration.
                true);
        settingsApp.clickAndWaitForNewWindow();

Benton Lam

unread,
Feb 25, 2015, 1:27:05 PM2/25/15
to adt...@googlegroups.com
Any updates on this issue? anyone know if it's resolved?

Yanguang Du

unread,
Jun 4, 2015, 5:45:19 PM6/4/15
to adt...@googlegroups.com
Any fix on UiScrollable scroll method, I think all scroll methods are not reliable. 
Reply all
Reply to author
Forward
0 new messages