Mobile phone Automation test: Appium Source analysis of Bootstrap 12

Source: Internet
Author: User
Tags appium

Poptest is the only training institute for developing Test and development engineers in China, aiming at the ability of the trainees to be competent in automated testing, performance testing and testing tools development. If you are interested in the course, please consult qq:908821478. ScrollTo

Package Io.appium.android.bootstrap.handler;

Import Com.android.uiautomator.core.UiObject;

Import com.android.uiautomator.core.UiObjectNotFoundException;

Import com.android.uiautomator.core.UiScrollable;

Import Com.android.uiautomator.core.UiSelector;

Import io.appium.android.bootstrap.*;

Import org.json.JSONException;

Import java.util.Hashtable;

/**

* This handler are used to scroll to elements in the Android UI.

*

* Based on the element Id of the scrollable, scroll to the object with the

* Text.

*

*/

public class ScrollTo extends CommandHandler {

/*

* @param command the {@link Androidcommand}

*

* @return {@link Androidcommandresult}

*

* @throws jsonexception

*

* @see Io.appium.android.bootstrap.commandhandler#execute (io.appium.android.

* Bootstrap. Androidcommand)

*/

@Override

Public Androidcommandresult Execute (final androidcommand command)

Throws Jsonexception {

if (!command.iselementcommand ()) {

Return Geterrorresult ("A scrollable view is required for this command.");

}

try {

Boolean result;

Final hashtable<string, object> params = Command.params ();

Final String Text = params.get ("text"). ToString ();

Final String direction = params.get ("direction"). ToString ();

Final androidelement el = command.getelement ();

if (!el.getuiobject (). Isscrollable ()) {

Return Geterrorresult ("The provided view is not scrollable.");

}

Final uiscrollable view = new Uiscrollable (El.getuiobject (). Getselector ());

if (Direction.tolowercase (). Contentequals ("horizontal")

|| View.getclassname (). Contentequals (

"Android.widget.HorizontalScrollView")) {

View.setashorizontallist ();

}

View.scrolltobeginning (100);

View.setmaxsearchswipes (100);

result = View.scrolltextintoview (text);

View.waitforexists (5000);

Make sure we can get to the item

UiObject ListViewItem = view.getchildbyinstance (

New Uiselector (). Text (text), 0);

We need to make sure that the item exists (visible)

if (! ( Result && listviewitem.exists ())) {

Return Geterrorresult ("Could not scroll element into view:" + text);

}

return Getsuccessresult (Result);

} catch (Final Uiobjectnotfoundexception e) {

return new Androidcommandresult (Wdstatus.no_such_element, E.getmessage ());

} catch (Final NullPointerException e) {//El is null

return new Androidcommandresult (Wdstatus.no_such_element, E.getmessage ());

} catch (Final Exception e) {

return new Androidcommandresult (Wdstatus.unknown_error, E.getmessage ());

}

}

}

In Uiautomator, it is sometimes necessary to find an item in a scrolling list, and this item is in a variable position, and we can find the control of a particular text by Uiscrollable Scrollto. And the bootstrap of this scrollto is to encapsulate such a demand.

First, you determine whether the control is scrollable, and then create the Uiscrollable object, because the default scrolling style is vertical, and if you need horizontal orientation, set the orientation to horizontal.

View.setashorizontallist ();

Then roll the scroll control to the beginning, then set the maximum search range to 100 times, because it is impossible to search forever. And then start calling

View.scrolltextintoview (text);

Start scrolling, and finally confirm whether you want to scroll to set the goal:

View.waitforexists (5000);

Because there may be a flush to the time, the calling method then passes in the time 5 seconds.

UiObject ListViewItem = view.getchildbyinstance (

New Uiselector (). Text (text), 0);

Finally, check the result, get the object that made text, determine whether it exists and return the corresponding result to the client.

Mobile phone Automation test: Appium Source analysis of Bootstrap 12

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.