Unable to get "mobile: find" working with java bindings

3,099 views
Skip to first unread message

Aniket Gadre

unread,
Jul 26, 2013, 6:50:25 AM7/26/13
to appium-...@googlegroups.com
Hi ,
I am trying to use mobile method mobile: find and following is my code. I think I am not sending parameters in correct format


Java Code:
HashMap<String, String> scrollParams = new HashMap<String , String>();
HashMap<String, HashMap<String , String>> scrollListView = new HashMap<String, HashMap<String , String>>();

scrollParams.put("3", "Gallery");
scrollParams.put("7", "Gallery");
scrollListView.put("scroll", scrollParams);

js.executeScript("mobile: find",  scrollListView);


Command fails because of following error:
info: [ADB STDOUT] java.lang.ClassCastException: org.json.JSONObject cannot be cast to org.json.JSONArray

Basically below JSON object is not being typecasted to JSONArray

{"scroll":
           "3":"Gallery",
  "7":"Gallery"
}
}

Line of failure in Find.Java:
} else if (strategy == Strategy.DYNAMIC) {
      Logger.debug("Finding dynamic.");
      final JSONArray selectors = (JSONArray) params.get("selector");
      final String option = selectors.get(0).toString().toLowerCase();
      final boolean all = option.contentEquals("all");
      Logger.debug("Returning all? " + all);
      UiScrollable scrollable = null;
      final boolean scroll = option.contentEquals("scroll");
      if (scroll) {
        UiSelector scrollableListView = new UiSelector().className(
            android.widget.ListView.class).scrollable(true);
        if (!new UiObject(scrollableListView).exists()) {
          // Select anything that's scrollable if there's no list view.
          scrollableListView = new UiSelector().scrollable(true);
        }

        // Nothing scrollable exists.
        if (!new UiObject(scrollableListView).exists()) {
          return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT,
              "No such element.");
        }

        scrollable = new UiScrollable(scrollableListView).setAsVerticalList();
      }
      Logger.debug("Scrolling? " + scroll);

bootstrap online

unread,
Jul 26, 2013, 8:17:18 AM7/26/13
to Aniket Gadre, appium-...@googlegroups.com
mobile find works with arrays. You need this JSON:

["scroll",[[3,"Gallery"]],[[7,"Gallery"]]]
> --
> http://appium.io
> ---
> You received this message because you are subscribed to the Google Groups
> "Appium-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to appium-discus...@googlegroups.com.
> Visit this group at http://groups.google.com/group/appium-discuss.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

bootstrap online

unread,
Jul 26, 2013, 9:00:40 AM7/26/13
to appium-...@googlegroups.com
On Fri, Jul 26, 2013 at 9:00 AM, bootstrap online
<ma...@bootstraponline.com> wrote:
> I added a Java example.
> https://github.com/appium/appium/blob/master/docs/mobile_methods.md#mobile-find

Aniket Gadre

unread,
Jul 26, 2013, 9:05:21 AM7/26/13
to appium-...@googlegroups.com
Doesn't seem to work.. I got below error

.tablet.TabletTitleActivityException in thread "main" java.lang.IllegalArgumentException: Argument is of an illegal type: org.json.JSONArray

bootstrap online

unread,
Jul 26, 2013, 9:12:26 AM7/26/13
to Aniket Gadre, appium-...@googlegroups.com
There must be some way to pass in a JSONArray directly.

bootstrap online

unread,
Jul 26, 2013, 9:19:20 AM7/26/13
to Aniket Gadre, appium-...@googlegroups.com
I think you have to subclass driver and then call the protected
execute method that accepts JSON.
https://code.google.com/p/selenium/source/browse/java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java

On Fri, Jul 26, 2013 at 9:12 AM, bootstrap online

mummana subramanya

