Mobile phone Automation test: Appium Source analysis of Bootstrap nine

Source: Internet
Author: User
Tags add time 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. Touchlongclick

Package Io.appium.android.bootstrap.handler;

Import Android.os.SystemClock;

Import Com.android.uiautomator.common.ReflectionUtils;

Import com.android.uiautomator.core.UiObjectNotFoundException;

Import Io.appium.android.bootstrap.Logger;

Import Java.lang.reflect.Method;

/**

* This handler are used to long click on the elements in the Android UI.

*

*/

public class Touchlongclick extends TouchEvent {

/*

* Uiautomator have a broken longclick, so we'll try to implement it using the

* Touchdown/touchup events.

*/

Private Boolean Correctlongclick (final int x, final int y, final int duration) {

try {

/*

* Bridge.getclass () returns Shelluiautomatorbridge on API 18/19 so use

* The Super class.

*/

Final Reflectionutils utils = new Reflectionutils ();

Final Method TouchDown = Utils.getcontrollermethod ("TouchDown", Int.class,

Int.class);

Final Method touchUp = Utils.getcontrollermethod ("TouchUp", Int.class, Int.class);

if ((Boolean) Touchdown.invoke (Utils.getcontroller (), x, y)) {

Systemclock.sleep (duration);

if ((Boolean) Touchup.invoke (Utils.getcontroller (), x, y)) {

return true;

}

}

return false;

} catch (Final Exception e) {

Logger.debug ("Problem invoking correct long click:" + E);

return false;

}

}

@Override

Protected Boolean executetouchevent () throws Uiobjectnotfoundexception {

Final Object paramduration = Params.get ("duration");

int duration = 2000; Seconds

if (paramduration! = null) {

Duration = Integer.parseint (paramduration.tostring ());

}

Printeventdebugline ("Touchlongclick", duration);

if (Correctlongclick (Clickx, Clicky, duration)) {

return true;

}

If Correctlongclick failed and we have an element

Then Uiautomator's Longclick is used as a fallback.

if (iselement) {

Logger.debug ("Falling Back to Broken Longclick");

return El.longclick ();

}

return false;

}

}

The Touchlongclick class inherits from the TouchEvent, And TouchEvent inherits from CommandHandler. Call TouchEvent's Execute method, call the Executetouchevent method, so we look at the above executetouchevent just fine, CEO Click event, in Uiaut Omator has the Uiobject.longclick () method, but the person who wrote the case knows that sometimes this method does not reach our needs, but we are our own reflection calls touchdown and touchup two methods, and in the Appium to help you solve, It helps you to do this, if you pass to a control object, that's fine, or call the Uiobject.longclick method, if you want to base on the coordinates, the time in the click, then call Currectlongclick this appium to you encapsulated good method.

Final Reflectionutils utils = new Reflectionutils ();

Final Method TouchDown = Utils.getcontrollermethod ("TouchDown", Int.class,

Int.class);

Final Method touchUp = Utils.getcontrollermethod ("TouchUp", Int.class, Int.class);

By reflection get Uiautomator in the class of no public, thus we want the method touchdown and Touchup.

Public Reflectionutils () throws IllegalArgumentException,

Illegalaccessexception, SecurityException, nosuchfieldexception {

Final Uidevice device = Uidevice.getinstance ();

Final Object bridge = Enablefield (Device.getclass (), "Muiautomationbridge")

. get (device);

if (api_18) {

Controller = Enablefield (Bridge.getclass (). Getsuperclass (),

"Minteractioncontroller"). Get (bridge);

} else {

Controller = Enablefield (Bridge.getclass (), "Minteractioncontroller")

. get (bridge);

}

}

Because of the Uiautomator API changes, Minteractioncontroller is a variable that exists in Uiautomationbridge's parent class in versions above Api18, and it exists in this class in versions below 18. So there's a little bit of difference in reflection, but in general it's about getting the Interactioncontroller class, because there's something we want but the touch class but the method in this class. Finally we can easily invoke the mouse touchup and touchdown them. Then add time, long press to achieve.

TouchUp

Package Io.appium.android.bootstrap.handler;

Import Com.android.uiautomator.common.ReflectionUtils;

Import com.android.uiautomator.core.UiObjectNotFoundException;

Import Io.appium.android.bootstrap.Logger;

Import Java.lang.reflect.Method;

/**

* This handler are used to perform a touchDown event on an element in the

* Android UI.

*

*/

public class TouchDown extends TouchEvent {

@Override

Protected Boolean executetouchevent () throws Uiobjectnotfoundexception {

Printeventdebugline ("TouchDown");

try {

Final Reflectionutils utils = new Reflectionutils ();

Final Method TouchDown = Utils.getcontrollermethod ("TouchDown", Int.class,

Int.class);

Return (Boolean) Touchdown.invoke (Utils.getcontroller (), Clickx, clicky);

} catch (Final Exception e) {

Logger.debug ("Problem invoking TouchDown:" + e);

return false;

}

}

}

With the above analysis, the analysis of Touchup and touchdown and Touchmove will not have to say more, is the principle of reflection

Mobile phone Automation test: Appium Source analysis of Bootstrap nine

Related Article

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.