unread,
Aug 5, 2013, 1:01:43 AM8/5/13
to appium-...@googlegroups.com, Aniket Gadre
damn it dint worked for me mobile:find not working in android :(

Aniket Gadre

unread,
Aug 5, 2013, 1:49:00 AM8/5/13
to mummana subramanya, appium-...@googlegroups.com
I gave up with mobile: find...used scrollTo instead
--
Best Regards,
Aniket Gadre
Test Automation Lead
Amdocs , Pune.
E-add: aniket...@amdocs.com

mummana subramanya

unread,
Aug 5, 2013, 1:51:16 AM8/5/13
to Aniket Gadre, appium-...@googlegroups.com
hie bro i have used swipe method (swipe up to down ) for scrolling but it is too lengthy .please can uh provide me the code snippet of it please 

Aniket Gadre

unread,
Aug 5, 2013, 1:55:21 AM8/5/13
to mummana subramanya, appium-...@googlegroups.com
PFB the code...Here, list can be any Scrollable view

//Scroll the List view
WebElement objListView = driver.findElement(By.tagName("list"));

//ScrollTo Params
HashMap<String, String> objListScroll = new HashMap<String, String>();
objListScroll.put("text", "Gallery");
objListScroll.put("element", ((RemoteWebElement)objListView).getId());
js.executeScript("mobile: scrollTo", objListScroll);


mummana subramanya

unread,
Aug 5, 2013, 2:14:53 AM8/5/13
to Aniket Gadre, appium-...@googlegroups.com
hiee ,fo me its not working .... i am trying to use contacts and want to find the desired name but in vain ... its getting clicked on keypad every time ...

here is  the snippet of it..

WebElement objListView = driver.findElement(By.tagName("TextView"));

        //ScrollTo Params
        JavascriptExecutor js = (JavascriptExecutor) driver;

        HashMap<String, String> objListScroll = new HashMap<String, String>();
        objListScroll.put("text","Sameer");
        objListScroll.put("element",((RemoteWebElement)objListView).getId());

        js.executeScript("mobile: scrollTo", objListScroll);

Aniket Gadre

unread,
Aug 5, 2013, 2:22:55 AM8/5/13
to mummana subramanya, appium-...@googlegroups.com
TextView cannot be scrolled. It should be a _Scrollable_ view. Check for scrollable view up in hierarchy

mummana subramanya

unread,
Aug 5, 2013, 2:23:41 AM8/5/13
to appium-...@googlegroups.com, mummana subramanya
hiee ,fo me its not working .... i am trying to use contacts and want to find the desired name but in vain ... its getting clicked on keypad every time ...

here is  the snippet of it..

WebElement objListView = driver.findElement(By.tagName(
"TextView"));

        //ScrollTo Params
        JavascriptExecutor js = (JavascriptExecutor) driver;
        HashMap<String, String> objListScroll = new HashMap<String, String>();
        objListScroll.put("text","Sameer");
        objListScroll.put("element",((RemoteWebElement)objListView).getId());
js.executeScript("mobile: scrollTo", objListScroll);

mummana subramanya

unread,
Aug 5, 2013, 2:44:49 AM8/5/13
to appium-...@googlegroups.com, mummana subramanya
hie , there is no scrollable view in the hierarchy only View is there ...

Aniket Gadre

unread,
Aug 5, 2013, 2:56:19 AM8/5/13
to mummana subramanya, appium-...@googlegroups.com
View can be any view with scrollable property as true. It need not necessarily be of class "ScrollableView"
In my case its a "ListView"

mummana subramanya

unread,
Aug 5, 2013, 5:07:49 AM8/5/13
to appium-...@googlegroups.com, mummana subramanya
thanks aniket
its working fine but its getting scrolled up instead of  scrolling down  are there any parameters to change? it?

Aniket Gadre

unread,
Aug 5, 2013, 6:11:15 AM8/5/13
to mummana subramanya, appium-...@googlegroups.com
As far as I remember, last time I saw the code, it scrolls to top 1st and then starts scrolling down

mummana subramanya

unread,
Aug 5, 2013, 6:13:00 AM8/5/13
to Aniket Gadre, appium-...@googlegroups.com
but in mine its getting scrolled up for some time and its getting shutdown before finding the element

ritika mishra

unread,
Aug 28, 2013, 5:01:12 AM8/28/13
to appium-...@googlegroups.com
Hi,

Any update on this? I too am not able to get "driver.executeScript("mobile: find", json);" work for me.
The way out that Aniket suggested doesn't seem to work properly. It keeps trying to scroll up, even though it is at the top of the list.
As mentioned by Aniket it will try to scroll up for the first time but then it should scroll down in the second attempt, instead it keeps scrolling up.

Was any one able to find a solution to it?

Thanks,
Ritika

Ramon Cidade

unread,
Mar 21, 2014, 2:31:48 PM3/21/14
to appium-...@googlegroups.com
Hi,
Just to share, I'm using this with and it's working!
My code:
HashMap<String, String> scrollObject = new HashMap<String, String>();
//Must be an element with scrollable  property; Android (ListView,ScrollabeView) IOS (UIAScrollableView
RemoteWebElement element = (RemoteWebElement) driver.findElement(By.tagName(scrollableElement));
JavascriptExecutor js = (JavascriptExecutor) driver;
String widId = ((RemoteWebElement) element).getId();
 //Text for search on the screen
scrollObject.put("text", text);
scrollObject.put("element", widId);
js.executeScript("mobile: scrollTo", scrollObject);

I was wordering if this text could be a content-desc or a name?

Luigi GougouGaga

unread,
Jun 11, 2014, 3:54:09 PM6/11/14
to appium-...@googlegroups.com

Found a simpler way in Java, here is the code :

public
void toSwipeThisTo(String UnString, String OneScroll) throws Exception {

// fonction to close

        WebElement button =  driver.findElementByAccessibilityId(UnString);

        button.click();

        WebElement scrollButton =  driver.findElementByClassName(UIAPickerWheelString);

        scrollButton.sendKeys(OneScroll);

Reply all
Reply to author
Forward
0 new messages